Recent Tutorials and Articles
    Introduction to Set in Java
    Published on: 2018-05-25 16:05:38

    This tutorial introduces you to Set interface and it's implementation classes such as HashSet, LinkedHashSet and TreeSet along with explaining their characteristics and properties.

    Since this tutorial is using Generics so it is expected that reader has got decent knowledge of Generics. If you are new to Generics, you are recommended to read this tutorial first - Introduction to Generics

     

    Set interface in Java is an un-ordered Collection of objects (it extends Collection interface) and has got following specifications -

    1. Does not maintain insertion order i.e. objects are not stored in the order they are inserted in Set. For the same reason, it is unable to provide positional access.
    2. Can not contain duplicate elements

    As we know Set is an interface and we obviously need to have its implementation in order to use its features. Following are 3 basic implementations of Set that Java collections provide us with -

    1. HashSet
      • Uses HashMap internally to store its elements
      • Can not contain duplicates
      • Permits null
      • Not synchronized i.e. should not be used in scenario where multiple threads could be accessing it simultaneously
    2. LinkedHashSet
      • Extends HashSet
      • Uses LinkedHashMap internally to store its elements
      • Maintains insertion order. In case, an element is added again, the order of that element remains unchanged
      • Permits null
      • Not synchronized i.e. should not be used in scenario where multiple threads could be accessing it simultaneously
    3. TreeSet
      • Uses TreeMap internally to store its elements
      • Implements SortedSet interface thereby providing sorting of elements by means of comparable which means elements to be added to TreeSet must implement Comparable interface or provide Comparator interface implementation while initializing TreeSet
      • Implements NavigableSet interface providing useful navigation methods for searching, manipulating and filtering
      • Does not permit null
      • Not synchronized i.e. should not be used in scenario where multiple threads could be accessing it simultaneously

    As a best practice, we should always use the reference variables for Set interface as it makes it easy to switch from one implementation to another implementation of Set inteface.

    SetInterfaceDemo.java
    package com.sts.allprogrammingtutorials.collections;
    
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.Set;
    import java.util.TreeSet;
    
    public class SetInterfaceDemo {
    
    	public static void main(String[] args) {
    
    		// Declaring a set using Set interface and creating an instance of
    		// HashSet
    		Set<String> strings = new HashSet<String>();
    
    		// adding elements to a set
    		strings.add("Amit");
    		strings.add("Tarun");
    		strings.add("Sumit");
    		strings.add("Lokesh");
    
    		System.out.println("HashSet maintains no order");
    		System.out.println("Result for HashSet is: - " + strings);
    
    		// Creating an instance of LinkedHashSet using same Set reference
    		strings = new LinkedHashSet<String>();
    
    		// adding elements to a set
    		strings.add("Amit");
    		strings.add("Tarun");
    		strings.add("Sumit");
    		strings.add("Lokesh");
    
    		System.out.println("LinkedHashSet maintains Insertion order");
    		System.out.println("Result for LinkedHashSet is: - " + strings);
    
    		// Creating an instance of TreeSet using same Set reference
    		strings = new TreeSet<String>();
    
    		// adding elements to a set
    		strings.add("Amit");
    		strings.add("Tarun");
    		strings.add("Sumit");
    		strings.add("Lokesh");
    
    		System.out.println("TreeSet maintains Ascending order");
    		System.out.println("Result for TreeSet is: - " + strings);
    
    	}
    
    }
    
    
    Output:
    HashSet maintains no order
    Result for HashSet is: - [Tarun, Sumit, Amit, Lokesh]
    LinkedHashSet maintains Insertion order
    Result for LinkedHashSet is: - [Amit, Tarun, Sumit, Lokesh]
    TreeSet maintains Ascending order
    Result for TreeSet is: - [Amit, Lokesh, Sumit, Tarun]
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 2018-05-25 16:05:38

    Comment Form is loading comments...