Question

How to implement the comparable and equals method in this Tree Map,so that my Contains Value return true.

How to do it?

How to implement?

import java.util.*;
class a
{
    public static void main(String arr[])
    {
        TreeMap<String,Emp> map=new TreeMap<String,Emp>();
        map.put("HEllo",new Emp("ada",23));
        map.put("aehqn",new Emp("rewr",343));
        map.put("rffewrf",new Emp("saerfwe",893743));
        Set<Map.Entry<String,Emp>> x=map.entrySet(); 
        Iterator<Map.Entry<String,Emp>> itr =x.iterator();
        while(itr.hasNext())
        {
            Map.Entry<String,Emp> m=itr.next();
            System.out.println(m.getKey());
            Emp e=m.getValue();
            e.display();
            System.out.println();
        }
        System.out.println("NOw the value we will finid is"+map.containsValue(new Emp("ada",23)));
    }
}
class Emp
{
    String n;
    int i;
    public Emp(String n,int i)
    {
        this.n=n;
        this.i=i;
    }
    public void display()
    {
        System.out.println("there are string  "+n+"  int"+i);
    }
}

Thanks in advance

Was it helpful?

Solution

Your Code will look like

class Emp implements Comparable<Emp>
{
    String n;
    int i;
    public Emp(String n,int i)
    {
        this.n=n;
        this.i=i;
    }
    public void display()
    {
        System.out.println("there are string  "+n+"  int"+i);
    }

    public boolean equals(Object o){
        if(o instanceof Emp){
            Emp d = (Emp)o;
            return ((d.n.equals(n)) && (d.i==i));
        }
        return false;
    }

    public int hashCode(){
        return i/2 + 17;
    }


    public int compareTo(Emp d){
        if(this.i>d.i)
            return 1;
        else if(this.i<d.i)
            return -1;
        return this.n.compareTo(d.n);
    }

}

Please Ignore if any syntax error and you can improve method implementations also.

OTHER TIPS

The containsValue() method of the Map interface uses the equals() method of the Object class.so in your case you have to override the equals() method and when you override the equals() method it is advised to override the hashCode() method too. Below is the correct code:-

public class a
{
    public static void main(String arr[])
    {
        TreeMap<String,Emp> map=new TreeMap<String,Emp>();
        map.put("HEllo",new Emp("ada",23));
        map.put("aehqn",new Emp("rewr",343));
        map.put("rffewrf",new Emp("saerfwe",893743));
        Set<Map.Entry<String,Emp>> x=map.entrySet(); 
        Iterator<Map.Entry<String,Emp>> itr =x.iterator();
        while(itr.hasNext())
        {
            Map.Entry<String,Emp> m=itr.next();
            System.out.println(m.getKey());
            Emp e=m.getValue();
            e.display();
        }
        System.out.println("Now the value we will find is"+map.containsValue(new Emp("ada",23)));
    }
}
class Emp 
{
    String n;
    int i;
    public Emp(String n,int i)
    {
        this.n=n;
        this.i=i;
    }
    public void display()
    {
        System.out.println("there are string  "+n+"  int"+i);
    }
    @Override
    public boolean equals(Object obj) {
        if(obj==null)
        {
            return false;
        }
        if(this.getClass().equals(obj.getClass()))
            return true;
        else
            return false;

    }
    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top