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
Explain the overview of UDP messaging.
Why string is a class?
What is the difference between @before and @beforeclass annotation?
In Java list the methods that can be overridden?
State one difference between a template class and class template.
What is module with example?
How do you get the length of a string in java?
Can we execute a program without main?
What does @param args mean in java?
What is procedure overloading?
What is the relationship between clipping and repainting under awt?
What is += mean in java?
What are the skills required for core java?
Which container method is used to cause a container to be laid out and redisplayed in java programming?
Can java cast null?