LinkedHashSet Class: ==================== -> LinkedHashSet is a sub-class of HashSet class. Syntax: public LinkedHashSet extends HashSet{ } -> The LinkedHashSet Object is same as HashSet Object. -> While creating the LinkedHashSet Object, like HashSet this can internally create LinkedHashMap Object with the internal capacity or 16 and load factor of 0.75. -> If we can add the elements into LinkedHashSet Object which equals to (default Capacity * load Factor), then the bucket size can be doubled. HashSet Vs LinkedHashSet: ========================== HashSet is unordered data collection and cannot preserve the insertion order and duplication is not allowed. LinkedHashSet is also unordered but it can preserve the insertion order and duplication not allowed.. Q: How the insertion order of elements in the LinkedHashSet object can be preserved? ===================================================================================== -> When we can add an element into the LinkedHashSet Object, then internally the LinkedHashMap Object can be invoked and calculate the hashCode for that element and also calculate the bucket index. According to the bucket Index, that element can write into that LinkedHashMap table. -> Here: the LinkedHashMap table contains Nodes with three parts in each bucket. 1) Previous 2) Data 3) next -> when we have added the first element, the previous can set with "null" and next also set with "null". -> when we ca add the second element into this LinkedHashSet object, then the element can store in the bucket according to the derived bucket index value. -> now, the next part of element-1 (node1) can start to point the previous part of second element (node-2). -> This is exactly as "doubly linked list. That is the reason the LinkedHashSet object can preserve the insertion order. package p1; import java.util.HashSet; import java.util.LinkedHashSet; public class MainClass { public static void main(String[] args) { // Creating the LinkedHashSet LinkedHashSet toDoList = new LinkedHashSet<>(); HashSet x = new HashSet<>(); System.out.println("The Empty LinkedHashSet = "+toDoList); toDoList.add("Buy Groceries"); toDoList.add("Finish Project"); toDoList.add("Call Mother"); System.out.println(toDoList.add("Buy Groceries")); toDoList.add("Attend Team Meeting"); for(String s:toDoList) { System.out.println(s); } // checking of specified task belonging to the given LinkedHashSet or not. if(toDoList.contains("Call Mother")) { System.out.println("The Given Task is in the given To Do List."); } } } ===================================================== package pack.p1; import java.util.LinkedHashSet; public class MainClass { public static void main(String[] args) { LinkedHashSet userSessionHistory = new LinkedHashSet<>(); userSessionHistory.add("Login at 9.05 am"); userSessionHistory.add("View Dashboard at 9.30 am"); userSessionHistory.add("Change Password at 9.35 am"); userSessionHistory.add("Logout at 9.40 am"); userSessionHistory.add("Login at 9.45 am"); for(String activity:userSessionHistory) { System.out.println(activity); } } }