The Set Interface tutorial in Java Collection

The blog provides the details about the Java Collection Set Interface. The Set Interface in java collection does not support duplicate elements like List Interface. The Set Interface inherits methods only from Collection Interface and provides comparison for equals and hashcode operation irrespective of having different implementation types

Set Interface Implementation Types

The Java Collection Framework provides 3 types of Set Implementation:

  • HashSet: stores elements in a Hash Table and provides high quality implementation
  • TreeSet: stores elements in a tree like structure and orders the elements based on values, performs slower than HashSet
  • LinkedHashSet: stores elements in a Hash Table with a linked list and orders the elements based on sequential insertion process, performs slower than HashSet

The Set Interface class Hierarchy is given below

public interface Set<E>
extends Collection<E>

The Set Interface provides the below given methods for implementation

HashSet MethodsHashSet Methods Description
add(E e)add the specified element to the set if not present
addAll(Collection <? Extends E>c)allows to insert all elements in the given collection to the set
clear()removes all elements from the set
containsAll(Collection <? Extends E>c)returns true if all elements in the given collection are present in the set
contains(object o)returns true if specified element presents in the set
isEmpty()returns true if no elements in the set
iterator()returns iterator over the elements in the set
remove (object o)remove the specified element if present in the set
removeAll(Collection <? Extends E>c)removes all elements in the given collection from the set
retainAll(Collection <? Extends E>c)retains all elements in the given collection in the set
size()returns the set size
spliterator()Creates a late-binding and fail-fast Spliterator over the elements in the set
hashcode()returns the hashcode value for set
toArray()returns an array for set elements
toArray(T[] a)returns an array for set elements with return type as array
The Set Interface tutorial in Java Collection

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
error: Content is protected !!