从列表对象创建一个sortedmap,其值表示为n个最低对象的列表列表的列表,映射到特定键
我正在使用一个CSV文件,其中包括有关事故的一些信息。
我已经创建了事故
类型:
private Integer driverAge;
private Integer vehicleAge;
public Accident(Integer driverAge, Integer vehicleAge) {
this.driverAge = driverAge;
this.vehicleAge = vehicleAge;
}
我还创建了一个读取所有CSV文件的函数,将所有事故转换为list< atest>
并将其保存到此类型意外结构
:
private List<Accident> accidents;
public AccidentArchive(List<Accident> accidents) {
this.accidents = accidents;
}
因此,我们正在使用我尚不完全理解的流,并且我一直在练习中,我必须制作返回sortedmap&lt的函数; k,v&gt;
,其中键必须是drigrage
值,并且值必须为 list 在 n
最低车辆
车辆 值值:
public SortedMap<Integer, List<Integer>> getNMinVehicleAgesPerDriverAge(Integer n) {
return getAccidents().stream().
...
我尝试使用collector.tomap()
and collector.tolist()
以某种方式使它起作用,但我不知道该怎么做。
I am working with a CSV file which includes some information about accidents.
I've created the Accident
type:
private Integer driverAge;
private Integer vehicleAge;
public Accident(Integer driverAge, Integer vehicleAge) {
this.driverAge = driverAge;
this.vehicleAge = vehicleAge;
}
I've also created a function that reads all the CSV file, converts all the accidents to a List<Accident>
and saves it to this type AccidentArchive
:
private List<Accident> accidents;
public AccidentArchive(List<Accident> accidents) {
this.accidents = accidents;
}
So, we are working with streams which I don't understand entirely yet, and I've been stuck in this exercise where I have to make a function that returns a SortedMap<K, V>
in which the key has to be the driverAge
values and the value has to be a list sorted in descending order of the n
lowest vehicleAge
values with the same driverAge
value:
public SortedMap<Integer, List<Integer>> getNMinVehicleAgesPerDriverAge(Integer n) {
return getAccidents().stream().
...
I have tried using Collectors.toMap()
and Collectors.toList()
to somehow make it work, but I have no idea how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简化方法
此问题与通过部分排序查找
n
最大值(或最小)值的算法问题相关。它是使用收藏家的实施似乎很难的,因此我决定引入简化的解决方案。我们可以使用:
classifier
function ,mapfactory
(允许指定结果类型地图)作为
groupingby()
的下游收集器,我们可以使用collectorsmapping> mapping()
and code> and code> tolist()代码>和a 函数将对映射到每个 key 的整个结果列表进行排序,然后将删除仅保留n
最低Vehicleage :
我之前说过的更多性能版本
,我们不需要对映射到每个键的所有值进行排序。与列表的总尺寸相比,
n
是矮人(例如3
to100,000
每个键),这将导致严重的性能命中。我们可以使用
PriorityQueue
(这是JDK中内置的HEAP数据结构的实现)来引入部分分类。为了增强先前的解决方案,我们需要替换
groupingby()
的下游收集器,您可以使用mapping()
和自定义收集器由Priorityqueue
支持,该>仅保留n
最低vericleage
与每个driverage
:负责根据所得列表的提供的最大大小和A 比较器来生成自定义收集器。
,分配没有指定使用
sortedmap
作为返回类型的要求。最好使用navigableMap
接口,该接口定义了更广泛的方法。Simplified approach
This problem correlates with the algorithmic question of finding
N
maximum (or minimum) values via partial sorting. It's implementation using collectors might seem tough, therefore I've decided to introduce a simplified solution.We can use a flavor of
groupingBy()
that expects three arguments:classifier
function,mapFactory
(which allows to specify the resulting type of map)As a downstream collector of
groupingBy()
we can utilizecollectingAndThen
with a combination of collectorsmapping()
andtoList()
and a function that will sort the whole resulting list mapped to each key, and then will drop unnecessary values retaining onlyn
lowestvehicleAge
:More performant version
As I've said before, we don't need to sort all values mapped to each key when we need only some of them. When
n
is dwarfish in comparison to the total size of the list (like3
to100,000
for every key) it will cause a serious performance hit.We can introduce a partial sorting by using
PriorityQueue
(which is an implementation of the Heap data structure built-in in the JDK).To enhance the previous solution, we need to replace the downstream collector of
groupingBy()
you can utilize a combination ofmapping()
and a custom collector backed by thePriorityQueue
which retain onlyn
lowestvehicleAge
values associated with eachdriverAge
:The method provided below is responsible for generating a custom collector based on the provided maximum size of the resulting list and a comparator. The logic behind it was explained in great detail in this answer:
By the way, if your assignment doesn't specify the requirement to utilize
SortedMap
as a return type. It better to useNavigableMap
interface, which defines a wider range of methods, instead.