0

There is a collection of 20 Student objects. You are supposed to write a method to return Student objects with distinct last names

E.g If Students are John Doe, John Lincoln, Amy Doe, Foo Bar => then output should return Student objects for John Doe, John Lincoln, Foo Bar.

This is my code. What is wrong ? It prints duplicate Student object Akshay Jain and Om Jain. As lastName is same,it should be avoided. In other cases it is giving correct output.

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;
        }
    }
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
Akshay Jain
  • 73
  • 2
  • 8
  • 2
    You could have at least taken the bullet out of the title – chancea May 12 '15 at 15:51
  • Actually doing you homework you can learn a lot of new interesting things! – uraimo May 12 '15 at 15:52
  • Here is a fun fact, there are so many examples how to use comparator for object properties. Use them, I assure you it will be literally copy-paste. – We are Borg May 12 '15 at 15:54
  • possible duplicate of [How to use Java comparator properly?](http://stackoverflow.com/questions/7117044/how-to-use-java-comparator-properly) – vittore May 12 '15 at 16:07

2 Answers2

0

Your class MyComparator is wrong. Look at this one:

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());
    }
}

Method "compare()" must return:

  • if s1<s2 => <0
  • if s1==s2 => 0
  • if s1>s2 => >0
srcarro
  • 61
  • 3
0

I have never used TreeSets nor Comparators but I took a look at the documentation and says that the add method uses the logic

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.

So I think the problem is that the TreeMap doesn't use your Comparator for adding just for sorting.

Then on the add method the equals of the Student is called and the problem lies on the line

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

because it returns false before comparing the last names so Akshay Jain and Om Jain comparison returns false and then both are inserted in the TreeMap.

JMorales
  • 115
  • 1
  • 9