Posts

Showing posts with the label Convert List to Map

Convert List to Map After Java 8

Image
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(