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



suppose in a class there is a code like this: { Set hs=new Hashset(); hs.add(new Emp("kathy&..

Answer / harish

This can achieved by overriding equals and hashCode method

Is This Answer Correct ?    1 Yes 0 No

suppose in a class there is a code like this: { Set hs=new Hashset(); hs.add(new Emp("kathy&..

Answer / mushtaq hussain

by implementing equals and toHashCode method in emp Class

Is This Answer Correct ?    0 Yes 1 No

suppose in a class there is a code like this: { Set hs=new Hashset(); hs.add(new Emp("kathy&..

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

Post New Answer

More Core Java Interview Questions

What is the driver class?

0 Answers  


What does the @override annotation do?

0 Answers  


what is difference between throw and throws in exception?

48 Answers   Spenco,


What is the purpose of the system class in java programming?

0 Answers  


What do you understand by weak reference?

0 Answers  






Can we assign null to double in java?

0 Answers  


What is the set interface in java programming?

0 Answers  


How many types of equations are there?

0 Answers  


What’s the difference between the methods sleep() and wait()?

0 Answers  


What is difference between static method and static variable?

7 Answers  


How do you bind variables?

0 Answers  


What is Difference between Serializable and Externalizable in Java serialization?

1 Answers   Cognizant, IBM,


Categories