Hashset
Output,
set 1
set 2
Linked Hash set,
- uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
- contains unique elements only.
![](http://javaconceptoftheday.com/wp-content/uploads/2015/01/LinkedHashSetClass.png)
Difference between List and Set
List can contain duplicate elements whereas Set contains unique elements only.
example,
HashSet<String> hashSet=new HashSet<String>();
hashSet.add("set 1");
hashSet.add("set 2");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
HashSet<String> hashSet=new HashSet<String>();
hashSet.add("set 1");
hashSet.add("set 2");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
Output,
set 1
set 2
Linked Hash set,
- Contains unique elements only like HashSet. It extends HashSet class and implements Set interface.
- Maintains insertion order.
Example,
LinkedHashSet<String> linkedHashSet=new LinkedHashSet<String>();
Tree set,
- contains unique elements only like HashSet. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.
- maintains ascending order.
Example,
TreeSet<String> al=new TreeSet<String>();
Differences,
![](http://1.bp.blogspot.com/-SkmqiUcH6qI/VR6EV2I1HFI/AAAAAAAAGKo/0s7s2eUr9kw/s640/Java%2B-%2BCollection%2BFramework%2B-%2BHashSet%2BVs.%2BLinkedHashSet%2BVs.%2BTreeSet.jpg)
No comments:
Post a Comment