Set Collection In Java

Set

  1. Set implements the properties from collection interface.
  2. Set is use to store unique values.
  3. Set has multiple implemented classes.

 

HashSet

 

  1. HashSet class is implements the property from Set interface.
  2. HashSet dynamic in size.
  3. HashSet store different type of values.
  4. HashSet is use to store a unique value.
  5. HashSet is unorder and unsorted. Not maintain the insertion order.
  6. HashSet is based on Hashing algorithm.
  7. Its is use for faster searching.
  8. Default Capacity of HashSet is 16 and load factor is 0.75
  9. HashSet is a Set Collection In Java

 

Example:-

import java.util.HashSet;

public class HashSetDemo {

	public static void main(String[] args) {
		HashSet set = new HashSet();
		set.add(11);
		set.add(12);
		set.add("abc");
		set.add("xyz");
		set.add(10.32);
		set.add(11);
		set.add("end");
		
		System.out.println("Set is : "+set);
		
		set.remove(11);
		
		System.out.println("After remove 11 : "+set);	
	}
}

 

LinkedHashSet

  1. LinkedHashSet implements the property of Set Interface and it has HashSet as a parent class.
  2. LinkedHashSet is dynamic in size.
  3. LinkedHashSet can store different type of values.
  4. LinkedHashSet store unique values.
  5. LinkedHashSet is based on Hashing Algorithm and Doubly linked list algo.
  6. LinkedHashSet is order.

Example:-

import java.util.LinkedHashSet;

public class LinkedHashDemo {

	public static void main(String[] args) {
		LinkedHashSet set = new LinkedHashSet();
		set.add(11);
		set.add(12);
		set.add("Abc");
		set.add('A');
		set.add("Abc");
		set.add(98.45);
		System.out.println(set);
	}

}

 

TreeSet

  1. TreeSet implements the property from Set, SortedSet and NavigableSet interface.
  2. TreeSet is dynamic in size.
  3. It Store unique value
  4. It store values of similar data type.
  5. It is sorted by ascending order.
  6. It is based on balance tree algorithm.
  7. TreeSet is  a Set Collection In Java

Example:-

import java.util.TreeSet;

public class TreeSetDemo {

	public static void main(String[] args) {
		TreeSet set = new TreeSet();
		set.add(11);
		set.add(12);
		set.add(20);
		set.add(18);
		set.add(6);
		set.add(98);
		System.out.println(set);
		
		System.out.println("Higher : "+ set.higher(15));
		System.out.println("Lower"+ set.lower(15));
		
		System.out.println("Ceiling : "+ set.ceiling(14));
		System.out.println("Floor : "+ set.floor(14));
		
		System.out.println(set.descendingSet());
		
		System.out.println("First : "+ set.first());
		System.out.println("Last : "+ set.last());
	}

}

 

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Set Collection In Java

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !