Java LinkedHashSet ClassJava LinkedHashSet class is a Hashtable and Linked list implementation of the Set interface. It inherits the HashSet class and implements the Set interface. The important points about the Java LinkedHashSet class are:
Note: Keeping the insertion order in the LinkedHashset has some additional costs, both in terms of extra memory and extra CPU cycles. Therefore, if it is not required to maintain the insertion order, go for the lighter-weight HashMap or the HashSet instead.Hierarchy of LinkedHashSet classThe LinkedHashSet class extends the HashSet class, which implements the Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order. LinkedHashSet Class DeclarationLet's see the declaration for java.util.LinkedHashSet class. Constructors of Java LinkedHashSet Class
Java LinkedHashSet ExampleLet's see a simple example of the Java LinkedHashSet class. Here you can notice that the elements iterate in insertion order. FileName: LinkedHashSet1.java Output: One Two Three Four Five Note: We can also use the enhanced for loop for displaying the elements.Java LinkedHashSet example ignoring duplicate ElementssFileName: LinkedHashSet2.java Output: Ravi Vijay Ajay Remove Elements Using LinkeHashSet ClassFileName: LinkedHashSet3.java Output: The hash set is: [Java, T, Point, Good, Website] true After removing the element, the hash set is: [Java, T, Point, Website] false Java LinkedHashSet Example: BookFileName: Book.java Output: 101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications & Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6 Next TopicJava TreeSet class |