[计数> 100 Java流时的分组依据

问题描述 投票:1回答:1

我有以下课程

private class MaintWindowClass {
    String SiteID;
    String Node;

    public String getSiteID() {
        return SiteID;
    }

    public String getNode() {
        return Node;
    }
}

一个节点可以有多个站点。我正在尝试获取站点计数> 100的节点。

是否可以通过流获取。到目前为止,我已经尝试过这些]

ArrayList<MaintWindowClass> maintWindowArray = new ArrayList<MaintWindowClass>(); // then add some MaintWindowClass in it. 
Map<String, Long> sitesGroupedCount = maintWindowArray.stream()
    .collect(Collectors.groupingBy(MaintWindowClass::getNode, 
        Collectors.counting())); // More appropriate this breaks sites in to Nodes and the site count in it.

我现在可以迭代此Map并获得站点计数> 100的那些节点,但是是否可以使用流仅获得站点计数大于100的那些节点?

java stream
1个回答
0
投票

您几乎拥有它!现在,您需要遍历地图并根据值过滤条目:

    List<String> nodesWithMoreThan100Sites = maintWindowArray.stream()
            .collect(Collectors.groupingBy(MaintWindowClass::getNode, Collectors.counting()))
            .entrySet()
            .stream()
            .filter(e->e.getValue() > 100)
            .map(e->e.getKey())
            .collect(Collectors.toList());
© www.soinside.com 2019 - 2024. All rights reserved.