Convert List to Map After Java 8
Hello everyone, Here we will learn how to convert List to Map in after Java 8 using Streams.
What's new in this example?
- From Java 14 onwards, the 'record' is a special type of class declaration aimed at reducing the boilerplate code.
- From Java 10 onwards, the 'var' keyword allows local variable type inference, which means the type for the local variable will be inferred by the compiler, so we don't need to declare that.
Example 1: Convert List to Map after Java 14 using Streams
package com.knf.dev.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String []args)
{
//The var keyword was introduced in Java 10.
var users = new ArrayList<User>();
var user1 = new User(1,"user1");
var user2 = new User(2,"user2");
var user3 = new User(3,"user3");
var user4 = new User(4,"user4");
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
System.out.println("User List: "+users);
System.out.println("Converting........");
System.out.println("User Map: "+
convertListToMapAfterJava14(users));
}
public static Map<Integer, User>
convertListToMapAfterJava14(List<User> list) {
var userMap
= list.stream()
.collect(Collectors.toMap(User::id,
Function.identity()));
return userMap;
}
}
record User (Integer id,String name){}
Output:
Example 2: Convert List to Map after Java 10 using Streams
package com.knf.dev.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String []args)
{
//The var keyword was introduced in Java 10.
var users = new ArrayList<User>();
var user1 = new User(1,"user1");
var user2 = new User(2,"user2");
var user3 = new User(3,"user3");
var user4 = new User(4,"user4");
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
System.out.println("User List: "+users);
System.out.println("Converting........");
System.out.println("User Map: "+
convertListToMapAfterJava10(users));
}
public static Map<Integer, User>
convertListToMapAfterJava10(List<User> list) {
var userMap
= list.stream()
.collect(Collectors.toMap(User::getId,
Function.identity()));
return userMap;
}
}
class User {
private Integer id;
private String name;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Output:
Example 3: Convert List to Map after Java 8 using Streams
package com.knf.dev.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String []args)
{
List<User> users = new ArrayList<User>();
User user1 = new User(1,"user1");
User user2 = new User(2,"user2");
User user3 = new User(3,"user3");
User user4 = new User(4,"user4");
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
System.out.println("User List: "+users);
System.out.println("Converting........");
System.out.println("User Map: "+
convertListToMapAfterJava8(users));
}
public static Map<Integer, User>
convertListToMapAfterJava8(List<User> list) {
var userMap
= list.stream()
.collect(Collectors.toMap(User::getId,
Function.identity()));
return userMap;
}
}
class User {
private Integer id;
private String name;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}