Home > other >  Java Stream filter by two values using collectors
Java Stream filter by two values using collectors

Time:03-17

I have the below working code using one value to compare but want to compare with two values. I need to add one more filter as "getAddressID" along with getAction. Could some help me to modify the below code?

List<Data> finalData = dataList.stream().collect(
    Collectors.collectingAndThen(
        Collectors.toCollection(
            () -> new TreeSet<>(Comparator.comparing(Data::getAction))),
            ArrayList::new))
public class TestCollection {

/**
 * @param args
 */
public static void main(String[] args) {
    
    List<Data> dataList = new ArrayList<Data>();
    
    Data data1 = new Data("DELIVERED", "1112");
    Data data2 = new Data("DELIVERED", "1113");
    Data data3 = new Data("PICKEUP", "1112");
    Data data4 = new Data("PICKEUP", "1112");
    
    dataList.add(data1);
    dataList.add(data2);
    dataList.add(data3);
    dataList.add(data4);
    
    //Filer by Action
    List<Data> finalData = dataList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<> (Comparator.comparing(Data::getAction))), ArrayList::new));
    
    for(Data data : finalData) {
        System.out.println(" Action "   data.getAction()   " Address ID "   data.getAddressID());
    }
    
    

}

}

The result is : "Action DELIVERED Address ID 1112" "Action PICKEUP Address ID 1112"

But I want result as: "DELIVERED Address ID 1112" "DELIVERED Address ID 1113" "PICKEUP Address ID 1112"

CodePudding user response:

You can chain your comparators using thenComparing, ie

new TreeSet<>(Comparator.comparing(Data::getAction).thenComparing(Comparator.comparing(Data::otherMethod))

CodePudding user response:

If I get it right, you are trying to get distinct items from your list by the combination of the two properties addressID & action. Here is a an accepted answer, thanks to @Stuart Marks, which works to get distinct elements by one property. That could be further modified to solve your use case like:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;


....

List<Data> dataList = // your data objects

List<Data> distinctData = dataList.stream()
            .filter(distinctByKeys(Data::getAction, Data::getAddressID))
            .collect(Collectors.toList());

where the method distinctByKeys could look like

private static <T> Predicate<T> distinctByKeys(final Function<? super T, ?>... keyExtractors) {
    final Map<List<?>, Boolean> seen = new ConcurrentHashMap<>();
    return t -> {
        final List<?> keys = Arrays.stream(keyExtractors)
                .map(ke -> ke.apply(t))
                .collect(Collectors.toList());
        return seen.putIfAbsent(keys, Boolean.TRUE) == null;
    };
}
  • Related