我有一个元素列表。我想根据某些条件将这个列表的元素分组到一个列表列表中。在java中可以很容易地做到这一点吗?
public class CollectionTest {
public static void main(String[] arg) {
Target target0 = new Target();
target0.setRisklevel("III");
target0.setLocation("Combined");
Target target1 = new Target();
target1.setRisklevel("III");
target1.setLocation("Combined");
Target target2 = new Target();
target2.setRisklevel("III");
target2.setLocation("Combined");
Target target3 = new Target();
target3.setRisklevel("III");
target3.setLocation("Combined");
Target target4 = new Target();
target4.setRisklevel("IV");
target4.setLocation("Combined");
Target target5 = new Target();
target5.setRisklevel("IV");
target5.setLocation("Combined");
Target target6 = new Target();
target6.setRisklevel("IV");
target6.setLocation("Combined");
Target target7 = new Target();
target7.setRisklevel("II");
target7.setLocation("Domestic");
Target target8 = new Target();
target8.setRisklevel("IV");
target8.setLocation("Domestic");
Target target9 = new Target();
target9.setRisklevel("IV");
target9.setLocation("Domestic");
Target target10 = new Target();
target10.setRisklevel("IV");
target10.setLocation("Domestic");
Target target11 = new Target();
target11.setRisklevel("IV");
target11.setLocation("Domestic");
List<Target> ucrtargetList = new ArrayList<Target>();
ucrtargetList.add(target0);
ucrtargetList.add(target1);
ucrtargetList.add(target2);
ucrtargetList.add(target3);
ucrtargetList.add(target4);
ucrtargetList.add(target5);
ucrtargetList.add(target6);
ucrtargetList.add(target7);
ucrtargetList.add(target8);
ucrtargetList.add(target9);
ucrtargetList.add(target10);
ucrtargetList.add(target11);
List<List<Target>> fullList = new ArrayList<List<Target>>();
}
}这里的条件是RiskLevel,并且List的列表中的每个列表中的位置必须相同。所以fullList应该有4个列表(第一个III &组合,第二个IV &组合,第三个II &国内,第四个IV &国内)。
我可以遍历列表并设置值。有没有更简单的方法来使用java8或apache commons来实现这一点?
发布于 2018-10-22 17:05:51
还不能评论,所以添加一个答案,以扩展@Schidu Luca回答的内容,'groupingBy‘子句可以链接,如下所示
Map<String, List<Target>> collect = ucrtargetList.stream()
.collect(Collectors.groupingBy(Target::getRisklevel, Collectors.groupingBy(Target::getLocation)));发布于 2018-10-22 19:43:32
根据你的描述,fullList应该有4个列表(第一个III和组合,第二个IV和组合,第三个II和国内,第四个IV和国内)我假设你正在寻找这个:
Function<Target, List<String>> riskLevelAndLocation = t -> List.of(t.getRiskLevel(), t.getLocation());
Map<List<String>, List<Target>> fullList = ucrtargetList.stream()
.collect(Collectors.groupingBy(riskLevelAndLocation));riskLevelAndLocation是一个Function,它返回包含riskLevel和location的List。groupingBy a List之所以有效,是因为List.equals(Object o)
当且仅当指定的对象也是一个列表,两个列表具有相同的大小,并且两个列表中所有对应的元素对都相等时,
才返回
true。
快速检查:
for (Entry<List<String>, List<Target>> entrySet : fullList.entrySet()) {
System.out.println(entrySet.getKey().toString() + ": " + entrySet.getValue().size());
}输出为:
[IV, Combined]: 3
[II, Domestic]: 1
[III, Combined]: 4
[IV, Domestic]: 4如果fullList必须具有您所描述的顺序,那么您可以使用LinkedHashMap对其进行排序。
发布于 2018-10-22 16:59:03
List<List<Target>> fullList = ucrtargetList.stream()
.collect(Collectors.groupingBy(Target::getRisklevel)) // Or another collector
.entrySet()
.stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList());如果要从结果中删除相同的对象,则需要添加方法'equals‘和'hashcode’,并收集要设置的对象。
https://stackoverflow.com/questions/52925450
复制相似问题