返回具有不同姓氏的Student对象

时间:2015-05-12 15:47:01

标签: java treeset

有20个学生对象的集合。您应该编写一个方法来返回具有不同姓氏的学生对象

例如,如果学生是John Doe,John Lincoln,Amy Doe,Foo Bar =>然后输出应该返回John Doe,John Lincoln,Foo Bar的Student对象。

这是我的代码。怎么了 ?它打印重复的学生对象Akshay Jain和Om Jain。因为lastName是相同的,所以应该避免。在其他情况下,它提供正确的输出。

class Student{
        private String firstName;
        private String lastName;
        public Student(String firstName, String lastName) {
            super();
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName( String firstName ) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName( String lastName ) {
            this.lastName = lastName;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((firstName == null) ? 0 : firstName.hashCode());
            result = prime * result
                    + ((lastName == null) ? 0 : lastName.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Student other = (Student) obj;
            if (firstName == null) {
                if (other.firstName != null)
                    return false;
            } else if (!firstName.equals(other.firstName))
                return false;
            if (lastName == null) {
                if (other.lastName != null)
                    return false;
            } else if (!lastName.equals(other.lastName))
                return false;
            return true;
        }

        @Override
        public String toString() {
            return "Student [firstName=" + firstName + ", lastName=" + lastName
                    + "]";
        }

        public static void main(String[] args) {

            TreeSet<Student> treeSet=new TreeSet<Student>(new MyComparator() {
            });
            treeSet.add(new Student("Akshay","Jain"));
            treeSet.add(new Student("Akshay","Shah"));
            treeSet.add(new Student("Rahul","Jain"));
            treeSet.add(new Student("Prakash","Patil"));        
            treeSet.add(new Student("Om","Jain"));
            treeSet.add(new Student("Chaitali","Mehata"));
            treeSet.add(new Student("Obama","Jain"));
            treeSet.add(new Student("Narendra","Jain"));
            treeSet.add(new Student("Vijay","Magdum"));
            treeSet.add(new Student("Hari","Patil"));
            treeSet.add(new Student("Anuj","Doshi"));
            treeSet.add(new Student("Arnav","Gandhi"));
            treeSet.add(new Student("Abhay","Jain"));
            treeSet.add(new Student("Kedar","Gandhi"));
            System.out.println(treeSet);
        }
    }

    class MyComparator implements Comparator<Student> {
        @Override
        public int compare(Student s1, Student s2) {
            if(s1.getLastName().equals(s2.getLastName()))           
                return 0;
            else            
                return +1;
        }
    }

2 个答案:

答案 0 :(得分:0)

你的班级MyComparator错了。看看这个:

class MyComparator implements Comparator<Student> {
    @Override
    public int compare(Student s1, Student s2) {
        int comp = s1.getLastName().compareTo(s2.getLastName());
        if (comp !=0 ) return comp;
        return s1.getFirstName().compareTo(s2.getFirstName());
    }
}

方法&#34;比较()&#34;必须返回:

  • 如果s1&lt; s2 =&gt; ℃,
  • 如果s1 == s2 =&gt; 0
  • 如果s1> s2 =&gt;大于0

答案 1 :(得分:0)

我从未使用过TreeSets和Comparators,但我查看了文档,并说add方法使用了逻辑

  

如果指定的元素尚不存在,则将其添加到此集合中。   更正式地说,如果集合,则将指定的元素e添加到此集合中   不包含元素e2(e == null?e2 == null:e.equals(e2))。   如果此集合已包含该元素,则该调用将离开该集合   不变并返回false。

所以我认为问题是TreeMap不会使用你的Comparator来添加仅用于排序。

然后在add方法中调用Student的equals,问题出在

行上
 else if (!firstName.equals(other.firstName))
        return false;

因为它在比较姓氏之前返回false,因此Akshay Jain和Om Jain比较返回false,然后两者都插入到TreeMap中。