If set accepts only one argument then how can it compare
two objects to avoid duplicates

Answers were Sorted based on User's Feedback



If set accepts only one argument then how can it compare two objects to avoid duplicates..

Answer / ashutosh

for comparison between two objects of the same class, class
must override equals() and hashcode() methods of the object
class.

Is This Answer Correct ?    6 Yes 1 No

If set accepts only one argument then how can it compare two objects to avoid duplicates..

Answer / r.jainrocks@gmail.com

Set compares hash code of new instance which
has to include in set with already containing values in Set.

because of this comparing b/w instances adding in set is slow
in Set and retrieval is fast.

Is This Answer Correct ?    1 Yes 0 No

If set accepts only one argument then how can it compare two objects to avoid duplicates..

Answer / suresh

if the adding element is the first element, then the
element will be added directly to set stream. If there are
elements is Set the first.
1. while adding the new element to set hashcode will be
generated to element by using hashCode() method.
2.Then generated hash code will be compare with each and
every element of the Set by using equels() method. if
equels method retunrs false then the element will be added
to Set else the element will be discarded.

So, in order to avoid the duplicats we have to override
both equels and hash code methods.

If above given info is wrong, plse let me know at
sureshamca47@gmail.com

Is This Answer Correct ?    1 Yes 0 No

If set accepts only one argument then how can it compare two objects to avoid duplicates..

Answer / manju kt

using "this" keyword

Is This Answer Correct ?    0 Yes 2 No

If set accepts only one argument then how can it compare two objects to avoid duplicates..

Answer / maankutti

need to overload < operator

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More Core Java Interview Questions

What are different types of encoding?

0 Answers  


What is the difference between superclass and subclass?

0 Answers  


We can write any Java Logics in our JSP. Then why we are using servlets(Controller Layer) to interact with the DAO ?

6 Answers   TCS,


Explain importance of throws keyword in java?

0 Answers  


What is externalizable?

0 Answers  


How many digits is int32?

0 Answers  


Difference between JDK, JRE, JVM

16 Answers   Deloitte, HCL, Mind Tree, Oracle, Reliance, TCS, ThinkBox,


How many bits is a double?

0 Answers  


What are the procedures?

0 Answers  


difference between java ,c#&java,c++

3 Answers  


What are thread priorities and importance of thread priorities in java?

0 Answers  


What are the characteristics provided in jdk1.6 apart from other versions?

3 Answers   TCS,


Categories