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 private static in java?

0 Answers  


What is the root class for all Java classes?

8 Answers   IBM, Infosys,


What is a conditional statement explain with example?

0 Answers  


What class is used to create Server side object?

1 Answers   TCS,


if the memory capacity is 700 presently occupied by process is 690. then another process request space(40) how this situation handled in java.

4 Answers   Wipro,






what is mutual exclusion? How can you take care of mutual exclusion using java threads? : Java thread

0 Answers  


There are 2 different ways to create an object. a)By using keyword "new" b)By using Class.forName ("className").newInstance(); What is the difference between these 2 methods.

3 Answers  


What is final?

0 Answers  


What are the access modifiers in java?

0 Answers  


String is mutable or immutable?

3 Answers  


Given: 11. public static void main(String[] args) { 12. Integer i = uew Integer(1) + new Integer(2); 13. switch(i) { 14. case 3: System.out.println(”three”); break; 15. default: System.out.println(”other”); break; 16. } 17. } ‘What is the result? 1 three 2 other 3 An exception is thrown at runtime. 4 Compilation fails because of an error on line 12.

9 Answers  


How does synchronized modifier work?

1 Answers   IBM,


Categories