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


Please Help Members By Posting Answers For Below Questions

How do you reverse a word in java?

621


What is covariant return type?

729


Differentiate storage classes on the basis of their scope?

761


What is the use of generics? When was it added to the Java development Kit?

630


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

774






what is comparable and comparator interface?

657


Which command from the jdk compiles a java program?

568


What is the exact difference in between Unicast and Multicast object? Where will it be used?

679


If a method is declared as protected, where may the method be accessed in java programming?

584


Difference between final and effectively final ?

723


How do you avoid global variables?

647


What is native code?

630


List out benefits of object oriented programming language?

568


What is a vector in java?

670


Explain the use of javap tool.

711