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

Explain the key functions of data binding?

0 Answers   Akamai Technologies, Aspire, Impetus, Infogain, Tavant Technologies, Virtusa,


What is object data type?

0 Answers  


what is Assertion?

4 Answers   Wipro,


What is a void return type?

6 Answers  


how a programmer confirms that the data submitted has been succesfully inserted into the database(either oracle or my sql).. How a programmer confirm if there is any problem with the program he wrote for insertion... ANS:--- >executeupdate method is having boolean return type, if anything goes wrong in data insertion or data updation, it would return false. otherwise, if it successfully inserts data into the database, it would return true NOW HOW TO I CHECK IN MY DURING EXECUTION WHETHER IT RETURNS TRUE OR FALSE... WELL IT WILL DISPLAY ANY MESSAGE OR NOT

0 Answers   Google,






can write code for serialization ?

1 Answers   HCL, MegaSoft,


Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?

0 Answers  


why is multiple inheritance not allowed in java?

7 Answers   Elementus Technologies, Huawei, Infosys,


There are two classes named classa and classb. Both classes are in the same package. Can a private member of classa can be accessed by an object of classb?

0 Answers  


What is the difference between double and float variables in java?

0 Answers  


Why Static variable required in java?For ex,class A { static int a; int b; } Why static is required?

5 Answers   Ericsson,


What is passing by reference in java?

0 Answers  


Categories