java.util Package
Collection framework classes and utility functions.
ArrayList<E>
Resizable array implementation of the List interface.
Constructor
Constructs an empty list with initial capacity of 10.
Methods
Appends the element to the end. Returns true.
Returns the element at the specified position.
Replaces the element at the specified position. Returns the old element.
Removes and returns the element at the specified position.
Returns the number of elements.
Returns true if the list contains no elements.
Returns true if the list contains the element.
Returns the index of the first occurrence, or -1 if not found.
Removes all elements from the list.
Returns a sequential stream over the elements.
ArrayList<String> list = new ArrayList<>();
list.add("Alice");
list.add("Bob");
list.add("Charlie");
System.out.println(list.get(1)); // "Bob"
System.out.println(list.size()); // 3
LinkedList<E>
Doubly-linked list implementation.
Constructor
Constructs an empty linked list.
Methods
Appends element to the end.
Inserts element at the beginning.
Appends element to the end.
Returns element at the specified position.
Returns the first element. Throws if empty.
Returns the last element. Throws if empty.
Replaces element at position. Returns old element.
Removes and returns element at position.
Removes and returns the first element.
Removes and returns the last element.
Returns the number of elements.
Returns true if the list is empty.
Returns true if the list contains the element.
Removes all elements.
HashMap<K, V>
Hash table implementation of the Map interface.
Constructor
Constructs an empty HashMap with default capacity (16) and load factor (0.75).
Methods
Associates the key with the value. Returns the previous value, or null.
Returns the value for the key, or null if not found.
Returns true if the map contains the key.
Removes the mapping for the key. Returns the previous value.
Returns the number of key-value mappings.
Returns true if the map contains no mappings.
Removes all mappings.
HashMap<String, Integer> map = new HashMap<>();
map.put("Alice", 25);
map.put("Bob", 30);
System.out.println(map.get("Alice")); // 25
System.out.println(map.containsKey("Bob")); // true
HashSet<E>
Hash table implementation of the Set interface. No duplicate elements.
Constructor
Constructs an empty HashSet.
Methods
Adds the element if not already present. Returns true if added.
Removes the element if present. Returns true if removed.
Returns true if the set contains the element.
Returns the number of elements.
Returns true if the set is empty.
Removes all elements.
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate, not added
System.out.println(set.size()); // 2
TreeSet<E>
Red-Black tree implementation. Elements are stored in sorted order.
Constructor
Constructs an empty TreeSet with natural ordering.
Methods
Adds the element in sorted order. Returns true if added.
Removes the element. Returns true if removed.
Returns true if the set contains the element.
Returns the lowest (first) element.
Returns the highest (last) element.
Returns the number of elements.
Returns true if the set is empty.
Removes all elements.
Returns elements as an array in sorted order.
TreeSet<Integer> set = new TreeSet<>();
set.add(5);
set.add(1);
set.add(3);
System.out.println(set.first()); // 1
System.out.println(set.last()); // 5
Arrays
Static utility methods for arrays.
Sorting
Sorts the array in ascending order using quicksort.
Searching
Searches using binary search. Returns index if found, or negative insertion point if not found.
Fill
Assigns the specified value to each element.
Copy
Copies the array, truncating or padding with nulls.
Copies a range of the array.
Comparison
Returns true if the two arrays are equal (same length and elements).
Conversion
Returns a string representation: "[a, b, c]".
Returns a fixed-size list backed by the array.
Integer[] nums = {5, 2, 8, 1, 9};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums)); // [1, 2, 5, 8, 9]
int index = Arrays.binarySearch(nums, 5);
System.out.println(index); // 2
List<Integer> list = Arrays.asList(1, 2, 3);
System.out.println(list); // [1, 2, 3]
Collections
Static utility methods for collections.
Sorts the list in ascending natural order.
Reverses the order of elements in the list.
Randomly permutes the list.
Returns the minimum element according to natural ordering.
Returns the maximum element according to natural ordering.
Searches a sorted list using binary search.
Stream<T>
Sequence of elements supporting aggregate operations.
Intermediate Operations
Returns a stream of elements that match the predicate.
Returns a stream of transformed elements.
Returns a stream with elements in natural order.
Returns a stream of at most maxSize elements.
Returns a stream with the first n elements skipped.
Terminal Operations
Performs an action for each element.
Returns the count of elements.
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
names.stream()
.filter(name -> name.length() > 3)
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
// Output: ALICE, CHARLIE, DAVID