Search code examples
javacollectionssetoverridingtreeset

java.util.TreeSet not following Set's no duplication contract?


I have seen the problem being asked here before but I didn't get much help from then so I'm asking it again with my genuine problem.

I want to remove duplicate object on the basis of all properties of object (Here Name, id, CompanyName, Address). Here is My code:

package CollectionDemo;
import java.util.TreeSet;
class Employee implements Comparable<Employee> {
    String Name;
    int id;
    String CompanyName;
    String Address;
    public Employee(String Name,int id,String CompanyName,String Address) {
        this.Name = Name;
        this.id = id;
        this.CompanyName = CompanyName;
        this.Address = Address;
    }
    @Override
    public String toString() {
        return "Name : "+this.Name+"\tID : "+this.id+"\tCompanyName : "+this.CompanyName+"\tAddress : "+this.Address;
    }
    @Override
    public int compareTo(Employee obj){
        if((this.Name.equals(obj.Name))&&(this.id==obj.id)&&(this.CompanyName.equals(obj.CompanyName))&&(this.Address.equals(obj.Address))) {
            return 0;
        }
        return 1;
    }
}

public class DemoTreeset {

    public static void main(String[] args) {
        TreeSet<Employee> ts = new TreeSet<>();
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 12, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 13, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        ts.add(new Employee("Panda", 12, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 13, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        for(Employee  e : ts) {
            System.out.println(e);
        }
    }
}

OUTPUT:

Name : Panda    ID : 11 CompanyName : Google    Address : California 
Name : Panda    ID : 12 CompanyName : Google    Address : California
Name : Panda    ID : 13 CompanyName : Google    Address : California
Name : Panda    ID : 11 CompanyName : Google    Address : California
Name : Panda    ID : 11 CompanyName : Infosys   Address : India
Name : Panda    ID : 13 CompanyName : Google    Address : California

I know TreeSet uses compareTo() instead of equals() to compare the objects so I override it but as you can see the output, it removed some duplicates but not all of them. I'm not understanding why I'm getting this output. Why is it not removing all the duplicates but only few of them?


Solution

  • Can you Check this one out? Output:

    Name : Panda ID : 11 CompanyName : Google Address : California

    Name : Panda ID : 12 CompanyName : Google Address : California

    Name : Panda ID : 13 CompanyName : Google Address : California

    Name : Panda ID : 11 CompanyName : Infosys Address : India

    package algorithms;
    
    import java.util.TreeSet;
    
    class Employee implements Comparable<Employee> {
    String Name;
    int id;
    String CompanyName;
    String Address;
    
    public Employee(String Name,int id,String CompanyName,String Address) {
        this.Name = Name;
        this.id = id;
        this.CompanyName = CompanyName;
        this.Address = Address;
    }
    
    @Override
    public String toString() {
        return "Name : "+this.Name+"\tID : "+this.id+"\tCompanyName : "+this.CompanyName+"\tAddress : "+this.Address;
    }
    
    @Override
    public int compareTo(Employee obj){
        final int BEFORE = -1;
        final int EQUAL = 0;
        final int AFTER = 1;
    
        if (this.equals(obj)) return EQUAL;
        
        int comparison = this.Name.compareTo(obj.Name);
        if (comparison != EQUAL) return comparison;
    
        comparison = this.Address.compareTo(obj.Address);
        if (comparison != EQUAL) return comparison;
    
        comparison = this.CompanyName.compareTo(obj.CompanyName);
        if (comparison != EQUAL) return comparison;
    
        comparison = ((Integer)(this.id)).compareTo(obj.id);
        if (comparison != EQUAL) return comparison;
    
        return EQUAL;
    }
    
       @Override 
       public boolean equals(Object aThat) {
           if (this == aThat) return true;
           if (!(aThat instanceof Employee)) return false;
    
           Employee that = (Employee)aThat;
           return
               ( this.Address.equals(that.Address)) &&
               (this.id == that.id) &&
               ( this.Name.equals(that.Name) ) &&
               ( this.CompanyName.equals(that.CompanyName) )
             ;
       }
    }
    
    
    public class DemoTreeSet {
    
    public static void main(String[] args) {
        TreeSet<Employee> ts = new TreeSet<>();
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 12, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 13, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        ts.add(new Employee("Panda", 12, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 13, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        ts.add(new Employee("Panda", 11, "Google", "California"));
        ts.add(new Employee("Panda", 11, "Infosys", "India"));
        for(Employee  e : ts) {
            System.out.println(e);
        }
    }
    

    }