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
How is java hashmap implemented?
What is string and its types?
Difference between character constant and string constant in java ?
What is default exception handling in java?
What does java edition mean?
How a variable is stored in memory?
What is lambda programming?
Do you need to import math in java?
What is multithreading and its advantages?
How much is a java license?
Write the algorithm to check the number non-leaf nodes in a tree.
What is canonical name in java?
What is difference between arraylist and list in java?
What is return keyword in java?
What does system out println () do?