Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Set

Set interface is also an implementation of Collections interface which is mainly needed if we do not want any duplicate. In other words Set will never allow duplicate elements to be added in it.

The concrete implementations of Set are:


java_Set

Methods used in Set

Note : We will be explaining the below methods in detail, when we start explaining the concrete implementation of Set (i.e. HashSet and LinkedHashSet)


  1. boolean add(Object o):

This method is used to 'add' an element to the Set.

  1. void clear()

This method is used to delete all the elements from this Set.

  1. boolean isEmpty()

This method checks if the Set is empty or not. It returns true if the Set is Empty else returns false.


  1. boolean contains(Object o)

This method checks if the Set contains any element or not. It returns true if the Set contains any element else returns false.

  1. Iterator iterator()

The iterator() method is used to Iterate the elements in the Set. Say, you have 10 elements in a Set and want to iterate them one by one. By using iterator() you can achieve them easily.

  1. boolean remove(Object o)

This method is used to remove a specific element from the Set if that is present.

  1. int size()

This method is used to return size of the Set.

Note :All the above methods will be used in the concrete implementation of Set (i.e. In HashSet and LinkedHashSet).




</