08 Common Containers
January 10, 2024 · 2 min read
If you have any questions, feel free to comment below. Click the block can copy the code.
And if you think it's helpful to you, just click on the ads which can support this site. Thanks!
8.1 List #
Interface: java.util.List<>.
Implementations:
java.util.ArrayList<>: resizable arrayjava.util.LinkedList<>: doubly linked list
Methods:
add(): add an element at the endclear(): clear the listsize(): return its lengthisEmpty(): check whether it is emptyget(i): get theith elementset(i, val): set theith element toval
Loop syntax:
// Standard
for(int i = 0; i < list.size(); i ++) {
System.out.println(list.get(i));
}
// Enhanced
for (Integer integer : list) {
System.out.println(integer);
}
8.2 Stack #
Note that, among these implementations, only the stack is a class rather than an interface.
Class: java.util.Stack<>
Methods:
push(): push an elementpop(): remove and return the top elementpeek(): return the top elementsize(): return its lengthempty(): check whether the stack is emptyclear(): clear the stack
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stk = new Stack<>();
stk.push(1);
stk.push(2);
System.out.println(stk.pop());
System.out.println(stk.peek());
}
}
8.3 Queue #
Interface: java.util.Queue<>
Implementations: Note: there is no ArrayList implementation.
java.util.LinkedList<>: doubly linked listjava.util.PriorityQueue<>: priority queue- A min-heap by default; for a max-heap, use
new PriorityQueue<>(Collections.reverseOrder())
- A min-heap by default; for a max-heap, use
Methods:
add(): add an element at the rear of the queueremove(): remove and return the front elementisEmpty(): check whether it is emptysize(): return its lengthpeek(): return the front elementclear(): clear the queue
public class Main {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(1);
q.add(2);
System.out.println(q.remove());
System.out.println(q.peek());
}
}
8.4 Set #
Interface: java.util.Set<K>
Implementations:
java.util.HashSet<K>: hash table (not necessarily ordered)java.util.TreeSet<K>: balanced tree (always ordered; implemented with a red-black tree)
Methods:
add(): add an elementcontains(): check whether it contains an elementremove(): remove an elementsize(): return the number of elementsisEmpty(): check whether it is emptyclear(): clear the set
Additional methods of java.util.TreeSet:
ceiling(key): return the smallest element greater than or equal tokey, ornullif none existsfloor(key): return the largest element less than or equal tokey, ornullif none exists
public class Main {
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
set.add(161);
set.add(222);
set.add(160);
for(int x : set) {
System.out.println(x);
}
}
}
8.5 Map #
Interface: java.util.Map<K, V>
Implementations:
java.util.HashMap<K, V>: hash table (not necessarily ordered)java.util.TreeMap<K, V>: balanced tree (always ordered; implemented with a red-black tree)
Methods:
put(key, value): add a key and its corresponding valueget(key): return the value corresponding to a keycontainsKey(key): check whether it contains a keyremove(key): remove a keysize(): return the number of elementsisEmpty(): check whether it is emptyclear(): clear the mapentrySet(): get the set of all entries in theMapMap.Entry<K, V>: the type of an entry in theMapgetKey(): get the keygetValue(): get the value
public class Main {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("123", 1);
map.put("345", 2);
map.put("678", 6);
System.out.println(map.containsKey("123"));
for(Map.Entry<String, Integer> entry : map.entrySet())
System.out.printf("%s %d\n", entry.getKey(), entry.getValue());
}
}
Additional methods of java.util.TreeMap<K, V>:
ceilingEntry(key): return the smallest entry with a key greater than or equal tokey, ornullif none existsfloorEntry(key): return the largest entry with a key less than or equal tokey, ornullif none exists
public class Main {
public static void main(String[] args) {
TreeMap<Integer, Integer> map = new TreeMap<>();
map.put(123, 1);
map.put(345, 2);
map.put(678, 6);
Map.Entry<Integer, Integer> down = map.floorEntry(124);
System.out.println(down);
}
}
Correspondences with C++:
List -----> list
Queue ----> queue
PriorityQueue ---> priority_queue
Stack ----> stack
TreeSet ----> set
HashSet ----> unordered_set
TreeMap ----> map
HashMap ----> unordered_map
Related readings
If you want to follow my updates, or have a coffee chat with me, feel free to connect with me: