Set collection interface:
A Set is a Collection that cannot contain duplicate elements. The Java platform contains three general-purpose Set implementations: HashSet, TreeSet, and LinkedHashSet
HashSet: This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not
guarantee that the order will remain constant over time. This class permits the null element.
HashSet<String> hs =new HashSet<String>();
hs.add("Name 1");
hs.add("Name 2");
hs.add("Name 3");
A Set is a Collection that cannot contain duplicate elements. The Java platform contains three general-purpose Set implementations: HashSet, TreeSet, and LinkedHashSet
HashSet: This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not
guarantee that the order will remain constant over time. This class permits the null element.
HashSet<String> hs =new HashSet<String>();
hs.add("Name 1");
hs.add("Name 2");
hs.add("Name 3");
int sizehashset = hs.size();
System.out.println(sizehashset);
Iterator<String> itr=hs.iterator();
while(itr.hasNext()){
System.out.println(itr.next()); }
System.out.println(sizehashset);
Iterator<String> itr=hs.iterator();
while(itr.hasNext()){
System.out.println(itr.next()); }
hs.contains(Object) ;
hs.clear();
TreeSet : TreeSet provides an implementation of the Set interface that uses a tree for storage. Objects are stored in sorted, ascending order. The TreeSet class implements NavigableSet interface that extends the SortedSet interface. Access and retrieval times are quite fast.
LinkedHashSet : LinkedHashSet is between HashSet and TreeSet. It is implemented as a hash table with a linked list running through it, so it provides the order of insertion.
LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set.