我的结构
A {
String id;
String bid;
}
B {
String id;
}
给出
List<A> aList = Arrays.asList(
new A (1,2),
new A (2,5),
new A (3,9),
new A (4,10),
new A (5, 20),
new A (6, 8),
new A (7, 90)
new A (8, 1)
);
List<B> bList = Arrays.asList(
new B (2),
new B (9),
new B (10)
);
现在,我希望将与任何B元素都不匹配的A元素收集到另一个集合中,并且应该从A集合本身中删除这些元素。
结果
List<A> aList = Arrays.asList(
new A (1,2),
new A (3,9),
new A (4,10)
);
List<A> aListBin = Arrays.asList(
new A (2,5),
new A (5, 20),
new A (6, 8),
new A (7, 90)
new A (8, 1)
);
我接受
我可以想到使用迭代器来迭代A,并针对A中的每个元素遍历B,如果找到,请保持其他状态,继续添加到单独的列表中,并使用迭代器remove删除。
有没有更好的方法使用流魔术来做到这一点?谢谢
答案 0 :(得分:1)
Collectors#partitionBy是你的朋友。
首先,我们将从id
列表中提取B
到裸露的Set<Integer>
中,以便将其用于查找:
Set<Integer> bSet = bList.stream()
.map(b -> b.id)
.collect(Collectors.toSet());
正如JB Nizet所提到的,HashSet
非常适合这份工作。
然后就这么简单–我们将按给定的谓词进行分区。谓词是A.bid
中是否包含B.id
(为方便起见,我们将其存储在bSet
中)。
Map<Boolean, List<A>> map = aList.stream()
.collect(Collectors.partitioningBy(a -> bSet.contains(a.bid)));
现在map.get(true)
包含B
中包含的所有项目,map.get(false)
中包含所有其他项目。
要替换aList
,只需重新分配aList
:
aList = map.get(true);
答案 1 :(得分:0)
您不能从使用Arrays.asList()创建的列表中删除元素。它返回您作为参数传递的数组的视图,因此调用remove将引发UnsupportedOperationException。
假设您拥有ArrayList,但我仍然认为您无法一步一步实现,当然也不能使用“流魔术”来实现,因为流不允许您修改原始集合。
分两步,就像:
List<A> newList = aList.stream()
.filter(a -> !bList.contains(a.bid))
.collect(Collectors.toList());
aList.removeAll(newList);
如果性能存在问题,请使用Set或Map(以id为键)而不是List来分别在O(1)和O(n)中执行contains()和removeAll()。
答案 2 :(得分:0)
是的,您可以使用Java 8 Streams。
这是您输入的完整示例:
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
public class MyClass {
public static void main(String args[]) {
class A {
public int id;
public int bid;
public A(int id, int bid) { this.id = id; this.bid = bid; }
public String toString() { return "(" + id + "," + bid + ")"; }
};
class B {
public int id;
public B(int id) { this.id = id; }
public String toString() { return "" + id; }
};
List<A> aList = Arrays.asList(
new A (1,2), // not removed
new A (2,5), // removed
new A (3,9), // not removed
new A (4,10), // not removed
new A (5, 20),// not removed
new A (6, 8), // not removed
new A (7, 90),// not removed
new A (8, 1)// not removed
);
List<B> bList = Arrays.asList(
new B (2),
new B (9),
new B (10)
);
List<A> aListBin = new ArrayList<>();
aList.stream()
.forEach( a -> {
if (bList.stream().noneMatch(b -> b.id == a.bid )) {
aListBin.add(a);
}
});
aList = aList.stream()
.filter( a -> bList.stream().anyMatch(b -> b.id == a.bid))
.collect(toList());
System.out.println("Alist-> " + aList);
System.out.println("Blist-> " + bList);
System.out.println("Removed-> " + aListBin);
}
}
输出:
Alist-> [(1,2), (3,9), (4,10)]
Blist-> [2, 9, 10]
Removed-> [(2,5), (5,20), (6,8), (7,90), (8,1)]
答案 3 :(得分:0)
您可以使用Collectors.partitioningBy
。是不是更好?取决于您的定义更好。它是更简洁的代码,但是效率不如您描述的简单迭代器循环。
除了使用字符串哈希集查找B类ID之外,我想不出比迭代器路由更有效的方法。
但是,如果您希望使用简洁的代码,以下是使用partitioningBy的代码:
class A {
int id;
int bid;
public A(int id, int bid){
this.id = id;
this.bid = bid;
}
public boolean containsBId(List<B> bList) {
return bList.stream().anyMatch(b -> bid == b.id);
}
}
class B {
int id;
public B(int id){
this.id = id;
}
}
class Main {
public static void main(String[] args) {
List<A> aList = Arrays.asList(
new A (1,2),
new A (2,5),
new A (3,9),
new A (4,10),
new A (5, 20),
new A (6, 8),
new A (7, 90),
new A (8, 1)
);
List<B> bList = Arrays.asList(
new B (2),
new B (9),
new B (10)
);
Map<Boolean, List<A>> split = aList.stream()
.collect(Collectors.partitioningBy(a -> a.containsBId(bList)));
aList = split.get(true);
List<A> aListBin = split.get(false);
}
答案 4 :(得分:0)
由于您要处理两个不同的类,因此无法直接比较它们。因此,您需要减少到最小公分母,即整数ID。
// start a stream of aList.
List<A> aListBin = aList.stream()
// Convert the bList to a collection of
// of ID's so you can filter.
.filter(a -> !bList.stream()
// get the b ID
.map(b->b.id)
// put it in a list
.collect(Collectors.toList())
// test to see if that list of b's ID's
// contains a's bID
.contains(a.bid))
//if id doesn't contain it, then at to the list.
.collect(Collectors.toList());
To finish up, remove the newly created list from the aList.
aList.removeAll(aListBin);
它们显示如下:
System.out.println("aListBin = " + aListBin);
System.out.println("aList = " + aList);
aListBin = [[2, 5], [5, 20], [6, 8], [7, 90], [8, 1]]
aList = [[1, 2], [3, 9], [4, 10]]
注意: