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?
Answers were Sorted based on User's Feedback
Answer / harish
This can achieved by overriding equals and hashCode method
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / mushtaq hussain
by implementing equals and toHashCode method in emp Class
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / 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 |
what is the constructor and how many types of constructors are used in java?
Is it possible for a yielded thread to get chance for its execution again?
What is procedure overloading?
What are the new features in java 8? Explain
Differentiate between stringbuffer and string?
Explain the hierarchy of java exception classes?
Can you make a constructor final in Java?
0 Answers SwanSoft Technologies,
Can a abstract class be declared final?
What is a treeset in java?
What is the integer of 16?
Is java supports multiple inheritance? explain?
what is dynamic method dispatch ?
27 Answers Fidelity, MUET, SDSF,