0

I have an ArrayList of String Array and the 2nd element of every String Array contains a name. I want to create a Map where it's Key is the name and Value is an ArrayList of all String Array which has the name.

How can I achive this in Java?

Input: ArrayList<String[]>

{"1", "NameABC", "somestring"}
{"2", "NameDEF", "somestring"}
{"3", "NameDEF", "somestring"}
{"4", "NameABC", "somestring"}
{"5", "NameXYZ", "somestring"}

Output: Map<String, ArrayList<String[]>

Key = NameABC, Value = ArrayList of String[] where every String[] has NameABC

Key = NameXYZ, Value = ArrayList of String[] where every String[] has NameXYZ

I've tried using stream.collect(Collectors.toMap()) but I can't figure out how to achieve the proper output.

3
  • Why did you try stream.collect(Collectors.toMap())? What was wrong with your attempt? Commented Jan 31, 2023 at 17:22
  • I cannot figure out a way to map the 2nd element of the String[] Commented Jan 31, 2023 at 17:59
  • By the way, in real work it would likely make more sense to define a record to hold the elements of each array as fields in an object. A record can be defined locally, within a method. Commented Jan 31, 2023 at 20:23

3 Answers 3

1

Streams and collectors are not always the best option:

Map<String, List<String[]>> map = new HashMap<>();
myListOfArrays.forEach(arr -> map.computeIfAbsent(arr[1], x -> new ArrayList<>()).add(arr));

If the key can be null and you want to store it under a blank, use the expression arr[1] == null ? "" : arr[1] instead:

myListOfArrays.forEach(arr -> map.computeIfAbsent(arr[1] == null ? "" : arr[1], x -> new ArrayList<>()).add(arr));
Sign up to request clarification or add additional context in comments.

1 Comment

Streams are not always the best option, but a stream with Collectors.groupingBy() seems perfectly appropriate here.
1

Try this -

List<String[]> l1 = new ArrayList<>();
    l1.add(new String[]{"1", "NameABC", "somestring"});
    l1.add(new String[]{"2", "NameDEF", "somestring"});
    l1.add(new String[]{"3", "NameDEF", "somestring"});
    l1.add(new String[]{"4", "NameABC", "somestring"});
    l1.add(new String[]{"5", "NameXYZ", "somestring"});
    
    Map<String, List<String[]>> map = l1.stream().collect(Collectors.groupingBy(arr -> arr[1]));
    
    for (Map.Entry<String, List<String[]>> entry : map.entrySet()) {
        String key = entry.getKey();
        List<String[]> val = entry.getValue();
        
        System.out.println(key + " -- "+ val);
    }

3 Comments

Thank you. Your solution worked. I got an NullPointerException when Key was null but that won't be an issue if I replace null values with empty String.
What's the point of new HashMap<>()? Just assign the stream result straight to your variable. Also, you don't need to work so hard to print a map. Just do System.out.println(map);. Otherwise, good answer.
Or, to get your specific output format, use map.forEach((key, val) -> System.out.println(key + " -- "+ val));
0

You can use Stream with groupingBy():

public static Map<String, List<String[]>> convert(List<String[]> data) {
    return data.stream().collect(Collectors.groupingBy(arr -> arr[1]));
}

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.