Utilities for Collections

GitLab Docs Pipeline Coverage
Java module with immutable containers with some specific consistency behaviour requirements, and handy builders. See morimekta.net/utils for procedures on releases.

Getting Started

To add to maven: Add this line to pom.xmlunder dependencies:

<dependency>
    <groupId>net.morimekta.utils</groupId>
    <artifactId>collect</artifactId>
    <version>5.0.0</version>
</dependency>

To add to gradle: Add this line to the dependencies group in build.gradle:

implementation 'net.morimekta.utils:collect:5.0.0'

Unmodifiable Containers

This module contains a number of unmodifiable containers with some specific consistency behaviour requirements, and handy builders. Each of these has handy constructor collectors, and require that all values (and keys) are non-null. The non-sorted map and set both guarantee that iteration order is kept (as with LinkedHashSet and LinkedHashMap), but will also be unmodifiable. The sorted variants have handy no-copy slicing for range subSet and subMap handling.

  • UnmodifiableList: A list that inherently cannot be modified. It has handy no-copy on slice behavior.
  • UnmodifiableMap: A hash-map that inherently cannot be modified, and keeps iteration order as it was built.
  • UnmodofiableSet: A hash-set that inherently cannot be modified, and keeps iteration order as it was built.
  • UnmodifiableSortedMap: An array based sorted map, that inherently cannot be modified.
  • UnmodifiableSortedSet: An array based sorted set, that inherently cannot be modified.
  • ListBuilder, SetBuilder MapBuilder: Base interfaces for simple builders of said base types.
import java.util.List;
import java.util.Set;

import static net.morimekta.collect.UnmodifiableSortedSet.toSortedSet;

interface Example {
    // Get the ID of the example.
    String getName();

    // Get a sorted set of the names of the examples.
    static Set<String> getNames(List<Example> examples) {
        return list.stream()
                   .map(Example::getName)
                   .collect(toSortedSet());
    }
}

Extra Collectors

See the ExtraCollectors class, with a set of handy collectors:

  • inBatchesOf: Split the stream into a list of lists of max size N, each inner list has a maximum result size from the batch.
  • inNumBatches: Split the stream into a list of N lists, each with close to the same size.
  • groupingBy: Group items in the stream by key, optionally map value.
  • groupingByAll: Group items in the stream by a set of keys, each item put under all keys provided.
import java.util.List;

import static net.morimekta.collect.util.ExtraCollectors.inBatchesOf;

interface Job extends Runnable {
    static void doJobs(List<Job> jobs) {
        jobs.stream()
            .collect(inBatchesOf(10))
            .forEach(batch -> {
                // do something with batch of 1..10 Job items.
            });
    }
}

Utilities

  • Binary: Wrapping a byte array in an immutable container with handy utilities and no-copy slicing behavior.
  • Pair & Tuple: Simple unmodifiable pair and tuple classes to encompass a pair of values or a short tuple with basic helpers and operations.
  • LazyCachedSupplier: Supplier that will lazily cache from another supplier and keep that value. Works similar to the kotlin lazy keyword.
  • SetOperations: Contains a number of standard set operations, all which follow the standard mathematical set operations explained e.g. in Encyclopedia Britannica or Probability Course.