Coder Social home page Coder Social logo

stream-utils's Introduction

Laconic Stream Utils

This is a library of laconic method-aliases for safety stream operations with collections in Java.

Introduction

Java 8+ introduces Stream API & Optional classes, but sometimes the syntax is really verbose even for simple stream manipulations. This library introduces method-aliases for some popular operation with collections.

For example, instead of the following code that maps users to modifiable list:

if (users != null) {
    return users.stream().map(User::getFirstName).collect(Collectors.toList());
}
return new ArrayList<>();

you can use short static method from the Lists class:

return map(users, User::getFirstName);

Installation

Min Requirements: JDK 8+

You can pull releases from the Maven Central repository: https://search.maven.org/artifact/io.github.avegera/stream-utils

Use the following dependency for Maven/Gradle:

Maven
<dependency>
    <groupId>io.github.avegera</groupId>
    <artifactId>stream-utils</artifactId>
    <version>0.3.0</version>
</dependency>
Gradle
implementation 'io.github.avegera:stream-utils:0.3.0'

Streams

Streams contains NPE-safety methods for stream instantiating by provided input data. For example, the following method returns stream of users (or empty stream in case when users == null):

Stream<User> stream = safeStream(users);

Lists

Lists contains NPE-safety method-aliases for common Stream API intermediate operations like:

  • map()

  • filter()

  • flatMap()

  • distinct()

  • sort()

By default, the result of operations is a List. Use suffix …​ToSet() in method name to return Set instead. For example, the following method returns list of groups:

List<Group> groups = map(users, User::getGroup);

But this one returns set of groups:

Set<Group> groups = mapToSet(users, User::getGroup);

Aliases for terminal operations also available:

  • collect(toList())

  • collect(toSet())

  • count()

  • findFirst()

  • findAny()

  • allMatch()

  • anyMatch()

  • noneMatch()

For example, the following method returns first user from list or null if no users inside:

User user = findFirstOrNull(users);

License

This project is published under the Apache License 2.0, see http://www.apache.org/licenses/LICENSE-2.0 for details.

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.