Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的Groupby计数_Java_Collections_Java 8 - Fatal编程技术网

java中的Groupby计数

java中的Groupby计数,java,collections,java-8,Java,Collections,Java 8,我对java从c#转向c#相当陌生。我有以下课程 class Resource { String name; String category; String component; String group; } HashMap<String, HashSet<String>> map = new HashMap<>(); for (Resource resource : resources) {

我对java从c#转向c#相当陌生。我有以下课程

class Resource {
    String name;
    String category;
    String component;
    String group;
}
    HashMap<String, HashSet<String>> map = new HashMap<>();
for (Resource resource : resources) {
            if (map.containsKey(resource.getCategory())) {
                map.get(resource.getCategory()).add(resource.getGroup());
            } else 
                HashSet<String> componentSet = new HashSet<>();
                componentSet.add(resource.getGroup());
                map.put(resource.getCategory(), componentSet);
            }
        }

        log.info("Group count in each category");
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) {
            log.info("{} - {}", entry.getKey(), entry.getValue().size());
        }
我想知道以下数字: 1.类别中的资源计数。 2.每个类别中组件的不同计数。(组件名称可以重复) 3.按类别和组分组的资源计数

    HashMap<String, HashSet<String>> map = new HashMap<>();
for (Resource resource : resources) {
            if (map.containsKey(resource.getCategory())) {
                map.get(resource.getCategory()).add(resource.getGroup());
            } else 
                HashSet<String> componentSet = new HashSet<>();
                componentSet.add(resource.getGroup());
                map.put(resource.getCategory(), componentSet);
            }
        }

        log.info("Group count in each category");
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) {
            log.info("{} - {}", entry.getKey(), entry.getValue().size());
        }
我使用
Collectors.groupingBy
取得了一些成功。然而,结果总是这样

Map<String, List<Resource>>
    HashMap<String, HashSet<String>> map = new HashMap<>();
for (Resource resource : resources) {
            if (map.containsKey(resource.getCategory())) {
                map.get(resource.getCategory()).add(resource.getGroup());
            } else 
                HashSet<String> componentSet = new HashSet<>();
                componentSet.add(resource.getGroup());
                map.put(resource.getCategory(), componentSet);
            }
        }

        log.info("Group count in each category");
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) {
            log.info("{} - {}", entry.getKey(), entry.getValue().size());
        }
Map
要获得计数,我必须解析键集并计算大小。 使用c#linq,我可以轻松计算上述所有指标。 我假设在java中也有更好的方法来实现这一点。请告知。

对于#1,我将与以下产品一起使用:

    HashMap<String, HashSet<String>> map = new HashMap<>();
for (Resource resource : resources) {
            if (map.containsKey(resource.getCategory())) {
                map.get(resource.getCategory()).add(resource.getGroup());
            } else 
                HashSet<String> componentSet = new HashSet<>();
                componentSet.add(resource.getGroup());
                map.put(resource.getCategory(), componentSet);
            }
        }

        log.info("Group count in each category");
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) {
            log.info("{} - {}", entry.getKey(), entry.getValue().size());
        }
这首先创建一个
LinkedHashMap
(保留插入顺序)。然后,
Resource
元素被迭代并放入这个映射中,这样它们就按照
category
进行分组,并且每个
group
被添加到映射到每个类别的
HashSet
。由于集合不允许重复,因此任何类别都不会有重复的组。然后,组的不同计数是每个集合的大小

    HashMap<String, HashSet<String>> map = new HashMap<>();
for (Resource resource : resources) {
            if (map.containsKey(resource.getCategory())) {
                map.get(resource.getCategory()).add(resource.getGroup());
            } else 
                HashSet<String> componentSet = new HashSet<>();
                componentSet.add(resource.getGroup());
                map.put(resource.getCategory(), componentSet);
            }
        }

        log.info("Group count in each category");
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) {
            log.info("{} - {}", entry.getKey(), entry.getValue().size());
        }
对于#3,我会再次使用
收集器.groupingBy
收集器.counting
,但我会使用复合键按以下方式分组:

Map<List<String>, Long> resourcesByCategoryAndGroup = resources.stream()
        .collect(Collectors.groupingBy(
                 r -> Arrays.asList(r.getCategory(), r.getGroup()), // or List.of
                 Collectors.counting()));
    HashMap<String, HashSet<String>> map = new HashMap<>();
for (Resource resource : resources) {
            if (map.containsKey(resource.getCategory())) {
                map.get(resource.getCategory()).add(resource.getGroup());
            } else 
                HashSet<String> componentSet = new HashSet<>();
                componentSet.add(resource.getGroup());
                map.put(resource.getCategory(), componentSet);
            }
        }

        log.info("Group count in each category");
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) {
            log.info("{} - {}", entry.getKey(), entry.getValue().size());
        }

感谢Fedrico的详细回复#1和#3非常有效。对于#2,我希望看到Map的输出。下面是我目前用来获取该计数的代码。这是在不使用旧式收集器的情况下实现的

    HashMap<String, HashSet<String>> map = new HashMap<>();
for (Resource resource : resources) {
            if (map.containsKey(resource.getCategory())) {
                map.get(resource.getCategory()).add(resource.getGroup());
            } else 
                HashSet<String> componentSet = new HashSet<>();
                componentSet.add(resource.getGroup());
                map.put(resource.getCategory(), componentSet);
            }
        }

        log.info("Group count in each category");
        for (Map.Entry<String, HashSet<String>> entry : map.entrySet()) {
            log.info("{} - {}", entry.getKey(), entry.getValue().size());
        }
HashMap map=newhashmap();
for(资源:资源){
if(map.containsKey(resource.getCategory())){
map.get(resource.getCategory()).add(resource.getGroup());
}否则
HashSet componentSet=新HashSet();
add(resource.getGroup());
map.put(resource.getCategory(),componentSet);
}
}
log.info(“每个类别中的组计数”);
对于(Map.Entry:Map.entrySet()){
log.info(“{}-{}”,entry.getKey(),entry.getValue().size());
}

对于#1,您是说给定一个特定的类别,有多少
资源
对象具有该特定类别?您应该使用另一个过载的groupingBy,例如
groupingBy(Resource::getCategory,Collectors.counting())
查看其他帖子:@ChrisGong-Correct@Anan
groupingBy
可以在
组件上再次接受下游
收集器
到groupBy,然后对它们进行计数-
Collectors.groupingBy(Resource::getCategory,Collectors.groupingBy(Resource::getComponent,Collectors.counting())
。旁白:只是想知道,使用C#你能在一个语句中实现这三个吗?这也是这里的期望吗?看看您到目前为止在java中尝试了什么,这个问题会变得非常清楚。我只是在等待你关于#2:)的回复。再次感谢费德里科。我希望有一个好方法将两步操作合并为一步。哦,好吧@Anan使用流表示#2的一行程序比这两行解决方案更长,可读性更低。我将删除此答案,因为它可能会收到其他用户的反对票。我想把这个加到原来的问题上