0

How I can group all lists in list which contains same exact value.

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

This list contains list1 list2 list3 and etc.... I want to find if list1.get(0) is the same as list2.get(0) if no check with list3.get(0) and etc... Later I need to group lists if I find the same values in each. For example if true add list1 and list3 and others which were found to

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

Now check with list2.get(0) if equal to others if yes group them and add them to anoter:

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

What I achieved so far:

    private static void findEqualLists(List<List<String>> list) {

    int i = 0;
    for (List<String> list1 : list) {
        if (!Collections.disjoint(list1, list.get(i))) {
            System.out.println(list1.get(0)+list.get(i).get(0));
        }else{
            System.out.println(list1.get(0)+list.get(i).get(0));
        }
        i++;
    }
}

Value exmaple:

list = {{2017-02,jhon,car},{2017-03,maria,car},{2017-03, boy, car}, {2017-01,arya, car}, {2017-02, girl, car}}

Output:

Group1:
2017-03,maria,car
2017-03, boy, car

Group2:
2017-02,jhon,car
2017-02, girl, car

Group3
2017-01,arya, car
4
  • 2
    1. what's the question? 2. what does it mean to "group" lists ? 3. instead of talking about how you're doing something, it's better to discuss what is it you want to achieve. Commented Oct 25, 2017 at 16:49
  • I want to achieve outpoot by giving value Commented Oct 25, 2017 at 16:50
  • 2
    "similar" and "grouping" are not well defined: are you "grouping" by date only? and again - what does "grouping" mean ? Commented Oct 25, 2017 at 16:52
  • "I want to achieve outpoot by giving value" -- Could you translate that, please? :-) Commented Oct 25, 2017 at 16:59

1 Answer 1

3

You want Collectors.groupingBy:

private static Map<String, List<List<String>>>
            findEqualLists(List<List<String>> list) {

    return list.stream().collect(Collectors.groupingBy(l -> l.get(0)));
}

The returned Map will use l.get(0) as a unique key, with each corresponding value being a List of only those Lists whose first element matches that key.

Sign up to request clarification or add additional context in comments.

6 Comments

An how I am able to Output returned Map like I showed above?
@D1's Call the Map’s values() method. You can either print the returned Collection directly, or, if you want curly braces ({ }) instead of brackets ([ ]), you can loop through the Collection and build a String yourself.
yes I understand that I can use values() method, but how I can divide into groups and print it seperatly? I mean that one key now has multiply values, do I need to iterate through every key and then to iterate through that key list to get values for that certain key?
@D1's One key has multiple values, because there are multiple entries in the original List which start with, for example, "2017-03". You don’t need to iterate through keys; you can simply iterate through values(), which is already divided into groups. The whole point of a Map is that each member of values() corresponds to exactly one key.
Yes it will be divided, but I don't get it how you gonna know when that group ended and another started to form?
|

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.