Sets

less than 1 minute read

  • Set takes only single instance of an element.
  • While adding an element into a set, a test of equality happens, to determine if the object being pushed already exist.
    • Thus override equals & hashCode methods in case of non-primitive Objects
  • With primitives, no need to override hashCode() and equals()

Declaration and important methods.

With Tree set, sorts to Default Natural Sorting Order, in case of primitives and Boxed type.

Set Iteration and removal using Iterator

Sets RetainAll

Count Common Problem

set1.retainAll(set2) -> retains all the common elements, like intersection