java.util Package

Collection framework classes and utility functions.

ArrayList<E>

Resizable array implementation of the List interface.

Constructor

ArrayList()

Constructs an empty list with initial capacity of 10.

Methods

boolean add(E element)

Appends the element to the end. Returns true.

E get(int index)

Returns the element at the specified position.

E set(int index, E element)

Replaces the element at the specified position. Returns the old element.

E remove(int index)

Removes and returns the element at the specified position.

int size()

Returns the number of elements.

boolean isEmpty()

Returns true if the list contains no elements.

boolean contains(Object o)

Returns true if the list contains the element.

int indexOf(Object o)

Returns the index of the first occurrence, or -1 if not found.

void clear()

Removes all elements from the list.

Stream<E> stream()

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

LinkedList()

Constructs an empty linked list.

Methods

boolean add(E element)

Appends element to the end.

void addFirst(E element)

Inserts element at the beginning.

void addLast(E element)

Appends element to the end.

E get(int index)

Returns element at the specified position.

E getFirst()

Returns the first element. Throws if empty.

E getLast()

Returns the last element. Throws if empty.

E set(int index, E element)

Replaces element at position. Returns old element.

E remove(int index)

Removes and returns element at position.

E removeFirst()

Removes and returns the first element.

E removeLast()

Removes and returns the last element.

int size()

Returns the number of elements.

boolean isEmpty()

Returns true if the list is empty.

boolean contains(Object o)

Returns true if the list contains the element.

void clear()

Removes all elements.

HashMap<K, V>

Hash table implementation of the Map interface.

Constructor

HashMap()

Constructs an empty HashMap with default capacity (16) and load factor (0.75).

Methods

V put(K key, V value)

Associates the key with the value. Returns the previous value, or null.

V get(Object key)

Returns the value for the key, or null if not found.

boolean containsKey(Object key)

Returns true if the map contains the key.

V remove(Object key)

Removes the mapping for the key. Returns the previous value.

int size()

Returns the number of key-value mappings.

boolean isEmpty()

Returns true if the map contains no mappings.

void clear()

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

HashSet()

Constructs an empty HashSet.

Methods

boolean add(E element)

Adds the element if not already present. Returns true if added.

boolean remove(Object o)

Removes the element if present. Returns true if removed.

boolean contains(Object o)

Returns true if the set contains the element.

int size()

Returns the number of elements.

boolean isEmpty()

Returns true if the set is empty.

void clear()

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

TreeSet()

Constructs an empty TreeSet with natural ordering.

Methods

boolean add(E element)

Adds the element in sorted order. Returns true if added.

boolean remove(Object o)

Removes the element. Returns true if removed.

boolean contains(Object o)

Returns true if the set contains the element.

E first()

Returns the lowest (first) element.

E last()

Returns the highest (last) element.

int size()

Returns the number of elements.

boolean isEmpty()

Returns true if the set is empty.

void clear()

Removes all elements.

Object[] toArray()

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

static void sort(Object[] a)

Sorts the array in ascending order using quicksort.

Searching

static int binarySearch(Object[] a, Object key)

Searches using binary search. Returns index if found, or negative insertion point if not found.

Fill

static void fill(Object[] a, Object val)

Assigns the specified value to each element.

Copy

static Object[] copyOf(Object[] original, int newLength)

Copies the array, truncating or padding with nulls.

static Object[] copyOfRange(Object[] original, int from, int to)

Copies a range of the array.

Comparison

static boolean equals(Object[] a, Object[] a2)

Returns true if the two arrays are equal (same length and elements).

Conversion

static String toString(Object[] a)

Returns a string representation: "[a, b, c]".

static List<T> asList(T... a)

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.

static void sort(List<T> list)

Sorts the list in ascending natural order.

static void reverse(List<?> list)

Reverses the order of elements in the list.

static void shuffle(List<?> list)

Randomly permutes the list.

static T min(Collection<T> coll)

Returns the minimum element according to natural ordering.

static T max(Collection<T> coll)

Returns the maximum element according to natural ordering.

static int binarySearch(List<T> list, T key)

Searches a sorted list using binary search.

Stream<T>

Sequence of elements supporting aggregate operations.

Intermediate Operations

Stream<T> filter(Predicate<T> predicate)

Returns a stream of elements that match the predicate.

Stream<R> map(Function<T, R> mapper)

Returns a stream of transformed elements.

Stream<T> sorted()

Returns a stream with elements in natural order.

Stream<T> limit(long maxSize)

Returns a stream of at most maxSize elements.

Stream<T> skip(long n)

Returns a stream with the first n elements skipped.

Terminal Operations

void forEach(Consumer<T> action)

Performs an action for each element.

long count()

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