1

I have a List of String[6]s. I'm trying to build a List<List> that contains the unique String elements at each index.

For some reason, I frequently run into this problem, search to see if answers exist, experiment a lot, and end up building a working method that does not use stream, but takes up like 20 lines.

To clarify, with an example:

//Sample items: String[] items = "AD", "AR", "BC", "DA", "RA", "DD";
//              String[] items2 = "AE", "AZ", "BU", "DI", "RE", "DP";
//              String[] items3 = "AD", "AO", "BU", "DZ", "RW", "DP";
List<String[]> itemsList;
List<String>[] distinctItems;

If itemsList contains the sample Items, how can .stream() put them in distinctItems? distinctItems should look like:

//distinctItems[0].get(0) == "AD"
//distinctItems[0].get(1) == "AE"
//distinctItems[0].size() == 2         ; Has 2 unique elements in index 0

//distinctItems[1].get(0) == "AR"
//distinctItems[1].get(1) == "AZ"
//distinctItems[1].get(2) == "AO"
//distinctItems[1].size() == 3         ; Has 3 unique elements in index 1

I'm getting pretty good at .stream() but on certain compound data types (List of Array[]), I can't seem to find the right methods and solutions.

Any help greatly appreciated.

2
  • 2
    Unclear! A List<String[]> is basically a two-dimensional data structure. A List<List>[] is a three-dimensional data structure. I am not getting how you want to transform your source list. Besides that, where are your code attempts? Commented Apr 3, 2019 at 18:41
  • Ah, I think I copied that wrong: List<String>[]. I have working solutions without stream but I think stream() would be more concise. I really have been working on a project using this throughout the night so I'm exhausted. If you think this question is invalid, I'll just take it down. Attempts using .stream() include .foreach(), .distinct(), but I am not sure how to apply .distinct() to the String[] within the original List. Commented Apr 3, 2019 at 18:49

1 Answer 1

1

If I understand correctly, you actually want a List<List<String>>, which has the same size as your 3 List<String[]>, and which contain, for each index; the distinct elements for this index.

So

    List<String[]> itemLists = new ArrayList<>();
    itemLists.add(new String[] { "AD", "AR", "BC", "DA", "RA", "DD" });
    itemLists.add(new String[] { "AE", "AZ", "BU", "DI", "RE", "DP" });
    itemLists.add(new String[] { "AD", "AO", "BU", "DZ", "RW", "DP" });

    List<List<String>> distinctItems =
        IntStream.range(0, itemLists.get(0).length)
                 .mapToObj(i -> itemLists.stream().map(itemArray -> itemArray[i]).distinct().collect(toList()))
                 .collect(toList());

    System.out.println("distinctItems = " + distinctItems);
Sign up to request clarification or add additional context in comments.

1 Comment

I think so, yes, a List<List<String>> as you describe would be just fine. A lot in here, going to study each of these .mapToObj(), .range() methods and stream()ing within a stream in more detail soon. This is opening up some very exciting doors! I am getting this though, "Cannot infer type argument(s) for <U> mapToObj(intFunction<? extends U>). Maybe I need an extra parentheses? Quick answer here, you seem very expert with stream()! Will accept once I make sure I understand this.

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.