Maps
Quote
“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” — Linus Torvalds
Maps are a collection of key-value pairs.
block-beta
columns 2
block:keys
columns 1
k1["5"] k2["10"] k3["15"] k4["20"]
end
block:vals
columns 1
v1["3"] v2["6"] v3["5"] v4["8"]
end
k1 --> v1
k2 --> v2
k3 --> v3
k4 --> v4
Which one to use?¶
- HashMap is the fastest to iterate.
- Use LinkedHashMap when the insertion order matters.
- Use TreeMap when the natural order matters.
- Use WeakHashMap for in-memory caching.
- Use ConcurrentHashMap for thread-safe operations.