Java 仅包含相交数据的过滤器阵列列表
我有以下课程:Java 仅包含相交数据的过滤器阵列列表,java,collections,guava,Java,Collections,Guava,我有以下课程: public class School{ List<ClassRoom> classRooms; } public class ClassRoom{ List<Student> students; } public class Student{ String name; List<Long> typeIdList; } 只是想知道番石榴是否能处理这样的事情? 通过交叉,我的意思是它必须发生在所有的实例中考虑
public class School{
List<ClassRoom> classRooms;
}
public class ClassRoom{
List<Student> students;
}
public class Student{
String name;
List<Long> typeIdList;
}
只是想知道番石榴是否能处理这样的事情?
通过交叉,我的意思是它必须发生在所有的实例中考虑这个类型。
我知道
for
循环更具可读性,但我只是发现番石榴的功能。如果您试图检查所有学生是否都有特定的类型ID
,请使用Iterables.all
和您现有的谓词
您还可以使用Multimaps.index()
,创建学生的Multimaps
,通过其typeId
索引:
Multimap studentsByTypeId=Multimaps.index(学生,新函数(){
公众长期申请(学生){
返回s.typeId;
}
};
然后可以使用studentsByTypeId.keySet()
获取唯一的typeId
s
您可以使用studentsByTypeId.keySet().size()==1检查它们是否都共享相同的typeId
,如果您想要相交的部分,您不应该与特定的部分进行比较,您至少应该检查它是否包含在其他集合的ID中。如下所示:
new Predicate<Student>() {
@Override
public boolean apply(Student s) {
return otherIds.contains(s.typeId);
}
}
new谓词(){
@凌驾
公共布尔应用(学生s){
返回otherid.contains(s.typeId);
}
}
但我仍然认为,如果你对两个集合进行二进制搜索(在对它们排序之后),你可以更快地得到答案
Collections.sort(list1);
Collections.sort(list2);
List<E> intersected = new ArrayList<E>();
for(E element : list1){
if(Collections.binarySearch(list2, element) >= 0){
intersected.add(element);
}
}
Collections.sort(列表1);
Collections.sort(列表2);
List intersected=新建ArrayList();
对于(E元素:列表1){
if(Collections.binarySearch(list2,element)>=0){
相交。添加(元素);
}
}
您甚至可以找到一种方法来查找最小的列表。它可以帮助它获得一些性能。您可以使用一个可以计算发生次数的:
ClassRoom classRoom = /* comes from somewhere */;
List<Student> students = classRoom.getStudents();
// Aggregate all the typeIds.
Multiset<Long> typeIds = HashMultiset.create();
for (Student student : students) {
// Assuming a student doesn't have duplicated typeIds:
typeIds.addAll(student.getTypeIds());
}
// Find which typeIds are present for all the students.
for (Multiset.Entry<Long> entry : typeIds.entrySet()) {
if (entry.getCount() == students.size()) {
System.out.println(entry.getElement());
}
}
教室=/*来自某处*/;
List students=教室。getStudents();
//聚合所有类型ID。
Multiset typeIds=HashMultiset.create();
用于(学生:学生){
//假设学生没有重复的TypeID:
addAll(student.getTypeIds());
}
//找出为所有学生提供的TypeID。
for(Multiset.Entry:typeIds.entrySet()){
if(entry.getCount()==students.size()){
System.out.println(entry.getElement());
}
}
我对你的要求有点困惑。你是否试图检查集合中的所有学生是否都具有相同的typeId
?一个特定的typeId
?你已经有了一个番石榴解决方案,可以获得所有具有指定typeId
的学生。这是什么谓词ava还是Apache Commons?@Mark:我只需要输入这是所有人的共同点students@Echo:当每个学生只能有一个typeId
时,我不明白为什么所有学生都有两个不同的typeId
。这是没有意义的。@Echo我必须同意Mark的观点。你能提供一个你想要实现的例子吗?你的解决方案很好,但以防万一我有动态列表数量,这需要一些努力。这仍然是可行的。如果您有动态列表数量,我强烈建议您考虑一种执行方式来实现这一点,我将尝试编辑我的答案,以获得一个包含多个集合的解决方案。您想要所有集合之间的交点吗?
Collections.sort(list1);
Collections.sort(list2);
List<E> intersected = new ArrayList<E>();
for(E element : list1){
if(Collections.binarySearch(list2, element) >= 0){
intersected.add(element);
}
}
ClassRoom classRoom = /* comes from somewhere */;
List<Student> students = classRoom.getStudents();
// Aggregate all the typeIds.
Multiset<Long> typeIds = HashMultiset.create();
for (Student student : students) {
// Assuming a student doesn't have duplicated typeIds:
typeIds.addAll(student.getTypeIds());
}
// Find which typeIds are present for all the students.
for (Multiset.Entry<Long> entry : typeIds.entrySet()) {
if (entry.getCount() == students.size()) {
System.out.println(entry.getElement());
}
}