collections set &list interfaces
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   narsimha
Posted On:   Sunday, July 1, 2012 11:23 AM

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


narsimhamurthy peddoju

Re: collections set &list interfaces

Posted By:   rahul123  
Posted On:   Tuesday, December 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();
lst.add(12);
lst.add(12);
lst.add(23);
lst.add(23);

HashSet hs = new HashSet(lst);
System.out.prinntln(hs);
output [12, 23];

Re: collections set &list interfaces

Posted By:   amitg  
Posted On:   Monday, August 6, 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 
 

About | Sitemap | Contact