说我有一个对象列表。我想将它们分组到一个列表列表中,其中每个内部列表包含布尔比较函数返回true的元素:
public class VO {
public VO(int age, int val) {
this.age = age;
this.val = val;
}
public int age;
public int val;
}
public void testGrouping() {
VO vo1 = new VO(1, 100); //Equal to vo2
VO vo2 = new VO(3, 105); //Equal to vo1 and vo3
VO vo3 = new VO(5, 110); //Equal to vo2 but not vo1 (age difference >2), so it belongs into a new bucket
VO vo4 = new VO(7, 116); //Equal to vo3, so it belongs into the same bucket as vo3
List<VO> values = Arrays.asList(vo1, vo2, vo3, vo4);
//Group values using isEqual(VO, VO) into buckets somehow
//Any two values in a bucket must pass the check
//Expected without any specific order:
//[[vo1, vo2, [vo3, vo4]]
}
private boolean isEqual(VO a, VO b) {
return Math.abs(a.age - b.age) <= 2 && Math.abs(a.val - b.val) <=10;
}
这只是我所拥有的数据的简化示例,实际上比较方法比这更复杂。重要的是,每个对象必须匹配其桶中与检查相关的其他对象。无法按特定值
对对象进行分组/映射我已经有了这样做的代码,但需要三个级别的for循环,这花了我一天的时间来编写。我很好奇是否可以通过流来实现这一目标。
答案 0 :(得分:0)
首先尝试映射您的项目,然后使用Collectors.groupingBy()
:
values.stream().map(name -> name.split("\\s+")).collect(groupingBy(a -> a[1]));
这不会返回列表而是返回数组 - 但概念是相同的。
答案 1 :(得分:0)
您可以稍微修改 isEqual
方法,以便将其用作 TreeMap
的比较器。然后您可以按如下方式收集 TreeMap<VO, List<VO>>
。此代码适用于 Java 7:
public static int isEqual(VO a, VO b) {
if (Math.abs(a.age - b.age) > 2 || Math.abs(a.val - b.val) > 10)
return 1;
else if (Math.abs(a.age - b.age) <= 2 && Math.abs(a.val - b.val) <= 10)
return 0;
else
return -1;
}
public static void main(String[] args) {
VO vo1 = new VO(1, 100);
VO vo2 = new VO(3, 105);
VO vo3 = new VO(5, 110);
VO vo4 = new VO(7, 116);
List<VO> values = Arrays.asList(vo1, vo2, vo3, vo4);
Map<VO, List<VO>> buckets = new TreeMap<>(new Comparator<VO>() {
@Override
public int compare(VO o1, VO o2) {
return isEqual(o1, o2);
}
});
for (VO vo : values) {
List<VO> list = buckets.get(vo);
if (list == null) {
list = new ArrayList<>();
}
list.add(vo);
buckets.put(vo, list);
}
// output
System.out.println(buckets);
//{VO{1=100}=[VO{1=100}, VO{3=105}], VO{5=110}=[VO{5=110}, VO{7=116}]}
}
public static class VO {
public int age, val;
public VO(int age, int val) {
this.age = age;
this.val = val;
}
@Override
public String toString() {
return "VO{" + age + "=" + val + "}";
}
}