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
Does java support multiple inheritances?
Can we convert stringbuilder to string in java?
What is lexicographically smallest string?
What is java beans?
What are the differences between forwarding () method and sendredirect() methods?
Define nashorn in java8.
What is array command?
Is node a data type in java?
Is Constructor possible in abstract class in java ?
List two java ide’s?
What happens when heap memory is full?
Write a program to print count of empty strings in java 8?
Is c better than java?
What is a container in a gui?
What is set and get methods in java?