Utility Classes and Interfaces

This page contains information about several very useful Java classes and interfaces that fall into the "utility" category.

Selected Utility Classes and Interfaces and Their Methods (from the Java API)

Classes

java.util.Arrays
Contains dozens of static methods (many of them overloaded multiple times) for manipulating arrays in various ways (such as sorting, searching, copying and filling). Also contains the "static factory method" asList() that allows arrays to be viewed as lists so that additional "list machinery" can be applied to them. We give here only the generic names of those methods we have illustrated in the sample program UtilityClasses/TestTheArraysClass.java. To see which versions of the methods are illustrated, study that sample program and to see what else is available, check the official documentation for any other method you might need, and details on the particular version of any method you plan to use.
- asList()
- binarySearch()
- copyOf()
- copyOfRange()
- equals()
- fill()
- setAll()
- sort()
- stream()
- toString()

java.util.Collections
Consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers" which return a new collection backed by a specified collection, and a few other odds and ends. As with Arrays, there are dozens of methods (but not so many of them overloaded this time) for manipulating collections. We give here only the generic names of those methods we have illustrated in the sample program UtilityClasses/TestTheCollectionsClass.java. To see which versions of the methods are illustrated, study that sample program and to see what else is available, check the online documentation for any other method you might need, and details on the particular version of any method you plan to use.
- addAll() adds one collection to another.
- asLifoQueue() returns a view of a deque as a queue.
- binarySearch() searches a list.
- copy() copies one list to another.
- disjoint() determines if two collections are disjoint.
- empty*() provides several methods which return empty entities of various kinds.
- fill() replaces all elements of a list with a specified element.
- frequency() returns number of elements in a collection equal to a specified object.
- indexOfSublist()
- lastIndexOfSublist()
- max() returns maximum element of a collection.
- min() returns minimum element of a collection.
- nCopies() returns immutable list of n copies of a specified object.
- replaceAll() replaces all occurrences of one specified value in a list with another.
- reverse() reverses a list.
- rotate() rotates elements of a list by a specified amount.
- shuffle() shuffles a list.
- sort() sorts a list.
- swap() swaps the two elements at specified positions in a list.

java.util.stream.Collectors
This class provides implementations of the Collector<T, A, R> interface that implement various useful reduction/collection operations, such as accumulating elements into collections, summarizing elements according to various criteria, and so on. Check the official documentation for precise interfaces, and to see a number of typical scenarios in which this class might be used. [This is unusual ... you generally don't see usage examples in the official documentation.]
- averagingDouble()
- averagingInt()
- averagingLong()
- collectingAndThen()
- counting()
- groupingBy()
- joining()
- mapping()
- maxBy()
- minBy()
- partitioningBy()
- reducing()
- summarizingDouble()
- summarizingInt()
- summarizingLong()
- summingDouble()
- summingInt()
- summingLong()
- toCollection()
- toList()
- toMap()
- toSet()

java.util.Optional<T>
An object of this class may or may not contain a null value, and represents an attempt by the Java folks (as of Java 8) to provide a more elegant way to deal with null values. If a non-null value is present, isPresent() will return true and get() will return the value. Additional methods that depend on the presence or absence of a contained value are also provided. For example, orElse() will return a default value if a non-null value is not present, and ifPresent() will execute a block of code if a non-null value is present.
- empty() is not a boolean method, but rather a method that returns an empty Optional instance.
- equals() is a boolean method that indicates whether some other object is equal to this Optional instance.
- filter() returns an Optional object describing a value if the value is present, and otherwise returns an empty Optional object.
- flatmap() returns an Optional object containing the result of applying a mapping function to a value if the value is present, and otherwise returns an empty Optional object.
- get() returns the value if the Optional object contains a value, and otherwise throws a NoSuchElementException.
- hashCode() returns the integer hash code of the present value, if there is a value, or zero if there is no value.
- ifPresent() invokes a supplied "consumer" with the value if a value is present and otherwise does nothing.
- isPresent() returns true if a value is present and otherwise returns false.
- map() returns an Optional object containing the result of applying a supplied mapping function to the value, if there is a value, and otherwise returns an empty Optional.
- of() returns an Optional object containing a supplied (non-null) value.
- ofNullable() returns an Optional object containing a supplied value if the value is non-null and otherwise returns an empty Optional object.
- orElse() returns the value if present, and otherwise returns a specified "other" value.
- orElseGet() returns the contained value if present and otherwise invokes a supplied "supplier" to produce a result which is then returned.
- orElseThrow() returns the contained value if present, and otherwise invokes a supplied "exception supplier" which throws a suitable exception.
- toString() returns a non-empty string representation of this Optional object, which should be suitable for debugging.


Interfaces

java.util.stream.Collector<T, A, R>
This is an important interface, but probably not one you will implement directly yourself. The Collectors class contains implementations of various useful operations contained in this interface and it is generally that class that you will use to "collect" things. The parameters in the interface are T (the type of input elements in the reduction/collection operation), A (the mutable accumulation type of the reduction operation, which may actually be "hidden" as an implementation detail), and R (the result type of the reduction operation).

java.util.stream.Stream<E>
A "stream" in this context is "a sequence of elements supporting sequential and parallel aggregate operations". Think of a "container" as a "collection of items in space", and a "stream" as a "collection of items in time". From the official documentation: [To perform a computation, stream operations are composed into a stream "pipeline". A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more "intermediate operations" (which transform a stream into another stream, such as filter(Predicate)), and a "terminal operation" (which produces a result or side-effect, such as count() or forEach(Consumer)). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.] Be sure to read the short description of what a stream is in the official documentation.
- allMatch()
- anyMatch()
- builder()
- collect()
- concat()
- count()
- distinct()
- empty()
- filter()
- findAny()
- findFirst()
- flatMap()
- forEach()
- forEachOrdered()
- generate()
- iterate()
- limit()
- map()
- mapToDouble()
- mapToInt()
- mapToLong()
- max()
- min()
- noneMatch()
- of()
- peek()
- reduce()
- skip()
- sorted()
- toArray()

java.util.stream.IntStream<E>
A sequence of primitive int-valued elements supporting sequential and parallel aggregate operations. This is the int primitive specialization of Stream.
- allMatch()
- anyMatch()
- asDoubleStream()
- asLongStream()
- average()
- boxed()
- builder()
- collect()
- concat()
- count()
- distinct()
- empty()
- filter()
- findAny()
- findFirst()
- flatMap()
- forEach()
- forEachOrdered()
- generate()
- iterate()
- iterator()
- limit()
- map()
- mapToDouble()
- mapToLong()
- mapToObj()
- max()
- min()
- noneMatch()
- of()
- parallel()
- peek()
- range()
- rangeClosed()
- reduce()
- sequential()
- skip()
- sorted()
- sum()
- summaryStatistics()
- toArray()

java.util.stream.DoubleStream<E>
The methods in this interface are, as you might expect, analogues (mutatis mutandis) of those found in the IntStream<E> interface, as listed above.

java.util.stream.LongStream<E>
The methods in this interface are, as you might expect, analogues (mutatis mutandis) of those found in the IntStream<E> interface, as listed above.

java.util.Iterator<E>
An iterator over a collection of elements of type E.
- default void forEachRemaining(action) performs given action for each remaining element.]
- boolean hasNext()
- E next()
- default void remove() [optional] removes last element returned by next().