如何以属性为键创建映射,将最大值作为值创建
在大学里,我们正在使用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
。
我尝试使用collections
,collector
和比较器
库,但我找不到任何方法来除以所有事故 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 Accident
s 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提供一个价值提取器,以吸引每次事故的受害者,以及一个减速者,以保持最高数量的受害者。
Provide a value extractor to get the victims of each accident, and a reducer to keep the highest number of victims.
您正在尝试按严重性进行分组,并计算每个组中的事故数量。
这应该可以帮助您实现所需的目标。
这是一个工作示例:
结果:
{fatal = 2,little = 2,严重= 1}
You are trying to group by Severity and count the number of Accidents in each group.
This should help you achieve what you are looking for.
Here is a working example:
The Result:
{FATAL=2, SLIGHT=2, SERIOUS=1}
一种解决方案是使用
collector
对返回最大受害者数量的每个组进行分组。One solution is to group by severity, using a
Collector
for each group that returns the maximum number of victims.