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

What is javac in java?

0 Answers  


What is OOP's Terms with explanation?

0 Answers  


What is tochararray in java?

0 Answers  


Is null an object java?

0 Answers  


Name few "optional" classes introduced with java 8 ?

0 Answers  


What is nullpointerexception in java?

0 Answers  


What does ide stand for?

0 Answers  


all are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined classes and we can extend atmost one class so each class can extend more than one class so java supports multiple inheritance?i am confused with this,pls any one explain me.

8 Answers   CFG,


11. static class A { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B”); } 16. } 17. public static void main(String[] args) { 18. new B().process(); 19. } What is the result? 1 B 2 The code runs with no output. 3 Compilation fails because of an error in line 12. 4 Compilation fails because of an error in line 15.

6 Answers  


What is singleton class?

16 Answers   3i Infotech, 7 Seas, ABC, Amdocs, Cap Gemini, Oracle, Persistent, TCS, Techforza,


Difference between ‘is-a’ and ‘has-a’ relationship in java?

0 Answers  


Explain when noclassdeffounderror will be raised ?

0 Answers  


Categories