Java 8 – Convert Stream to LinkedHashMap

In this article, we will discuss how to convert Stream into a LinkedHashMap in Java 1.8 version using Stream API

Stream to LinkedHashMap :

Using Collectors.toMap() method, we can convert Stream into a Map. There are 2 variants of Collectors.toMap() method,

  1. Collectors.toMap(keyMapper, valueMapper, mergeFunction)
  2. Collectors.toMap(keyMapper, valueMapper, mergeFunction, supplier)

1. Using Collectors.toMap(keyMapper, valueMapper, mergeFunction) :

  • First variant of Collectors.toMap() method accepts 3 input-arguments
    1. Key mapper – mapping function to produce keys
    2. Value mapper – mapping function to produce values
    3. Merge Function – this is used to resolve collisions between values associated with the same key
  • Above method helps to convert Stream into Map
  • For converting into LinkedHashMap, create LinkedHashMap object and pass above obtained Map as constructor-argument
  • LinkedHashMap stores Key-Value pairs according to insertion order of Keys
  • Finally, print converted LinkedHashMap pairs to console

StreamToMapUsingCollectorsToMap.java

package net.bench.resources.stream.to.linkedhashmap;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamToMapUsingCollectorsToMap {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. convert Stream<String> to Map<String, Integer>
		Map<String, Integer> map = nameStream
				.collect(Collectors.toMap(
						Function.identity(), // 1. actual String as KEY
						String::length,  // 2. String length as their VALUE
						(key1, key2) -> key1) // 3. duplicate KEY resolver
						);


		// 2.1 print to console
		System.out.println("1. Stream to Map conversion : \n\n" + map);


		// 3. convert Map to LinkedHashMap using inter-conversion constructor
		LinkedHashMap<String, Integer> hMap = new LinkedHashMap<>(map);


		// 3.1 print to console
		System.out.println("\n\n2. Stream to LinkedHashMap conversion : \n\n" + hMap);
	}
}

Output:

1. Stream to Map conversion : 

{Lingaraj=8, Abdul=5, Rajiv=5, Santosh=7, Anbu=4}


2. Stream to LinkedHashMap conversion : 

{Lingaraj=8, Abdul=5, Rajiv=5, Santosh=7, Anbu=4}

2. Using Collectors.toMap(keyMapper, valueMapper, mergeFunction, supplier) :

  • This is the 2nd variant of Collectors.toMap() method which accepts 4 input-arguments
    1. Key mapper – mapping function to produce keys
    2. Value mapper – mapping function to produce values
    3. Merge Function – this is used to resolve collisions between values associated with the same key
    4. Supplier – function which returns a new, empty Map into which the results will be inserted
  • Above method helps to convert Stream into HashMap, LinkedHashMap or TreeMap directly or whichever Supplier we pass as 4th argument
  • In the below example, we are passing LinkedHashMap implementation class as method/constructor reference LinkedHashMap::new
  • LinkedHashMap stores Key-Value pairs according to insertion order of Keys
  • Finally, print converted LinkedHashMap pairs to console

StreamToLinkedHashMapUsingCollectorsToMap.java

package net.bench.resources.stream.to.linkedhashmap;

import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamToLinkedHashMapUsingCollectorsToMap {

	public static void main(String[] args) {

		// 1. Stream of String tokens
		Stream<String> nameStream = Stream.of(
				"Rajiv",
				"Anbu",
				"Santosh",
				"Abdul",
				"Lingaraj"
				);


		// 2. convert Stream<String> to LinkedHashMap<String, Integer>
		LinkedHashMap<String, Integer> lhMap = nameStream
				.collect(Collectors.toMap(
						Function.identity(), // 1. actual String as KEY
						String::length,  // 2. String length as their VALUE
						(key1, key2) -> key1, // 3. duplicate KEY resolver
						LinkedHashMap::new // 4. implementation-class
						));


		// 2.1 print to console
		System.out.println("Stream to LinkedHashMap conversion : \n\n" + lhMap);
	}
}

Output:

Stream to LinkedHashMap conversion : 

{Rajiv=5, Anbu=4, Santosh=7, Abdul=5, Lingaraj=8}

Related Articles:

References :

Happy Coding !!
Happy Learning !!

Java 8 - Convert Stream to TreeMap
Java 8 - Convert Stream to HashMap