简体   繁体   中英

Java 8 Stream API Filtering

I have a collection of objects, with the following type:

{
    String action_name; //add or delete
    long action_time;
    String action_target;
}

Need to get the latest merged operation on each action_target

Sample input data:

[add|1001|item1, add|1002|item2, delete|1003|item1, add|1004|item1]

Expected result:

[add|1002|item2, add|1004|item1]

Sample input data:

[add|1001|item1, add|1002|item2, delete|1003|item1]

Expected result:

[add|1002|item2]

Sample input data:

[delete|1001|item1, add|1002|item2, add|1003|item1]

Expected result:

[add|1002|item2, add|1003|item1]

Is this approachable using Java8 stream APIs? Thanks.

You want to group by one criteria (the action_target ) combined with reducing the groups to the maximum of their action_time values:

Map<String,Item> map=items.stream().collect(
    Collectors.groupingBy(item->item.action_target,
        Collectors.collectingAndThen(
            Collectors.maxBy(Comparator.comparing(item->item.action_time)),
            Optional::get)));

This returns a Map<String,Item> but, of course, you may call values() on it to get a collection of items.

Beautified with static import s, the code looks like:

Map<String,Item> map=items.stream().collect(groupingBy(item->item.action_target,
    collectingAndThen(maxBy(comparing(item->item.action_time)), Optional::get)));

Your additional request of taking care of idempotent "add" and follow-up "delete" actions can be simplified to “remove items whose last action is "delete" ” which can be implemented just by doing that after collecting using a mutable map:

HashMap<String,Item> map=items.stream().collect(groupingBy(
    item->item.action_target, HashMap::new,
    collectingAndThen(maxBy(comparing(item->item.action_time)), Optional::get)));
map.values().removeIf(item->item.action_name.equals("delete"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM