Convert a Stream to HashMap in Java 8

309 total views, 2 views today

Welcome to Java 8 series. In this tutorial, you will learn about how to convert a Stream to a HashMap in Java 8.

If you would like to learn more about Java Stream operations.Please check out this link.

In order, to convert a stream to a collection we have to use Collectors api provided in Java 8. So we have to import the below package

java.util.stream.Collectors

Convert a stream to a HashMap using Collectors.toMap()

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StaticReference
{
    public static void main(String[] args)
    {

        List<String> fruits = Arrays.asList("apple", "mango", "pomo", 
                                            "banana", "orange", "apple");

        Map<String, String> collectedFruits = fruits
                .stream()
                .collect(Collectors.toMap(
                        eachFruit -> eachFruit, //KeyMapper
                        eachFruit -> eachFruit, //Value Mapper
                        (first, second) -> first, //Merge function
                        HashMap::new)); //Create a Hashmap

        collectedFruits.forEach((key, value) -> {
            System.out.println("(" + key + "," + value + ")");
        });

    }
}

Output

(orange,orange)
(banana,banana)
(apple,apple)
(pomo,pomo)
(mango,mango)

Convert a stream to a ConcurrentHashMap using Collectors.toMap()

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class StaticReference
{
    public static void main(String[] args)
    {

        List<String> fruits = Arrays.asList("apple", "mango", "pomo", 
                                            "banana", "orange", "apple");

        Map<String, String> collectedFruits = fruits
                .stream()
                .collect(Collectors.toMap(
                        eachFruit -> eachFruit, //KeyMapper
                        eachFruit -> eachFruit, //Value Mapper
                        (first, second) -> first, //Merge function
                        ConcurrentHashMap::new));

        collectedFruits.forEach((key, value) -> {
            System.out.println("(" + key + "," + value + ")");
        });

    }
}

Output

(banana,banana)
(orange,orange)
(apple,apple)
(pomo,pomo)
(mango,mango)

I hope you like this article. Thanks for reading and Please leave us a comment below!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

2 thoughts on “Convert a Stream to HashMap in Java 8

    • Author gravatar

      I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.

      • Author gravatar

        Thank you so much for your feedback and you have made my day! Please visit my LinkedIn we can connect over there!

        linkedin.com/in/rajasekar-sribalan-5b191b34

        Thank you buddy !Please keep following our site(www.staticreference.com) and spread a word about us via your social shares and references:)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.