如何以属性为键创建映射,将最大值作为值创建

发布于 2025-01-31 05:28:17 字数 1553 浏览 5 评论 0原文

在大学里,我们正在使用CSV文件和流。现在我正在做一些功课,但我已经在这项练习中停留了几天。

我有一个CSV文件,上面有一些有关事故的数据(例如事故严重性,受害者的数量等),并且我的功能可以读取数据并将其转换为事故>事故>对象的列表(它具有CSV具有的每种类型数据的属性):

public class Accident {
    private Severity severity;
    private Integer victims;
    
    public Accident(Severity severity, Integer victims) {
        this.severity = severity;
        this.victims = victims;
    }
}

我将该列表保存在对象中,我可以使用getAccidents()

private List<Accident> accidents;

public AccidentArchive(List<Accident> accidents) {
    this.accidents = accidents;
}

public Map<Severity, Integer> getMaxVictimsPerSeverity() {
    //Not working correctly
    return getAccidents().stream().collect(Collectors.toMap(Accident::getSeverity, Accident::getVictims, Integer::max));
}

现在,我必须制作一个函数创建具有属性作为密钥的地图,作为值的最大值。我以为地图键可能是严重性(这是一个具有值轻微的枚举每次事故中最多的受害者数量最多,具有相同的严重性。

例如,地图输出可能是:little = 1,严重= 3,致命= 5

我尝试使用collectionscollector比较器库,但我找不到任何方法来除以所有事故 s,获取每个受害者的最大数量,然后在map&lt;严重性,整数&gt;中保存两个值。

现在的功能现在就开始了:

public Map<Severity, Integer> getMaxVictimsPerSeverity() {
    return getAccidents().stream().collect(Collectors.toMap(Accident::getSeverity, ...))

我尝试了很多事情,但是找不到任何方法来返回我想要的东西。我该怎么做?

In college we are working with CSV files and streams. Right now I am doing some homework but I've been stuck in this exercise for some days now.

I have a CSV file with some data about accidents (like the accident severity, the number of victims and so on) and I have a function that reads that data and converts it to a list of Accident objects (which has a property for each type of data the CSV has):

public class Accident {
    private Severity severity;
    private Integer victims;
    
    public Accident(Severity severity, Integer victims) {
        this.severity = severity;
        this.victims = victims;
    }
}

I have saved that list in an object and I can call the list with getAccidents():

private List<Accident> accidents;

public AccidentArchive(List<Accident> accidents) {
    this.accidents = accidents;
}

public Map<Severity, Integer> getMaxVictimsPerSeverity() {
    //Not working correctly
    return getAccidents().stream().collect(Collectors.toMap(Accident::getSeverity, Accident::getVictims, Integer::max));
}

Right now, I have to make a function that creates a map with an attribute as a key and a maximum value as a value. I have thought that the map keys could be the severity (which is an enum with values SLIGHT, SERIOUS and FATAL) and the values could be the highest number of victims of every accident with that same severity.

For example, the map output could be: SLIGHT=1, SERIOUS=3, FATAL=5.

I have tried using the Collections, Collectors and Comparator libraries but I can't find any way to divide all the Accidents in each severity value, get the maximum number of victims for each and then save both values in a Map<Severity, Integer>.

The function right now starts this way:

public Map<Severity, Integer> getMaxVictimsPerSeverity() {
    return getAccidents().stream().collect(Collectors.toMap(Accident::getSeverity, ...))

I have tried a lot of things but I can't find any way to make it return what I want. How can I do this?

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

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

发布评论

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

评论(3

我纯我任性 2025-02-07 05:28:17

提供一个价值提取器,以吸引每次事故的受害者,以及一个减速者,以保持最高数量的受害者。

return getAccidents().stream()
    .collect(Collectors.toMap(Accident::getSeverity, Accident::getVictims, Integer::max));

Provide a value extractor to get the victims of each accident, and a reducer to keep the highest number of victims.

return getAccidents().stream()
    .collect(Collectors.toMap(Accident::getSeverity, Accident::getVictims, Integer::max));
海之角 2025-02-07 05:28:17

您正在尝试按严重性进行分组,并计算每个组中的事故数量。

Map<Severity, Long> counted = list.stream()
            .collect(Collectors.groupingBy(Accident::getSeverity, Collectors.counting()));

这应该可以帮助您实现所需的目标。

这是一个工作示例:

import java.util.*;
import java.util.stream.*;

public class Test {
    
    public static void main(String args[]) {
      Accident a1 = new Accident(Severity.SLIGHT);
      Accident a2 = new Accident(Severity.SERIOUS);
      Accident a3 = new Accident(Severity.SLIGHT);
      Accident a4 = new Accident(Severity.FATAL);
      Accident a5 = new Accident(Severity.FATAL);
      
      List<Accident> list= new ArrayList<>();
      list.add(a1);
      list.add(a2);
      list.add(a3);
      list.add(a4);
      list.add(a5);
      
      Map<Severity, Long> counted = list.stream()
            .collect(Collectors.groupingBy(Accident::getSeverity, Collectors.counting()));

      System.out.println(counted);
    }
    
    static class Accident{
        Severity severtity;
        Accident(Severity s){
            this.severtity = s;
        }

        public Severity getSeverity(){
            return severtity;
        }
    }

    static enum Severity{
        SLIGHT, SERIOUS, FATAL;
    }
}

结果:{fatal = 2,little = 2,严重= 1}

You are trying to group by Severity and count the number of Accidents in each group.

Map<Severity, Long> counted = list.stream()
            .collect(Collectors.groupingBy(Accident::getSeverity, Collectors.counting()));

This should help you achieve what you are looking for.

Here is a working example:

import java.util.*;
import java.util.stream.*;

public class Test {
    
    public static void main(String args[]) {
      Accident a1 = new Accident(Severity.SLIGHT);
      Accident a2 = new Accident(Severity.SERIOUS);
      Accident a3 = new Accident(Severity.SLIGHT);
      Accident a4 = new Accident(Severity.FATAL);
      Accident a5 = new Accident(Severity.FATAL);
      
      List<Accident> list= new ArrayList<>();
      list.add(a1);
      list.add(a2);
      list.add(a3);
      list.add(a4);
      list.add(a5);
      
      Map<Severity, Long> counted = list.stream()
            .collect(Collectors.groupingBy(Accident::getSeverity, Collectors.counting()));

      System.out.println(counted);
    }
    
    static class Accident{
        Severity severtity;
        Accident(Severity s){
            this.severtity = s;
        }

        public Severity getSeverity(){
            return severtity;
        }
    }

    static enum Severity{
        SLIGHT, SERIOUS, FATAL;
    }
}

The Result: {FATAL=2, SLIGHT=2, SERIOUS=1}

不疑不惑不回忆 2025-02-07 05:28:17

一种解决方案是使用collector对返回最大受害者数量的每个组进行分组。

return getAccidents().stream()
        .collect(Collectors.groupingBy(Accident::getSeverity,
                Collectors.mapping(Accident::getVictims,
                        Collectors.collectingAndThen(
                                Collectors.maxBy(Comparator.<Integer>naturalOrder()),
                                Optional::orElseThrow))));

One solution is to group by severity, using a Collector for each group that returns the maximum number of victims.

return getAccidents().stream()
        .collect(Collectors.groupingBy(Accident::getSeverity,
                Collectors.mapping(Accident::getVictims,
                        Collectors.collectingAndThen(
                                Collectors.maxBy(Comparator.<Integer>naturalOrder()),
                                Optional::orElseThrow))));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文