If set accepts only one argument then how can it compare
two objects to avoid duplicates
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
Why is inheritance used in java?
What does system out println () do?
How will you add panel to a frame?
how many ways we can serialize the java object?
what is the difference between ArrayList and Vector
19 Answers KPIT, Sasken, Satyam, Span Infotech, Wipro,
How will you serialize a singleton class without violating singleton pattern?
Suppose there is a System A which gives some output. This output is used as input by system B. The rate at which System A produces is faster than the rate at which system B consumes it. How can you improve this?
what is main purpose of interface?
2 Answers Accenture, HCL, NIIT,
What will happen if a thrown exception is not handled?
0 Answers ABB, Akamai Technologies, Infogain,
What is 16 bits called?
difference between String a; and String a=new String();? y do v need to assign memory to the variable?
How to use arraylist in java netbeans?