CollectionHelper

A set of static utility methods to simplify filtering and querying Java's Collections.

License

License

GroupId

GroupId

com.github.simonpercic
ArtifactId

ArtifactId

collectionhelper
Last Version

Last Version

1.2.1
Release Date

Release Date

Type

Type

aar
Description

Description

CollectionHelper
A set of static utility methods to simplify filtering and querying Java's Collections.
Project URL

Project URL

https://github.com/simonpercic/CollectionHelper
Source Code Management

Source Code Management

https://github.com/simonpercic/CollectionHelper

Download collectionhelper

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.simonpercic/collectionhelper/ -->
<dependency>
    <groupId>com.github.simonpercic</groupId>
    <artifactId>collectionhelper</artifactId>
    <version>1.2.1</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.simonpercic/collectionhelper/
implementation 'com.github.simonpercic:collectionhelper:1.2.1'
// https://jarcasting.com/artifacts/com.github.simonpercic/collectionhelper/
implementation ("com.github.simonpercic:collectionhelper:1.2.1")
'com.github.simonpercic:collectionhelper:aar:1.2.1'
<dependency org="com.github.simonpercic" name="collectionhelper" rev="1.2.1">
  <artifact name="collectionhelper" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.simonpercic', module='collectionhelper', version='1.2.1')
)
libraryDependencies += "com.github.simonpercic" % "collectionhelper" % "1.2.1"
[com.github.simonpercic/collectionhelper "1.2.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

CollectionHelper

Build Status Download

What is it?

A set of static utility methods to simplify filtering and querying Java's Collections.

A limited subset of .NET framework's LINQ Enumerable Methods for Java.

Methods

Filter

Filters a collection using the given predicate

List<T> filter(Collection<T> items, Predicate<T> predicate)

First

Returns the first element from a collection that matches the given predicate. Throws an exception if no matching element is found

T first(Collection<T> items, Predicate<T> predicate)

First or null

Returns the first element from a collection that matches the given predicate or null if no matching element is found

T firstOrNull(Collection<T> items, Predicate<T> predicate)

Any

Returns true if any element of a collection matches the given predicate

boolean any(Collection<T> items, Predicate<T> predicate)

All

Returns true if all elements of a collection match the given predicate

boolean all(Collection<T> items, Predicate<T> predicate)

Is empty

Returns true if the collection is null or contains no elements

boolean isEmpty(Collection items)

Single

Returns the only element from a collection that matches the given predicate. Throws an exception if the number of found elements is not exactly 1

T single(Collection<T> items, Predicate<T> predicate)

Single or null

Returns the only element from a collection that matches the given predicate or null if such element is not found. Throws an exception if there is more than 1 element matching the predicate

T singleOrNull(Collection<T> items, Predicate<T> predicate)

Count

Returns the number of elements in a collection matching the given predicate

int count(Collection<T> items, Predicate<T> predicate)

Map

Projects each element of a collection into a new collection

List<TResult> map(Collection<TSource> items, Mapper<TSource, TResult> mapper)

Javadoc

Click here

Usage

Add using Gradle:

compile 'com.github.simonpercic:collectionhelper:1.2.1'

Use

// sample list
List<Integer> integerList = Arrays.asList(1, 4, 2, 7, 8, 0, 5);

// filter
List<Integer> largerThan2 = CollectionHelper.filter(integerList, new Predicate<Integer>() {
            @Override public boolean apply(Integer intValue) {
                return intValue > 2;
            }
        });
        
// map
List<String> mappedList = CollectionHelper.map(integerList, new Mapper<Integer, String>() {
            @Override public String map(Integer intValue) {
                return String.format("myString_%d", intValue);
            }
        });

Android Pro-tip

Use the awesome Gradle Retrolambda Plugin with Java 8 to use lambdas:

// filter
List<Integer> largerThan2 = CollectionHelper.filter(integerList, intValue -> intValue > 2);
        
// map
List<String> mappedList = CollectionHelper.map(integerList, intValue -> String.format("myString_%d", intValue));

// or even using a method reference
List<String> simpleMappedList = CollectionHelper.map(integerList, String::valueOf);

Appendix

If you are using Java 8 and are NOT on Android you can also use Streams to simplify working with Collections.

Change Log

See CHANGELOG.md

License

Open source, distributed under the MIT License. See LICENSE for details.

Versions

Version
1.2.1
1.2.0
1.1.0
1.0.0