Code Monkey home page Code Monkey logo

commons-lang's Introduction

CI Maven Central Apache 2.0 codecov Valid Gradle Wrapper

cronn commons-lang

Add the following Maven dependency to your project:

<dependency>
    <groupId>de.cronn</groupId>
    <artifactId>commons-lang</artifactId>
    <version>1.1</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 streams
  • Action: An interface that is similar to Runnable but allows to throw checked exceptions
  • AlphanumericComparator: A comparator that implements the Alphanum Algorithm which is useful to sort versions or filenames in a more

StreamUtil

toSingleElement()

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]

toSingleOptionalElement()

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());

toLinkedHashSet()

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());

AlphanumericComparator

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.

Requirements

  • Java 17+

commons-lang's People

Contributors

bwaldvogel avatar

Stargazers

Ayk Borstelmann avatar  avatar

Watchers

 avatar Andrei Ivantsov avatar Jobst Essmeyer avatar Michal Bazanski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.