通过一个属性组成对象的另一个属性组

发布于 2025-01-20 11:07:48 字数 579 浏览 0 评论 0 原文

谁能帮我。我有一个对象列表 - 就我而言,有啤酒厂。 每个酿酒厂都有一些属性(字段),例如名称,地址,ID,省(州位置)等...一个Brewerie(名称)可以位于许多省份。现在我需要解决问题:如何在每个状态下服用数量的啤酒厂?因此,按省份分组名称。所有数据都是从CSV文件读取的。我创建了读者,其中返回列表OD Breweries。 当我尝试一下时:

Map<String, List<Breweries>> hashMap = new HashMap<>();
    hashMap = list.stream().collect(Collectors.groupingBy(Breweries::getProvince));
    for (Map.Entry<String, List<BreweriesPOJO>> stringListEntry : hashMap.entrySet()) {
        System.out.println(stringListEntry.getKey());
    }

这将使我返回密钥(省)和整个对象作为值。

我坐在它上了几个小时。我没有想法。

can anyone help me. I have a list of Objects - in my case there are breweries.
Each brewerie has some attributes(fields) like name, adress, id, province(the state where is situated) etc... One brewerie (name) can be situated in many provinces. Now i need solve the problem: how to take number of breweries in each state? So, grouping Names by Provinces. All data is reading from csv file. I've created the Reader which return List od Breweries.
When i try this:

Map<String, List<Breweries>> hashMap = new HashMap<>();
    hashMap = list.stream().collect(Collectors.groupingBy(Breweries::getProvince));
    for (Map.Entry<String, List<BreweriesPOJO>> stringListEntry : hashMap.entrySet()) {
        System.out.println(stringListEntry.getKey());
    }

this returns me key(Province) and whole object as a value.

I've been sitting on it for a few hours. I'm out of ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-01-27 11:07:48

您的示例不正确,您有一个 HashMap> 但您希望 hashMap.entrySet() 返回 Set>。它应该是Set>。当您执行 Map.Entry#getKey 时,它将返回您的情况下的密钥,即 Breweries::getProvince 返回的 String 。如果您想要当前条目的 Breweries 列表,请使用 Map.Entry#getValue

Map<String, List<Breweries>> hashMap = list.stream().collect(Collectors.groupingBy(Breweries::getProvince));
for (Map.Entry<String, List<Breweries>> stringListEntry : hashMap.entrySet()) {
    List<Breweries> breweries = stringListEntry.getValue();
}

但如果您只想要每个省的啤酒厂数量,您可以直接执行此操作,如下所示:

Map<String, Long> hashMap = list.stream().collect(Collectors.groupingBy(Breweries::getProvince, Collectors.counting()));
for (Map.Entry<String, Long> entry : hashMap.entrySet()){
    System.out.println(entry.getKey() + ": " + entry.getValue() + " Breweries");
}

Your example is not correct you have a HashMap<String, List<Breweries>> but you expect hashMap.entrySet() to return Set<Map.Entry<String, List<BreweriesPOJO>>. It should be Set<Map.Entry<String, List<Breweries>>. when you do Map.Entry#getKey it will return the key in your case the String return by Breweries::getProvince. If you want the list of the Breweries for the current entry use Map.Entry#getValue

Map<String, List<Breweries>> hashMap = list.stream().collect(Collectors.groupingBy(Breweries::getProvince));
for (Map.Entry<String, List<Breweries>> stringListEntry : hashMap.entrySet()) {
    List<Breweries> breweries = stringListEntry.getValue();
}

But if you just want the number of Breweries per province you can do this directly like:

Map<String, Long> hashMap = list.stream().collect(Collectors.groupingBy(Breweries::getProvince, Collectors.counting()));
for (Map.Entry<String, Long> entry : hashMap.entrySet()){
    System.out.println(entry.getKey() + ": " + entry.getValue() + " Breweries");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文