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
Can we create an object of private class?
What is qualitative variable?
Differentiate between vector and array list.
I want to persist data of objects for later use. What’s the best approach to do so?
What is difference between static class and singleton pattern?
Is set ordered?
What is meant by tab pans?
if u open login & logout ,how can udisplay the timelogin & logout members ?
What is a numeric digit?
How do you test a method for an exception using junit?
What is Mutex (Mutual Exclusion Object) ?
How many types of constructors are used in java?
What java is used for?
what is meant by Garbage collection?
How to instantiate member inner class?