Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Java Collections Framework APIs

Tech 2

Core Collection Interfaces and Implementations

List Implementations

ArrayList provides a resizable array with O(1) random access performance:

import java.util.ArrayList;

List<String> items = new ArrayList<>();
items.add("first");
items.get(0);

LinkedList offers a doubly-linked list structure optimized for frequent insertions and removals at any position:

import java.util.LinkedList;

LinkedList<Integer> numbers = new LinkedList<>();
numbers.addFirst(10);
numbers.addLast(20);

Map Implementations

HashMap delivers O(1) average time complexity for put and get operations using hash-based storage:

import java.util.HashMap;

Map<String, Integer> cache = new HashMap<>();
cache.put("key", 42);
Integer value = cache.get("key");

TreeMap maintains entries sorted by keys using a Red-Black tree structure:

import java.util.TreeMap;

TreeMap<String, Double> sortedData = new TreeMap<>();
sortedData.put("banana", 2.5);
Map<String, Double> subset = sortedData.subMap("apple", "cherry");

Set Implementations

HashSet stores unique elements without duplicates using underlying HashMap:

import java.util.HashSet;

Set<String> uniqueNames = new HashSet<>();
uniqueNames.add("Alice");
boolean exists = uniqueNames.contains("Alice");

TreeSet maintains sorted unique elements with guaranteed O(log n) performance:

import java.util.TreeSet;

TreeSet<Integer> orderedNumbers = new TreeSet<>();
orderedNumbers.add(5);
Integer lowest = orderedNumbers.first();

Queue and Deque

PriorityQueue implements a min-heap for retrieving smallest element efficiently:

import java.util.PriorityQueue;

PriorityQueue<Integer> minHeap = new PriorityQueue<>();
minHeap.offer(30);
minHeap.offer(10);
int smallest = minHeap.poll(); // returns 10

Stack follows LIFO principles for element retrieval:

import java.util.Stack;

Stack<Character> charStack = new Stack<>();
charStack.push('A');
charStack.push('B');
char topElement = charStack.pop(); // returns 'B'

Queue interface supports FIFO operations:

import java.util.LinkedList;
import java.util.Queue;

Queue<String> taskQueue = new LinkedList<>();
taskQueue.offer("task1");
taskQueue.offer("task2");
String next = taskQueue.poll();

Deque enables insertion and removal from both ends:

import java.util.ArrayDeque;
import java.util.Deque;

Deque<Integer>两端队列 = new ArrayDeque<>();
两端队列.addFirst(1);
两端队列.addLast(2);
两端队列.removeFirst();

Thread-Safe Collection Alternatives

Synchronized Legacy Classes

Vector mirrors ArrayList functionality with synchronized methods ensuring thread safety:

import java.util.Vector;

Vector<Double> threadSafeList = new Vector<>();
threadSafeList.addElement(3.14);

Hashtable provides synchronized key-value storage with Enumeration support:

import java.util.Hashtable;

Hashtable<Long, String> safeTable = new Hashtable<>();
safeTable.put(1L, "value");

High-Concurrency Utilities

ConcurrentHashMap uses fine-grained locking for better throughput in concurrent scenarios:

import java.util.concurrent.ConcurrentHashMap;

ConcurrentHashMap<String, Object> concurrentMap = new ConcurrentHashMap<>();
concurrentMap.putIfAbsent("lock", new Object());

ConcurrentLinkedQueue implements a lock-free queue using CAS operations:

import java.util.concurrent.ConcurrentLinkedQueue;

ConcurrentLinkedQueue<String> messages = new ConcurrentLinkedQueue<>();
messages.offer("hello");
String msg = messages.poll();

CopyOnWriteArrayList creates a fresh copy on each modification, ideal for read-heavy workloads:

import java.util.concurrent.CopyOnWriteArrayList;

CopyOnWriteArrayList<Integer> snapshotList = new CopyOnWriteArrayList<>();
snapshotList.add(100);

CopyOnWriteArraySet mirrors the above behavior for sets:

import java.util.concurrent.CopyOnWriteArraySet;

CopyOnWriteArraySet<String> safeSet = new CopyOnWriteArraySet<>();
safeSet.add("concurrent");

ConcurrentSkipListMap provides sorted key-value pairs with concurrent access:

import java.util.concurrent.ConcurrentSkipListMap;

ConcurrentSkipListMap<String, Integer> sorted = new ConcurrentSkipListMap<>();
sorted.put("zebra", 26);
sorted.put("apple", 1);

ConcurrentSkipListSet combines set semantics with skip list ordering:

import java.util.concurrent.ConcurrentSkipListSet;

ConcurrentSkipListSet<Double> ordered = new ConcurrentSkipListSet<>();
ordered.add(1.5);
ordered.add(0.3);

Selection Criteria

Use Case Recommended Type
Frequent lookups by index ArrayList
Frequent insertions/deletions LinkedList
Key-value associations HashMap
Sorted unique elements TreeSet
Producer-consumer patterns ConcurrentLinkedQueue
Read-heavy with rare writes CopyOnWriteArrayList

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.