collections set &list interfaces
2 posts in topic
Thread View Thread View

collections set &list interfaces...
Sun Jul 01, 2012 11:23 AM

How can you say set will not allow duplicates?how can you remove duplicate elements from list?

narsimhamurthy peddoju


Mon Aug 06, 2012 10:22 AM





Hi Narsinmha ,

The basic characteristic of set is to allow only unique values in collection and Set make sure that using the equals method of the elements being added. So let say if you are adding Person object and if you implement proper equals and hashcode methods then Set will make use of this equals and hashcode methods to make sure that elements are unique. 

Ans to Q 2: Just copy all your element in Set to remove duplicates 

 Set is the child element of collection if we want&...

Tue Dec 17, 2013 08:43 PM

Set is the child element of collection
if we want  a group of individual element in a single unit and duplecate are not allow and insertion order not preserve then we should go for Set interface.
because equal method is check. weather the element is duplicate or not .  if it is duplicate it will return false.

List lst = new List();

HashSet hs = new HashSet(lst);
output [12, 23];

About | Sitemap | Contact