Skip to content

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

License

Notifications You must be signed in to change notification settings

simonpercic/CollectionHelper

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages