0

I'm trying to convert Map> to a List/Set of Strings in single line but couldn't do it unless I use traditional forEach for map. Is anyone aware to do it in single line using streams.

import java.util.*;

public class StreamDemo {
    public static void main(String[] args) {
        HashMap<String, List<String>> map = new HashMap<>();
        ArrayList<String> s1 = new ArrayList<>();
        map.put("A", Arrays.asList("A1, A2"));
        map.put("B", Arrays.asList("B1, B2"));
        map.put("C", Arrays.asList("C1, C2"));

        Set<String> stringValues = new HashSet<>();

        // LOGIC TO GET ALL VALUES A1, A2, B1, B2, C1, C2 INTO stringValues

        System.out.println(stringValues);
        // Expected output
        // [A1, A2, B1, B2, C1, C2]

    }
}
0

4 Answers 4

6

you could use the following stream which basically streams over your values and collect their items inside of a HashSet but note that the HashSet doesn't guarantee sorting. For that you'd want to use TreeSet::new in the end

Set<String> stringValues = map.values()
   .stream()
   .flatMap(List::stream)
   .collect(Collectors.toCollection(HashSet::new));
Sign up to request clarification or add additional context in comments.

3 Comments

Collectors.toSet() is more succinct.
Instead of Collectors.toCollection(HashSet::new) you could use Collectors.toSet().
@ArvindKumarAvinash Depends on whether you want to enforce the implementation. toSet() doesn't guarantee anything
4

A solution without using streams can be even simpler:

map.values().forEach(l -> l.forEach(stringValues::add));

Comments

1

Try the below code:

HashMap<String, List<String>> map = new HashMap<>();
        ArrayList<String> s1 = new ArrayList<>();
        map.put("A", Arrays.asList("A1, A2"));
        map.put("B", Arrays.asList("B1, B2"));
        map.put("C", Arrays.asList("C1, C2"));
        HashSet<String> strings = new LinkedHashSet<>();
        map.values().stream().sequential().forEach(value -> {
            value.forEach(value1 -> {
                strings.add(value1);
            });
        });
        System.out.println(strings);

Comments

1

Since your expected output is [A1, A2, B1, B2, C1, C2], use LinkedHashSet instead of a HashSet.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        HashMap<String, List<String>> map = new HashMap<>();
        ArrayList<String> s1 = new ArrayList<>();
        map.put("A", Arrays.asList("A1, A2"));
        map.put("B", Arrays.asList("B1, B2"));
        map.put("C", Arrays.asList("C1, C2"));
        Set<String> stringValues = new LinkedHashSet<>();
        map.values().forEach(list -> list.forEach(stringValues::add));
        System.out.println(stringValues);
    }
}

Output:

[A1, A2, B1, B2, C1, C2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.