Master java skills

Set in Java

Set stores only unique elements as it is a collection of unique elements. No duplicates are allowed unlike List. Below is the hierarchy of Set interface in collections framework

HashSet

One of the implementation class of Set interface is HashSet. It stores elements using a mechanism called hashing. Below are some important features of hash set.

  1. Stores only unique elements
  2. Uses hashing technique to store elements
  3. It allows one null value
  4. Doesn’t maintain insertion order of elements
  5. elements are stored according to their hash codes
  6. Initial capacity is 16 with load factor 0.75

Creating and iterating over HashSet

In the below example, we are adding some duplicate elements, but hash set will keep only a single copy of duplicate element.

package com.javatrainingschool;

import java.util.HashSet;
import java.util.Set;

public class HashSetExample {

	public static void main(String[] args) {
		
		Set<Integer> set = new HashSet<Integer>();
		
		set.add(100);
		set.add(300);
		set.add(400);
		set.add(300);
		set.add(500);
		set.add(500); 
		
		System.out.println("Size of the set is : " + set.size());
		
		for(int value : set) {
			System.out.println(value);
		}
	}
}
Output :
Size of the set is : 4
400
100
500
300

Remove methods in Set

//Below method will remove the specified object
public boolean remove(Object o);

boolean removeAll(Collection<?> c);

//Below method will clear the HashSet
public void clear();

Contains method in HashSet

This method checks if the passed object exist in the hashset or not. Below is the syntax

public boolean contains(Object o);

LinkedHashSet

This class maintains insertion order of elements in the set. So, the first element inserted in the set becomes the first element in the set and the last inserted element becomes last element in the set. Let’s understand it using one example

package com.javatrainingschool;

import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetExample {

	public static void main(String[] args) {
		
		Set<Integer> set = new LinkedHashSet<Integer>();
		set.add(100);
		set.add(100);
		set.add(200);
		set.add(300);
		set.add(300);
		set.add(400);
		
		System.out.println(set);
	}
}
Output :
[100, 200, 300, 400]

TreeSet

TreeSet class maintains natural sorting order of elements. Meaning if TreeSet contains integer values, then least value will be the first element in the TreeSet, and the highest value will become the last.

package com.javatrainingschool;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetExample {
	
	public static void main(String[] args) {
		
		Set<Integer> set = new TreeSet<Integer>();
		
		set.add(500);
		set.add(100);
		set.add(700);
		set.add(400);
		set.add(900);
		set.add(900);
		
		for(int value : set) {
			System.out.println("Value : " + value);
		}
	}
}
Output :
Value : 100
Value : 400
Value : 500
Value : 700
Value : 900

Create a HashSet using ArrayList

We can obtain a set from a list. All we need to do is, pass the list in the constructor of HashSet class as a paratemeter. Let’s see one example

package com.javatrainingschool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashSetFromArrayListExample {
	
	public static void main(String[] args) {
		
		List<Integer> list = new ArrayList<Integer>();
		list.add(100);
		list.add(200);
		list.add(300);
		list.add(400);
		list.add(500);
		list.add(300);
		list.add(200);
		list.add(300);
		
		Set<Integer> set = new HashSet<Integer>(list);
		
		System.out.println("List is :" + list);
		System.out.println("Set is : " + set);
	}
}
Output :
List is :[100, 200, 300, 400, 500, 300, 200, 300]
Set is : [400, 100, 500, 200, 300]