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 do you reverse a word in java?
What is covariant return type?
Differentiate storage classes on the basis of their scope?
What is the use of generics? When was it added to the Java development Kit?
what is mutual exclusion? How can you take care of mutual exclusion using java threads? : Java thread
what is comparable and comparator interface?
Which command from the jdk compiles a java program?
What is the exact difference in between Unicast and Multicast object? Where will it be used?
If a method is declared as protected, where may the method be accessed in java programming?
Difference between final and effectively final ?
How do you avoid global variables?
What is native code?
List out benefits of object oriented programming language?
What is a vector in java?
Explain the use of javap tool.