Java - 如何根据其元素的属性分隔列表 [英] Java - How to separate a list based on a property of it's elements

查看:119
本文介绍了Java - 如何根据其元素的属性分隔列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要执行操作的对象的列表。然而,我首先需要将列表分成单独的列表,使得具有相同parentID的所有项目都在同一列表中,然后单独执行每个列表的操作(原因是操作将对象的parentID作为参数)。



根据需要,根据其元素的给定属性分离列表的最佳方法是什么?将在原始列表中传递的对象的数量最多为< 10,000, 1,000。



所有的帮助非常感谢!

解决方案

像您可能想使用 Multimaps.index 来自 Guava 。这将为您建立一个多地图,每个键都有一个元素集合。



keyFunction index 将是一个函数,它只是从单个元素中检索属性。


I have a list of objects which I want to perform an operation on. However I firstly need to divide the list into separate lists such that all items with the same parentID are in the same list, and then the operation is performed on each list separately (the reason being that the operation takes the parentID of the objects as a parameter).

What is the best way to separate a list based on a given property of it's elements, as required here? The highest number of objects that will be passed in the original list is < 10,000 and normally will be < 1,000.

All help is much appreciated!

解决方案

It sounds like you might want to use Multimaps.index from Guava. That will build you a multi-map, where each key has a collection of elements.

The keyFunction passed into index would be a Function which just retrieves the property from a single element.

这篇关于Java - 如何根据其元素的属性分隔列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆