Hashmap with Streams in Java 8 Streams to collect value of Map

JavaHashmapJava 8Collectors

Java Problem Overview

Let consider a hashmap

Map<Integer, List> id1 = new HashMap<Integer,List>();

I inserted some values into both hashmap.

For Example,

List<String> list1 = new ArrayList<String>();


List<String> list2 = new ArrayList<String>();

List<String> list3 = new ArrayList<String>();


Q1) Now I want to apply a filter condition on the key in hashmap and retrieve the corresponding value(List).

Eg: Here My query is key=1, and output should be 'list1'

I wrote

id1.entrySet().stream().filter( e -> e.getKey() == 1);

But I don't know how to retrieve as a list as output of this stream operation.

Q2) Again I want to apply a filter condition on the key in hashmap and retrieve the corresponding list of lists.

Eg: Here My query is key=1%(i.e key can be 1,10,15), and output should be 'list1','list2','list3'(list of lists).

Java Solutions

Solution 1 - Java

If you are sure you are going to get at most a single element that passed the filter (which is guaranteed by your filter), you can use findFirst :

Optional<List> o = id1.entrySet()
                      .filter( e -> e.getKey() == 1)

In the general case, if the filter may match multiple Lists, you can collect them to a List of Lists :

List<List> list = id1.entrySet()
                     .filter(.. some predicate...)

Solution 2 - Java

What you need to do is create a Stream out of the Map's .entrySet():

// Map<K, V> --> Set<Map.Entry<K, V>> --> Stream<Map.Entry<K, V>>

From the on, you can .filter() over these entries. For instance:

// Stream<Map.Entry<K, V>> --> Stream<Map.Entry<K, V>>
.filter(entry -> entry.getKey() == 1)

And to obtain the values from it you .map():

// Stream<Map.Entry<K, V>> --> Stream<V>

Finally, you need to collect into a List:

// Stream<V> --> List<V>

If you have only one entry, use this instead (NOTE: this code assumes that there is a value; otherwise, use .orElse(); see the javadoc of Optional for more details):

// Stream<V> --> Optional<V> --> V

Solution 3 - Java

For your Q2, there are already answers to your question. For your Q1, and more generally when you know that the key's filtering should give a unique value, there's no need to use Streams at all.

Just use get or getOrDefault, i.e:

List<String> list1 = id1.getOrDefault(1, Collections.emptyList());

Solution 4 - Java

You can also do it like this

public Map<Boolean, List<Student>> getpartitionMap(List<Student> studentsList) {
	List<Predicate<Student>> allPredicates = getAllPredicates();
    Predicate<Student> compositePredicate =  allPredicates.stream()
                             .reduce(w -> true, Predicate::and)
     Map<Boolean, List<Student>> studentsMap= studentsList
    return studentsMap;

public List<Student> getValidStudentsList(Map<Boolean, List<Student>> studentsMap) throws Exception {
	List<Student> validStudentsList =  studentsMap.entrySet()
             .filter(p -> p.getKey() == Boolean.TRUE)
             .flatMap(p -> p.getValue().stream())
	return validStudentsList;

public List<Student> getInValidStudentsList(Map<Boolean, List<Student>> studentsMap) throws Exception {
	List<Student> invalidStudentsList = 
             .filter(p -> p.getKey() == Boolean.FALSE)
             .flatMap(p -> p.getValue().stream())
	return invalidStudentsList;

With flatMap you will get just List<Student> instead of List<List<Student>>.


Solution 5 - Java

Using keySet-

		.filter(x -> x == 1)
		.map(x -> id1.get(x))

Solution 6 - Java

Maybe the sample is oversimplified, but you don't need the Java stream API here. Just use the Map directly.

 List<String> list1 = id1.get(1); // this will return the list from your map

Solution 7 - Java

why all that !!!

> List list1 = id1.get(1);


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepak ShajanView Question on Stackoverflow
Solution 1 - JavaEranView Answer on Stackoverflow
Solution 2 - JavafgeView Answer on Stackoverflow
Solution 3 - Javauser2336315View Answer on Stackoverflow
Solution 4 - JavaBasitView Answer on Stackoverflow
Solution 5 - JavaShreya MourView Answer on Stackoverflow
Solution 6 - Javaglory1View Answer on Stackoverflow
Solution 7 - JavaYHRView Answer on Stackoverflow