Add the following Maven dependency to your project:
<dependency>
<groupId>de.cronn</groupId>
<artifactId>commons-lang</artifactId>
<version>1.3</version>
</dependency>
This library includes the following classes, which are described in detail in the sections below:
StreamUtil
: Collectors and utilities that are useful when working with Java streamsAction
: An interface that is similar toRunnable
but allows to throw checked exceptionsAlphanumericComparator
: A comparator that implements the Alphanum Algorithm which is useful to sort versions or filenames in a more
Good case:
Object number = Stream.of(1, 2, 3)
.filter(value -> value > 2)
.collect(StreamUtil.toSingleElement());
// number = 3
Bad case:
Object number = Stream.of(1, 2, 3, 4)
.filter(value -> value > 2)
.collect(StreamUtil.toSingleElement());
// Throws IllegalStateException: Exactly one element expected but got 2: [3, 4]
Similar to toSingleElement()
but returns an Optional
and throws no exception if no element was found.
Optional<Object> number = Stream.of(1, 2, 3)
.filter(value -> value > 2)
.collect(StreamUtil.toSingleOptionalElement());
StreamUtil.toLinkedHashSet()
is a drop-replacement for Collectors.toSet()
that guarantees a stable/deterministic order.
Example:
// numbers contains 1, 2, 3 and returns the elements in exactly this order when iterating
Set<Object> numbers = Stream.of(1, 2, 3, 2, 3)
.collect(StreamUtil.toLinkedHashSet());
SetUtils
provides utility methods for creating ordered sets in Java.
Unlike Set.of(…)
, SetUtils.orderedSet(…)
maintains the order of elements as they are added.
Set<String> ordered = SetUtils.orderedSet("abc", "def", "ghi");
// Output: [abc, def, ghi]
Set<Integer> numbers = SetUtils.orderedSet(3, 1, 2, 1);
// Output: [3, 1, 2]
Key Difference:
SetUtils.orderedSet(…)
usesLinkedHashSet
, ensuring insertion order is preserved.
People sort strings with numbers differently than software does. Most sorting algorithms compare ASCII values, which produces an ordering that is inconsistent with human logic.
Consider the following list of filenames:
file1.txt
file2.txt
file10.txt
file3.txt
If you sort this list using the default sort order, the result will be:
file1.txt
file10.txt
file2.txt
file3.txt
This order is not intuitive for most people.
However, by using the AlphanumericComparator, you will get:
file1.txt
file2.txt
file3.txt
file10.txt
This order is more natural and aligns with human expectations.
- Java 17+