Java Set Interface
We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates.
- HashSet: This class stores its elements in hash tables. It uses the hashCode() method to find the objects. This class has the benefit that addition and checking the presence of an element has constant time. The disadvantage is that we lose the order of insertion of the elements.
- LinkedHashSet: Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It inherits the HashSet class and implements the Set interface.
- TreeSet: This class stores its elements in a sorted tree structure. The benefit is that elements are always in sorted order, but addition and checking the presence of an element takes longer time O(log n). TreeSet implements a special interface called NavigableSet.
Useful Methods
Modifier and Type |
Method |
Description |
boolean |
add(E e) |
It adds the specified element to this set if it is not already present (optional operation). |
boolean |
addAll(Collection<? extends E> c) |
It adds all of the elements in the specified collection to this set if they're not already present (optional operation). |
void |
clear() |
It removes all of the elements from this set (optional operation). |
boolean |
contains(Object o) |
It returns true if this set contains the specified element. |
boolean |
remove(Object o) |
It removes the specified element from this set if it is present (optional operation). |
boolean |
equals(Object o) |
It compares the specified object with this set for equality. |
int |
hashCode() |
It returns the hash code value for this set. |
boolean |
isEmpty() |
It returns true if this set contains no elements. |
Iterator<E> |
iterator() |
It returns an iterator over the elements in this set. |
Example: HashSet
import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class SetExample { public static void main(String[] args) { Set<String> setType = new HashSet<String>(); setType.add("INDIA"); setType.add("CHINA"); setType.add("PAKISTAN"); // access via Iterator Iterator<String> iterator = setType.iterator(); while (iterator.hasNext()) { String element = (String) iterator.next(); System.out.println(element); } } }
Output:
CHINA PAKISTAN INDIA
Example: LinkedHashSet
import java.util.LinkedHashSet; public class MyClass { public static void main(String[] args) { LinkedHashSet<Integer> linkedset = new LinkedHashSet<Integer>(); // Adding element to LinkedHashSet linkedset.add(10); linkedset.add(40); linkedset.add(30); linkedset.add(20); // This will not add new element as 40 already exists linkedset.add(40); linkedset.add(11); System.out.println("Size of LinkedHashSet = " + linkedset.size()); System.out.println("Original LinkedHashSet:" + linkedset); System.out.println("Removing 10 from LinkedHashSet: " + linkedset.remove(10)); System.out.println("Trying to Remove 100 which is not " + "present: " + linkedset.remove(100)); System.out.println("Checking if 30 is present=" + linkedset.contains(30)); System.out.println("Updated LinkedHashSet: " + linkedset); } }Output:
Size of LinkedHashSet = 5 Original LinkedHashSet:[10, 40, 30, 20, 11] Removing 10 from LinkedHashSet: true Trying to Remove 100 which is not present: false Checking if 30 is present=true Updated LinkedHashSet: [40, 30, 20, 11]
Example: TreeSet
import java.util.TreeSet; public class MyClass { public static void main(String[] args) { TreeSet<Integer> treeSet= new TreeSet<>(); treeSet.add(100); treeSet.add(50); treeSet.add(200); // Duplicates will not get insert treeSet.add(50); // Elements get stored in default natural // Sorting Order(Ascending) System.out.println(treeSet); // [50,100,200] // ts1.add("Abc") ; will throw ClassCastException at run time } }
Output:
[50, 100, 200]