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 + '\'' +
'}';
}
}

Output:

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

ReactJS - Bootstrap - Buttons

Spring Core | BeanFactoryPostProcessor | Example

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Custom Exception Handling in Quarkus REST API

ReactJS, Spring Boot JWT Authentication Example

Java, Spring Boot Mini Project - Library Management System - Download

Top 5 Java ORM tools - 2024

Java - DES Encryption and Decryption example