Java 检测非唯一值的列表数组

Java 检测非唯一值的列表数组,java,android,Java,Android,addAll工作,但removeAll不工作,甚至在两个数组列表中都有类似的值 List<Student> sourceList = new ArrayList<Student>(students); List<Student> destinationList = new ArrayList<Student>(students1); sourceList.removeAll(students1); destinatio

addAll
工作,但
removeAll
不工作,甚至在两个数组列表中都有类似的值

  List<Student> sourceList = new ArrayList<Student>(students);
    List<Student> destinationList = new ArrayList<Student>(students1);

    sourceList.removeAll(students1);
    destinationList.removeAll(students);
    sourceList.addAll(destinationList);

    for (Student student : sourceList) {
        System.out.println("SIM:::Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
    }
List sourceList=newarraylist(学生);
List destinationList=新数组列表(students1);
sourceList.removeAll(学生1);
目的论者。移除所有(学生);
sourceList.addAll(destinationList);
for(学生:源列表){
System.out.println(“SIM:::Student:[RollNo:“+Student.getRollNo()+”,Name:“+Student.getName()+”]”;
}
您可以改用,它的设计目的是不允许重复

import java.util.*;

public class HashSetDemo {

   public static void main(String args[]) {
      // create a hash set
      HashSet<String> hs = new HashSet<String>();

      // add elements to the hash set
      hs.add("B");
      hs.add("A");
      hs.add("C");

      // trying to add duplicate elements to the hash set
      hs.add("C");
      hs.add("B");
      hs.add("A");

      // Only stores one of each, and orders them.
      System.out.println(hs); // [A, B, C]
   }
}
import java.util.*;
公共类HashSetDemo{
公共静态void main(字符串参数[]){
//创建哈希集
HashSet hs=新的HashSet();
//将元素添加到哈希集
hs.添加(“B”);
hs.添加(“A”);
hs.添加(“C”);
//正在尝试将重复元素添加到哈希集
hs.添加(“C”);
hs.添加(“B”);
hs.添加(“A”);
//每个只存储一个,然后订购。
系统输出打印Ln(hs);//[A,B,C]
}
}
我终于找到了解决办法。 它在两个listArray中显示相似的项

 private void findsimalarity() {

    List<Student> sourceList = new ArrayList<>();
    List<Student> destinationList = new ArrayList<>();

    sourceList.addAll(students);
    destinationList.addAll(students1);

    destinationList.removeAll(sourceList);

    sourceList.removeAll(destinationList);

    if (sourceList.size() >= destinationList.size()) {
        //destination list contains similar items
        for (Student student : destinationList) {
            System.out.println("SIMILAR:::Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
        }
    } else {
        //source list contains similar items
        for (Student student : sourceList) {
            System.out.println("SIMILAR:::Student: [RollNo : " + student.getRollNo() + ", Name : " + student.getName() + " ]");
        }
    }

}
private void findsimalarity(){
List sourceList=new ArrayList();
List destinationList=new ArrayList();
sourceList.addAll(学生);
destinationList.addAll(学生1);
destinationList.removeAll(源列表);
sourceList.removeAll(目标列表);
if(sourceList.size()>=destinationList.size()){
//目的地列表包含类似的项目
for(学生:目标列表){
System.out.println(“类似:::学生:[RollNo:“+Student.getRollNo()+”,Name:“+Student.getName()+”]”);
}
}否则{
//源列表包含类似的项
for(学生:源列表){
System.out.println(“类似:::学生:[RollNo:“+Student.getRollNo()+”,Name:“+Student.getName()+”]”);
}
}
}

确保
Student
有意义地覆盖
equals
。谢谢你的回答,我也尝试了Hashset,但我无法删除addAll works,但删除all not works