Set
- Set implements the properties from collection interface.
- Set is use to store unique values.
- Set has multiple implemented classes.
HashSet
- HashSet class is implements the property from Set interface.
- HashSet dynamic in size.
- HashSet store different type of values.
- HashSet is use to store a unique value.
- HashSet is unorder and unsorted. Not maintain the insertion order.
- HashSet is based on Hashing algorithm.
- Its is use for faster searching.
- Default Capacity of HashSet is 16 and load factor is 0.75
- 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
- LinkedHashSet implements the property of Set Interface and it has HashSet as a parent class.
- LinkedHashSet is dynamic in size.
- LinkedHashSet can store different type of values.
- LinkedHashSet store unique values.
- LinkedHashSet is based on Hashing Algorithm and Doubly linked list algo.
- 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
- TreeSet implements the property from Set, SortedSet and NavigableSet interface.
- TreeSet is dynamic in size.
- It Store unique value
- It store values of similar data type.
- It is sorted by ascending order.
- It is based on balance tree algorithm.
- 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
Job’s For Fresher