Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put data from ResultSet to HashMap using Lambda expression?

I have a table in my SQL Server 2012 Database as follows:

Student:
ID, CardNo, Name, ZipCode, city, ...

I'm writing a Java desktop application using JDK 8, in which I want to put selected rows from this table in a HashMap with CardNo as key and Name as value.

What's the efficient way to fill this HashMap using a lambda expression?

Note: CardNo is unique.

like image 550
O.Badr Avatar asked May 08 '16 23:05

O.Badr


People also ask

How to populate a map using lambda expression?

These key/value pairs are also called map entries. In the below example, we can populate a Map using a lambda expression. We have passed Character and Runnable arguments to Map object and pass a lambda expression as the second argument in the put () method of Map class.

How to initialize HashMap with Lambda in Java 8?

Java 8 Object Oriented Programming Programming To initialize a HashMap with Lambda, at first create a HashMpa and use Callable − HashMap<Integer, Callable<Integer>>map = newHashMap<Integer, Callable<Integer>> () For key-value pair, you need to initialize them in the following way −

What is lambda expression in Java?

A lambda expression is one or more line of code which works like function or method. It takes a parameter and returns the value. Lambda expression can be used to convert ArrayList to HashMap.

How to convert ArrayList to HashMap using lambda expression?

A lambda expression is one or more line of code which works like function or method. It takes a parameter and returns the value. Lambda expression can be used to convert ArrayList to HashMap. Below is the implementation of the above approach. Time Complexity: O (N), where N is the length of Arraylist.


3 Answers

Here is a simpler way:

List<Student> students = xxxDao.retrieveStudents();

Map<Integer, String> map = students.stream()
            .collect(Collectors.toMap(Student::getCardNo, Student::getName));

Also, you could add a static import for Collectors.toMap and make the expression even more readable.

like image 109
GiovanyMoreno Avatar answered Oct 19 '22 19:10

GiovanyMoreno


How about this:

List<Student> students = xxxDao.findFromDB();
Map<String, String> map = students.stream().collect(
                                   Collectors.groupingBy(Student::getCardNo, 
                                              Collectors.collectingAndThen(
                                                         Collectors.mapping(Student::getName, 
                                                                    Collectors.toList()), nameList->nameList.get(0))));

-------------------------------------------------------split line------------------------------------------------------------------

This way I give above is much rather complex, and thanks to the @FedericoPeraltaSchaffner, there is other more simple way to express what we want. Since CardNo is unique, just use Collectors.toMap

List<Student> students = xxxDao.findFromDB();
Map<String, String> map = students.stream().collect(Collectors.toMap(Student::getCardNo, Student::getName));
like image 20
zhouxin Avatar answered Oct 19 '22 20:10

zhouxin


Why use a lambda? Keep it simple:

ResultSet resultSet = stmt.executeQuery(query); // assuming this

Map<String, String> map = new HashMap<>();
while(resultSet.next())
    map.put(resultSet.getString("CardNo"), resultSet.getString("Name"));

Using streams/lambdas is not automatically the best option, as here.

like image 1
Bohemian Avatar answered Oct 19 '22 21:10

Bohemian