按对象值分组,计数然后按最大对象属性设置组密钥

问题描述

这是一种方法。首先将其分组为列表,然后将列表处理为实际所需的值:

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap;


Map<Route,Integer> routeCounts = routes.stream()
        .collect(groupingBy(x -> x))
        .values().stream()
        .collect(toMap(
            lst -> lst.stream().max(comparingLong(Route::getLastUpdated)).get(),
            List::size
        ));

解决方法

我设法使用Java 8 Streams
API编写了一个解决方案,该解决方案首先按其值对对象Route列表进行分组,然后对每个组中的对象数进行计数。它返回一个映射Route->
Long。这是代码:

Map<Route,Long> routesCounted = routes.stream()
                .collect(Collectors.groupingBy(gr -> gr,Collectors.counting()));

和Route类:

public class Route implements Comparable<Route> {
    private long lastUpdated;
    private Cell startCell;
    private Cell endCell;
    private int dropOffSize;

    public Route(Cell startCell,Cell endCell,long lastUpdated) {
        this.startCell = startCell;
        this.endCell = endCell;
        this.lastUpdated = lastUpdated;
    }

    public long getLastUpdated() {
        return this.lastUpdated;
    }

    public void setLastUpdated(long lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public Cell getStartCell() {
        return startCell;
    }

    public void setStartCell(Cell startCell) {
        this.startCell = startCell;
    }

    public Cell getEndCell() {
        return endCell;
    }

    public void setEndCell(Cell endCell) {
        this.endCell = endCell;
    }

    public int getDropOffSize() {
        return this.dropOffSize;
    }

    public void setDropOffSize(int dropOffSize) {
        this.dropOffSize = dropOffSize;
    }

    @Override
    /**
     * Compute hash code by using Apache Commons Lang HashCodeBuilder.
     */
    public int hashCode() {
        return new HashCodeBuilder(43,59)
                .append(this.startCell)
                .append(this.endCell)
                .toHashCode();
    }

    @Override
    /**
     * Compute equals by using Apache Commons Lang EqualsBuilder.
     */
    public boolean equals(Object obj) {
        if (!(obj instanceof Route))
            return false;
        if (obj == this)
            return true;

        Route route = (Route) obj;
        return new EqualsBuilder()
                .append(this.startCell,route.startCell)
                .append(this.endCell,route.endCell)
                .isEquals();
    }

    @Override
    public int compareTo(Route route) {
        if (this.dropOffSize < route.dropOffSize)
            return -1;
        else if (this.dropOffSize > route.dropOffSize)
            return 1;
        else {
                // if contains drop off timestamps,order by last timestamp in drop off
                // the highest timestamp has preceding
            if (this.lastUpdated < route.lastUpdated)
                return -1;
            else if (this.lastUpdated > route.lastUpdated)
                return 1;
            else
                return 0;
        }
    }
}

我还想实现的是,每个组的密钥都是lastUpdated值最大的密钥。我已经在研究此解决方案,但是我不知道如何组合计数和按值分组以及路由最大lastUpdated值。这是我要实现的示例数据:

例:

List<Route> routes = new ArrayList<>();
routes.add(new Route(new Cell(1,2),new Cell(2,1),1200L));
routes.add(new Route(new Cell(3,5),1800L));
routes.add(new Route(new Cell(1,1700L));

应该转换为:

Map<Route,Long> routesCounted = new HashMap<>();
routesCounted.put(new Route(new Cell(1,1700L),2);
routesCounted.put(new Route(new Cell(3,1800L),1);

请注意,映射的键(包含2条路由)是 具有最后更新值最大的键