The most heavily used utility package. Provides the entire Collections Framework (ArrayList, HashMap, LinkedList, TreeMap), as well as Optional, Random, Scanner, and date/time utilities.
Import: import java.util.*; | Docs: Oracle JavaDoc
| Name | Signature | Description |
|---|---|---|
| ArrayList | class ArrayList<E> implements List<E> | Resizable array — O(1) random access |
| list.add | boolean add(E e) | Append element to end |
| list.get | E get(int index) | Get element at index |
| list.size | int size() | Return number of elements |
| list.remove | E remove(int index) | Remove element at index |
| list.contains | boolean contains(Object o) | True if list contains o |
| list.sort | void sort(Comparator<? super E> c) | Sort list (pass null for natural order) |
| Collections.sort | static <T> void sort(List<T> list) | Sort list using natural ordering |
| Collections.shuffle | static void shuffle(List<?> list) | Randomly shuffle list |
| Collections.reverse | static void reverse(List<?> list) | Reverse list in place |
| Collections.max | static <T> T max(Collection<? extends T> coll) | Return maximum element |
| HashMap | class HashMap<K,V> implements Map<K,V> | Hash table key-value store — O(1) average |
| map.put | V put(K key, V value) | Associate key with value |
| map.get | V get(Object key) | Return value for key, or null |
| map.getOrDefault | V getOrDefault(Object key, V defaultValue) | Return value or default if absent (Java 8+) |
| map.containsKey | boolean containsKey(Object key) | True if map contains key |
| map.remove | V remove(Object key) | Remove key-value pair |
| map.entrySet | Set<Map.Entry<K,V>> entrySet() | Return set of key-value pairs for iteration |
| map.keySet | Set<K> keySet() | Return set of all keys |
| LinkedList | class LinkedList<E> | Doubly linked list — O(1) insert/delete at ends |
| TreeMap | class TreeMap<K,V> | Sorted map (red-black tree) — O(log n) |
| HashSet | class HashSet<E> | Unordered set — O(1) add/contains |
| TreeSet | class TreeSet<E> | Sorted set — O(log n) |
| Optional | class Optional<T> | Container that may or may not hold a value (Java 8+) |
| Optional.of | static <T> Optional<T> of(T value) | Create non-empty Optional |
| Optional.empty | static <T> Optional<T> empty() | Create empty Optional |
| Optional.ofNullable | static <T> Optional<T> ofNullable(T value) | Create Optional (null becomes empty) |
| opt.isPresent | boolean isPresent() | True if value is present |
| opt.get | T get() | Return value (throws if empty) |
| opt.orElse | T orElse(T other) | Return value or other if empty |
| Scanner | class Scanner | Simple text scanner for parsing primitives and strings |
| scanner.nextInt | int nextInt() | Read next int from input |
| scanner.nextLine | String nextLine() | Read entire line |
| scanner.nextDouble | double nextDouble() | Read next double |
| scanner.hasNextLine | boolean hasNextLine() | True if more input lines exist |
| Random | class Random | Pseudo-random number generator |
| random.nextInt | int nextInt(int bound) | Return random int in [0, bound) |
| random.nextDouble | double nextDouble() | Return random double in [0.0, 1.0) |
Save as ClassName.java (matching the public class name), compile with javac ClassName.java, run with java ClassName.
import java.util.*;
import java.util.stream.Collectors;
public class JavaUtilDemo {
public static void main(String[] args) {
// --- ArrayList ---
List<String> fruits = new ArrayList<>(Arrays.asList(
"banana", "apple", "cherry", "date", "elderberry"));
Collections.sort(fruits);
System.out.println("Sorted: " + fruits);
fruits.removeIf(f -> f.length() > 5);
System.out.println("Short names: " + fruits);
// --- HashMap: word frequency ---
String text = "go java go python java go rust java";
Map<String, Integer> freq = new HashMap<>();
for (String word : text.split(" "))
freq.merge(word, 1, Integer::sum);
freq.entrySet().stream()
.sorted(Map.Entry.<String,Integer>comparingByValue().reversed())
.forEach(e -> System.out.printf(" %-10s %d%n", e.getKey(), e.getValue()));
// --- Optional ---
Optional<String> opt = freq.keySet().stream()
.filter(k -> k.startsWith("j"))
.findFirst();
System.out.println("First 'j' lang: " + opt.orElse("none"));
// --- Scanner from String ---
Scanner sc = new Scanner("42 3.14 hello world");
System.out.println("int: " + sc.nextInt());
System.out.println("double: " + sc.nextDouble());
System.out.println("words: " + sc.next() + " " + sc.next());
// --- Random ---
Random rng = new Random(42);
System.out.print("Random ints: ");
for (int i = 0; i < 5; i++)
System.out.print(rng.nextInt(100) + " ");
System.out.println();
}
}
// javac JavaUtilDemo.java && java JavaUtilDemo