Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

Is empty string in java?

0 Answers  


Can we override static methods in java?

0 Answers  


When parseint method can be used?

0 Answers  


where is .equals() method and how is it different from == operation <giving some confusing implementation> Is hashing related to these?

4 Answers   iFlex,


I need to know about complete topic in java's collections i with an examples

1 Answers   TCS,


Can a set contain duplicates?

0 Answers  


Difference between comparator and comparable in java?

0 Answers  


How do you escape in java?

0 Answers  


What happens when heap memory is full?

0 Answers  


Why do we need wrapper class?

0 Answers  


Catch(Exception e){ } in that what is Exception and purpose of that in that place exactly?

3 Answers  


What super () does in java?

0 Answers  


Categories