Java HashSet and LinkedList
The Collections framework has many types. We already saw ArrayList and HashMap. Here are three more which you will use often.
HashSet: unique values
A Set does not allow duplicates. HashSet is fast, but has no order.
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Set<String> cities = new HashSet<>();
System.out.println(cities.add("Delhi"));
System.out.println(cities.add("Mumbai"));
System.out.println(cities.add("Delhi")); // false: duplicate, not added
System.out.println(cities.size());
System.out.println(cities.contains("Mumbai"));
Set<Integer> sorted = new TreeSet<>(java.util.List.of(5, 3, 9, 3, 1, 5));
System.out.println(sorted); // sorted and unique
}
}
Output
true
true
false
2
true
[1, 3, 5, 9]
Remove duplicates from a list
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
public class Dup {
public static void main(String[] args) {
List<Integer> numbers = List.of(4, 7, 4, 1, 7, 9, 1);
List<Integer> unique = new ArrayList<>(new LinkedHashSet<>(numbers)); // keeps the order
System.out.println(unique);
}
}
Output
[4, 7, 1, 9]
Set operations
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Ops {
public static void main(String[] args) {
Set<Integer> a = new TreeSet<>(Set.of(1, 2, 3, 4));
Set<Integer> b = new TreeSet<>(Set.of(3, 4, 5, 6));
Set<Integer> union = new TreeSet<>(a);
union.addAll(b);
Set<Integer> common = new TreeSet<>(a);
common.retainAll(b);
Set<Integer> onlyA = new TreeSet<>(a);
onlyA.removeAll(b);
System.out.println("Union: " + union);
System.out.println("Common: " + common);
System.out.println("Only in a: " + onlyA);
}
}
Output
Union: [1, 2, 3, 4, 5, 6]
Common: [3, 4]
Only in a: [1, 2]
LinkedList, Queue and Deque
LinkedList is a list where every item points to the next one. It is also a Queue (first in, first out) and a Deque (a queue with two ends, which can work as a stack).
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
public class Link {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("first");
queue.offer("second");
queue.offer("third");
System.out.println(queue.peek()); // look at the head
System.out.println(queue.poll()); // take the head
System.out.println(queue);
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.pop()); // last in, first out
System.out.println(stack);
}
}
Output
first
first
[second, third]
3
[2, 1]
Which one to use?
| Need | Use |
|---|---|
| A list with fast reading by index | ArrayList |
| Unique values, fast check | HashSet |
| Unique values, sorted | TreeSet |
| Unique values, keep the insert order | LinkedHashSet |
| A queue (first in, first out) | ArrayDeque or LinkedList |
| A stack (last in, first out) | ArrayDeque |
| Key and value pairs | HashMap |
Note:
For a stack or a queue, ArrayDeque is usually faster than LinkedList. The old Stack class should not be used in new code. Next: Java Generics.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |