Home > Enterprise >  how to impose a condition after sorting an array list based on an order
how to impose a condition after sorting an array list based on an order

Time:08-25

lets say I have a array list:

List<Class1> items = new ArrayList<>();
items.add(new Class1("name1", "2"));
.
.
.

then I used the compare method to return my list in an order sorted by the name field.

name1:2
name1:1
name1:2
name1:3
name1:3
name2:2
name2:3
name2:1
name3:1
name3:2
name3:3

now Im trying to return a list containing the name values who have 1,2,3 in order?

now I want the expected output to be like:

name1 has the items 1,2,3 in order so its a problem.
name3 has the item 1,2,3 in order so its a problem.

how can I achieve this?

First I did sort my array list in order to achieve the above sorted list:

any suggestions on how to achieve this?

CodePudding user response:

if you just want the list of names that have 1,2,3 sequence.

List<Integer> subList = Arrays.asList(1,2,3);

//groupBy names
Map<String, List<Class1>> groupedByName = class1List.stream()
                .collect(Collectors
                        .groupingBy(
                                Class1::getName,
                                LinkedHashMap::new, Collectors.toList()
));
//filter out the names that contains sublist in the values
List<String> result = groupedByName.entrySet().stream()
                .filter(entry -> 
Collections.indexOfSubList(entry.getValue().stream().map(Class1::getValue).collect(Collectors.toList()), gg) != -1)
                        .map(Map.Entry::getKey)
                .collect(Collectors.toList());

first, the list is grouped by name and added to a linkedHashMap which keeps the insertion order. then a list of names containing the subList in the value array is returned.

Collections.indexOfSubList returns the subArray if present or returns -1 when a sub-array cant be found. then the map is filtered with the only entries containing the sub-array. then the keys of the map are returned

CodePudding user response:

Assuming you have a class Class1 similar to this:

public class Class1 {
    String name;
    String item;

    //getter, constructor, toString ..
}

and a list of Class1 objects:

List<Class1> class1List = new ArrayList<>(List.of(
        new Class1("name1", "2"),
        new Class1("name1", "1"),
        new Class1("name1", "2"),
        new Class1("name1", "3"),
        new Class1("name1", "3"),
        new Class1("name2", "2"),
        new Class1("name2", "3"),
        new Class1("name2", "1"),
        new Class1("name3", "1"),
        new Class1("name3", "2"),
        new Class1("name3", "3")));

you could group by name and map to a list of item values and then use Collections.indexOfSubList to check if the value list has a sublist containing 1, 2, 3 in this order :

class1List.stream()
          .collect(Collectors.groupingBy(Class1::getName,
                                         Collectors.mapping(Class1::getItem, Collectors.toList())))
          .entrySet()
          .stream()
          .filter(e -> Collections.indexOfSubList(e.getValue(), List.of("1", "2", "3")) > -1)
          .map(Map.Entry::getKey)
          .sorted()
          .forEach(s -> {
              System.out.println(s   " has the items 1,2,3 in order so its a problem.");
          });
  • Related