Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 忽略对象Arraylist中的重复项_Java_Comparator - Fatal编程技术网

Java 忽略对象Arraylist中的重复项

Java 忽略对象Arraylist中的重复项,java,comparator,Java,Comparator,我有一个包含许多对象的数组列表。我想从中删除重复的对象。。我用TreeSet和Comparator尝试了下面的选项,但它不起作用。列表中添加了以下类对象 public class Student { private String name; private String location; private int score; private int age; private String department; Student(){}

我有一个包含许多对象的数组列表。我想从中删除重复的对象。。我用TreeSet和Comparator尝试了下面的选项,但它不起作用。列表中添加了以下类对象

    public class Student {
    private String name;
    private String location;
    private int score;
    private int age;
    private String department;

    Student(){}
    Student(String sName,String loc,int ag,int scr,String dep){
        setName(sName);
        setLocation(loc);
        setScore(scr);
        setAge(ag);
        setDepartment(dep);
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }


}
下面是我的主课

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;


public class MyMain {
    public static void main(String[] args) {
        Student s1= new Student("John","Aus",25,100,"Finance");
        Student s2= new Student("John","Aus",25,100,"Finance");
        Student s3= new Student("John","Aus",26,100,"Finance");
        Student s4= new Student("Alex","Ind",20,101,"Finance");
        Student s5= new Student("Alex","Ind",20,101,"Finance");
        Student s6= new Student("Alex","Ind",28,101,"Finance");
        ArrayList<Student> studentsList= new ArrayList<Student>();

        studentsList.add(s1);
        studentsList.add(s2);
        studentsList.add(s3);
        studentsList.add(s4);
        studentsList.add(s5);
        studentsList.add(s6);       

        for(int i=0;i<studentsList.size();i++){
            Student s=(Student)studentsList.get(i);
            System.out.println(i+ "  "+s.getName()+" "+s.getLocation()+" "+s.getAge()+" "+s.getScore()+" "+s.getDepartment());
        }

        Set set = new TreeSet(new Comparator<Student>() {

            @Override
            public int compare(Student s1, Student s2) {
                if( s1.getName().equalsIgnoreCase(s2.getName()) && s1.getLocation().equalsIgnoreCase(s2.getLocation()) && s1.getScore()==s2.getScore()){
                    return 0;
                }               
                return 1;
            }



        });     
        set.addAll(studentsList);
        studentsList.clear();
        studentsList.addAll(set);

        System.out.println("---------Final Result----------");
        for(int i=0;i<studentsList.size();i++){
            Student s=(Student)studentsList.get(i);
            System.out.println(i+ "  "+s.getName()+" "+s.getLocation()+" "+s.getAge()+" "+s.getScore()+" "+s.getDepartment());
        }       

    }

}
但我的预期产出是

0  John Aus 100 25 Finance
1  Alex Ind 101 20 Finance
请帮我做这个。这种方法正确吗?另外,我希望保持Student类的完整性(不能重写equals和hashCode)

请注意

您发布的结果是有效的 在您的竞争对手内,您可以检查:

  • 名字
  • 位置
  • 得分
  • 这就是为什么你会得到副本-因为分数不同您刚刚交换了年龄和分数参数,仅此而已

    Student(String sName,String loc,int scr,int ag,String dep)
    
    Student s1= new Student("John","Aus",25,100,"Finance");
    

    重写
    等于
    和/或
    hashCode
    以何种方式不使
    Student
    保持不变?我认为comparator正在做您希望它做的工作。问题在于您向Student的构造函数输入参数的顺序。请使用
    Set
    以避免重复。正如你提到的,这是我在代码中的错误。刚刚更新过,效果很好。在进行此类比较时,只需再澄清一点,列表中元素的顺序是否重要?列表中元素的顺序对结果没有影响(将保持不变)。。至于性能-这恐怕是另一个问题:)哈哈:)这里有一些有趣的事情给你:谢谢,我会检查网址。。。同时,我刚刚向列表中添加了3个对象,如下所示,但现在最终列表包含重复项。。。看来它不起作用了。。。。学生s7=新学生(“吉姆”,“工业”,28102,“营销”);学生s8=新生(“吉姆”,“工业”,28102,“营销”);学生s9=新学生(“蚂蚁”,“Ind”,29102,“营销”);
    Student(String sName,String loc,int scr,int ag,String dep)
    
    Student s1= new Student("John","Aus",25,100,"Finance");