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
What is overloading and overriding in java?
When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?
What are synchronized methods ?
What does mean in regex?
Explain about OOPS concepts and fundamentals.
When the constructor of a class is invoked?
What is a protected void?
What is floor in java?
What is better- service oriented or batch oriented solutions?
What is variable and its types?
What is classes in java?
Explain the difference between intermediate and terminal operations in java8?
What is java regex?
Is hashset ordered?
Does a function need a return?