Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

LinkedHashSet → Java LinkedHashSet


Java LinkedHashSet

LinkedHashSet in Java collection framework

The LinkedHashSet class in Java provides a hybrid implementation that combines the characteristics of a HashSet and a LinkedList. Hashing: Similar to HashSet, elements are stored using a hashing mechanism for efficient average time complexity of add, remove, and contains operations. Linked List Ordering: Elements are maintained in a linked list structure, preserving the order in which they were added. This is unlike HashSet which doesn't guarantee any specific order.

Characteristics of LinkedHashSet

Insertion order is preserved: Elements are added to the end of the internal linked list, reflecting the order of insertion. Duplicates are not allowed: Like HashSet, it doesn't allow duplicate elements based on the hashCode() and equals() methods. Slower than HashSet for some operations: Maintaining order adds some overhead compared to a regular HashSet.

Initializing and Declaring LinkedHashSet:

Importing syntax - LinkedHashSet import java.util.LinkedHashSet;
This line imports the LinkedHashSet class from the java.util package.
Declaration syntax LinkedHashSet<DataType> setName;
Explanation : LinkedHashSet: This specifies the class you're using. <DataType>: Placeholder for the data type the set will store. setName: The name for your reference variable.
Initialize a LinkedHashSet Creating an empty LinkedHashSet:
Creating LinkedHashSet LinkedHashSet<String> countries = new LinkedHashSet<>();

Creating a LinkedHashSet with initial elements (Collection initializer):
Creating a LinkedHashSet with initial elements LinkedHashSet<Integer> numbers = new LinkedHashSet<>() {{ add(10); add(20); add(30); }};

Simple Examples demonstrating LinkedHashSet

1. String LinkedHashSet
LinkedHashSet example in java with string elements import java.util.LinkedHashSet; public class Main { public static void main(String[] args) { // Create a LinkedHashSet to store Strings LinkedHashSet<String> fruits = new LinkedHashSet<String>(); // Add elements (maintains insertion order) fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); // Print the contents of the set System.out.println(fruits); // Access the first element (based on insertion order) String firstFruit = fruits.getFirst(); System.out.println("First fruit: " + firstFruit); // Iterate through elements in the order they were added for (String fruit : fruits) { System.out.println(fruit); } } }


[Apple, Banana, Orange] First fruit: Apple Apple Banana Orange

2. Integer LinkedHashSet
LinkedHashSet simple example in java import java.util.LinkedHashSet; public class Main { public static void main(String[] args) { // Create a LinkedHashSet to store Integers LinkedHashSet<Integer> numbers = new LinkedHashSet<Integer>(); // Add elements (maintains insertion order) numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(10); // Duplicates are ignored // Print the contents of the set System.out.println(numbers); // Access the last element added int lastNumber = numbers.getLast(); System.out.println("Last number: " + lastNumber); } }


Output: [10, 20, 30] Last number: 30

3. Custom Object LinkedHashSet:
LinkedHashSet example with objects as elements import java.util.LinkedHashSet; class Product { private String name; private double price; public void setName(String name){; } public String getName(){ return name; } public void setPrice(double price){ this.price=price; } public double getPrice(){ return price; } } public class Main { public static void main(String[] args) { // Create a LinkedHashSet to store Product objects LinkedHashSet<Product> products = new LinkedHashSet<Product>(); // Create Product objects and add them to the set (maintains order) Product p1 = new Product(); p1.setName("Shirt"); p1.setPrice(19.99); products.add(p1); Product p2 = new Product(); p2.setName("Hat"); p2.setPrice(14.50); products.add(p2); // Iterate through products in the order they were added for (Product product : products) { System.out.println(product.getName()); } } }


Shirt Hat

Remember, although LinkedHashSet allows storing different data types, it's generally recommended to use a generic type like <String> or <Integer> for better type safety and code clarity.