Set interface

In this article, we will discuss Set interface in detail

Set doesn’t allow duplicate items and also doesn’t maintain insertion order

 

Set interface:

  • Set is a sub-interface of Collection interface (i.e.; Set extends Collection)
  • To represent a group of element/objects as a single unit/entity, where duplicates aren’t allowed
  • Set allows only unique element/objects to be inserted
  • Set doesn’t maintain insertion order
  • While iterating through Set, we will get items in random order (as against insertion order in List)
  • There are 2 direct concrete classes for Set interface viz.; HashSet, LinkedHashSet and another one TreeSet (through SortedSet –> NavigableSet)
  • Set interface doesn’t contain any methods but only inherited methods from Collection interface
  • Present in java.util package and extends java.util.Collection interface

14-Set-interace-in-java

Source: Team BenchResources.Net

 

Below listed classes implements Set interface

  • HashSet
  • LinkedHashSet
  • TreeSet (through SoretdSet –> NavigableSet)

 

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
https://docs.oracle.com/javase/7/docs/api/java/util/Set.html

 

Happy Coding !!
Happy Learning !!

HashSet class
Enumeration v/s Iterator v/s ListIterator interfaces