suppose in a class there is a code like this:
{
Set hs=new Hashset();
hs.add(new Emp("kathy",1000));
hs.add(new Emp("kathy",2000));
}
how can u avoid the above code in your class as set won't
allow duplicate objects?

Answer Posted / venkata rao ummadisetty

Hi,

Can any one provide a full Example for using HashCode and
equals method

class Emp{
private String name;
private int salary;
Emp(String s,int p){
this.name=s;
this.salary=p;
}

public String toString(){
return name+":"+salary;

}

}

// In public static void main method

Set hs=new HashSet();
hs.add(new Emp("kathy",1000));
hs.add(new Emp("kathy",2000));
hs.add(new Emp("kathy",2000));
java.util.Iterator er=hs.iterator();

while(er.hasNext()){
System.out.println(er.next());}

Is This Answer Correct ?    0 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is overloading and overriding in java?

664


When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?

573


What are synchronized methods ?

621


What does  mean in regex?

575


Explain about OOPS concepts and fundamentals.

598






When the constructor of a class is invoked?

597


What is a protected void?

506


What is floor in java?

564


What is better- service oriented or batch oriented solutions?

1540


What is variable and its types?

547


What is classes in java?

523


Explain the difference between intermediate and terminal operations in java8?

574


What is java regex?

536


Is hashset ordered?

555


Does a function need a return?

535