Welcome to our Java LinkedHashSet tutorial! In this lesson, we'll dive deep into understanding LinkedHashSet, a special kind of Set in Java. We'll cover the basics, real-world examples, and advanced applications. By the end of this lesson, you'll be able to use LinkedHashSet with confidence! 🚀
LinkedHashSet is a type of Set in Java, extending the abstract HashSet class. Unlike a regular Set, LinkedHashSet maintains the insertion order of its elements. This means that when you add elements to a LinkedHashSet, they will be stored in the order they were added.
LinkedHashSet comes in handy when you need to work with collections that have a specific order, like maintaining the order of items in a shopping cart or the timeline of events in a log file.
To create a LinkedHashSet, you can use the LinkedHashSet constructor, which takes an optional collection as an argument:
import java.util.LinkedHashSet;
LinkedHashSet<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");Since LinkedHashSet implements the Iterable interface, you can iterate through its elements using a for-each loop:
for (String fruit : fruits) {
System.out.println(fruit);
}The size() method returns the number of elements in the LinkedHashSet:
int size = fruits.size(); // Returns 3The isEmpty() method checks if the LinkedHashSet is empty or not:
boolean isEmpty = fruits.isEmpty(); // Returns falseThe contains() method checks if a specified element exists in the LinkedHashSet:
boolean containsApple = fruits.contains("Apple"); // Returns trueThe remove() method removes a specified element from the LinkedHashSet:
fruits.remove("Banana");What does the LinkedHashSet class extend in Java?
Let's create a simple LinkedHashSet-based implementation of a LRU (Least Recently Used) cache. This cache will store a maximum of 3 items, and when adding a new item, it will remove the least recently used item.
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
class LRUCache {
private final int capacity;
private final LinkedHashMap<Integer, Integer> cache;
private final LinkedHashSet<Integer> keys;
public LRUCache(int capacity) {
this.capacity = capacity;
this.cache = new LinkedHashMap<Integer, Integer>() {
private static final long serialVersionUID = 1L;
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
};
this.keys = new LinkedHashSet<>(capacity);
}
public void put(int key, int value) {
if (keys.contains(key)) {
// If the key already exists, update the value and remove it from the keys set
cache.put(key, value);
keys.remove(key);
} else {
// If the key doesn't exist, add it to the cache and keys sets
cache.put(key, value);
keys.add(key);
}
}
public Integer get(int key) {
return cache.get(key);
}
public void display() {
for (Integer key : keys) {
System.out.println("Key: " + key + ", Value: " + cache.get(key));
}
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(3);
cache.put(1, 1);
cache.put(2, 2);
cache.put(3, 3);
cache.put(4, 4); // This will remove the least recently used item (Key: 1, Value: 1)
cache.display();
}
}This implementation demonstrates a practical use of LinkedHashSet in a real-world application.
That's all for our Java LinkedHashSet tutorial! Now you're well-equipped to use LinkedHashSet in your own projects. Happy coding! 🤖