Skip to content

Misc java utility classes

clementyee303 edited this page Apr 25, 2021 · 1 revision

Originally posted by @tshuenhau

ArrayList

Method name Description
add​(int index, E element) Inserts the specified element at the specified position in this list.
add​(E e) Appends the specified element to the end of this list.
clear() Removes all of the elements from this list.
contains​(Object o) Returns true if this list contains the specified element.
get​(int index) Returns the element at the specified position in this list.
indexOf​(Object o) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
isEmpty() Returns true if this list contains no elements.
remove​(int index) Removes the element at the specified position in this list.
size() Returns the number of elements in this list.

HashMap

Method name Description
put(YourClass key, YourClass value) Adds key to the HashMap with the value value.
clear() Clears the HashMap.
containsKey(Object o) Checks if key o is in the HashMap, based off the object’s equals() method
containsValue(Object o) Checks if value o is in the HashMap, based off the object’s equals() method
isEmpty() Checks if the HashMap is empty
remove(Object o) Removes the entry with key o if it is in the HashMap, based off the object’s equals() method
size() Returns the number of elements in the HashMap
entrySet() Returns a set of all entries in the HashMap
keySet() Returns a set of all keys in the HashMap
values() Returns a collection of all values in the HashMap

Predicate

Method name Description
test(T t) Evaluates this predicate on the given argument. Returns true if the input argument matches the predicate, otherwise false
and(Predicate<? super T> other) Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
negate() Returns a predicate that represents the logical negation of this predicate.
or(Predicate<? super T> other) Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
isEqual(Object targetRef) Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).

Function

Method name Description
apply(T t) Applies this function to the given argument.
compose(Function<? super V,? extends T> before) Returns a composed function that first applies the before function to its input, and then applies this function to the result.
andThen(Function<? super R,? extends V> after) Returns a composed function that first applies this function to its input, and then applies the after function to the result.
identity() Returns a function that always returns its input argument.

Stream

Method name Description
accept(T t) Performs this operation on the given argument.
andThen(Consumer<? super T> after) Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.
collect​(Collector<? super T,​A,​R> collector) Performs a mutable reduction operation on the elements of this stream using a Collector.
filter​(Predicate<? super T> predicate) Returns a stream consisting of the elements of this stream that match the given predicate.
flatMap​(Function<? super T,​? extends Stream<? extends R>> mapper) Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
iterate​(T seed, Predicate<? super T> hasNext, UnaryOperator next) Returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.

Supplier

Method name Description
get() Gets a result.