CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-eclipse-collections--eclipse-collections

A comprehensive collections library for Java delivering productivity and performance through an expressive and efficient set of APIs and types.

Pending
Overview
Eval results
Files

functional-programming.mddocs/

Functional Programming

Eclipse Collections provides comprehensive functional programming support through Functions, Predicates, Procedures, and factory classes for common patterns. These interfaces enable powerful functional-style operations across all collection types.

Capabilities

Core Functional Interfaces

Base functional interfaces providing the foundation for all functional operations.

/**
 * Function interface for transforming objects of type T to type R
 */
interface Function<T, R> {
    /**
     * Transform the input argument to output value
     * @param argument input value to transform
     * @return transformed output value
     */
    R valueOf(T argument);
}

/**
 * Supplier function with no parameters returning type R
 */
interface Function0<R> {
    /**
     * Compute and return a value
     * @return computed value
     */
    R value();
}

/**
 * Function interface for transforming two parameters to result type R
 */
interface Function2<T1, T2, R> {
    /**
     * Transform two input arguments to output value
     * @param argument1 first input parameter
     * @param argument2 second input parameter  
     * @return transformed output value
     */
    R value(T1 argument1, T2 argument2);
}

/**
 * Function interface for transforming three parameters to result type R
 */
interface Function3<T1, T2, T3, R> {
    /**
     * Transform three input arguments to output value
     * @param argument1 first input parameter
     * @param argument2 second input parameter
     * @param argument3 third input parameter
     * @return transformed output value
     */
    R value(T1 argument1, T2 argument2, T3 argument3);
}

/**
 * Predicate interface for testing conditions on objects of type T
 */
interface Predicate<T> {
    /**
     * Evaluate the predicate on the given argument
     * @param object argument to evaluate
     * @return true if argument satisfies predicate, false otherwise
     */
    boolean accept(T object);
}

/**
 * Two-parameter predicate interface
 */
interface Predicate2<T, P> {
    /**
     * Evaluate the predicate on the given arguments
     * @param object first argument to evaluate
     * @param parameter second argument to evaluate
     * @return true if arguments satisfy predicate, false otherwise
     */
    boolean accept(T object, P parameter);
}

/**
 * Procedure interface for executing actions on objects of type T
 */
interface Procedure<T> {
    /**
     * Execute an action on the given argument
     * @param object argument to process
     */
    void value(T object);
}

/**
 * Two-parameter procedure interface
 */
interface Procedure2<T, P> {
    /**
     * Execute an action on the given arguments
     * @param object first argument to process
     * @param parameter second argument to process
     */
    void value(T object, P parameter);
}

/**
 * Procedure interface for objects with int parameter
 */
interface ObjectIntProcedure<T> {
    /**
     * Execute action on object with int parameter
     * @param object object to process
     * @param index int parameter (often used as index)
     */
    void value(T object, int index);
}

Primitive Functional Interfaces

Functional interfaces for primitive types avoiding boxing overhead.

// Int-specific functional interfaces
/**
 * Function from objects to int primitives
 */
interface IntFunction<T> {
    /**
     * Transform object to int value
     * @param object input object
     * @return int result
     */
    int intValueOf(T object);
}

/**
 * Function from int primitives to objects
 */
interface IntToObjectFunction<R> {
    /**
     * Transform int to object value
     * @param value input int
     * @return transformed object
     */
    R valueOf(int value);
}

/**
 * Function from int to int
 */
interface IntToIntFunction {
    /**
     * Transform int to int value
     * @param value input int
     * @return transformed int
     */
    int valueOf(int value);
}

/**
 * Function with no parameters returning int
 */
interface IntFunction0 {
    /**
     * Compute and return int value
     * @return computed int
     */
    int value();
}

/**
 * Two-parameter function returning int
 */
interface IntObjectToIntFunction<T> {
    /**
     * Transform object and int to int result
     * @param object input object
     * @param intParameter input int parameter
     * @return transformed int result
     */
    int intValueOf(T object, int intParameter);
}

/**
 * Predicate for testing int values
 */
interface IntPredicate {
    /**
     * Test int value against predicate
     * @param value int value to test
     * @return true if value satisfies predicate
     */
    boolean accept(int value);
}

/**
 * Procedure for processing int values
 */
interface IntProcedure {
    /**
     * Execute action on int value
     * @param value int value to process
     */
    void value(int value);
}

/**
 * Procedure for processing int pairs
 */
interface IntIntProcedure {
    /**
     * Execute action on two int values
     * @param int1 first int value
     * @param int2 second int value
     */
    void value(int int1, int int2);
}

// Boolean-specific functional interfaces
/**
 * Function from objects to boolean primitives
 */
interface BooleanFunction<T> {
    boolean booleanValueOf(T object);
}

/**
 * Function from boolean primitives to objects
 */
interface BooleanToObjectFunction<R> {
    R valueOf(boolean value);
}

/**
 * Predicate for testing boolean values
 */
interface BooleanPredicate {
    boolean accept(boolean value);
}

/**
 * Procedure for processing boolean values
 */
interface BooleanProcedure {
    void value(boolean value);
}

// Similar patterns exist for: Byte, Char, Short, Long, Float, Double
// ByteFunction<T>, ByteToObjectFunction<R>, BytePredicate, ByteProcedure
// CharFunction<T>, CharToObjectFunction<R>, CharPredicate, CharProcedure  
// ShortFunction<T>, ShortToObjectFunction<R>, ShortPredicate, ShortProcedure
// LongFunction<T>, LongToObjectFunction<R>, LongPredicate, LongProcedure
// FloatFunction<T>, FloatToObjectFunction<R>, FloatPredicate, FloatProcedure
// DoubleFunction<T>, DoubleToObjectFunction<R>, DoublePredicate, DoubleProcedure

Predicates Factory

Factory class providing common predicates and predicate operations.

/**
 * Factory class for creating common predicates
 */
public final class Predicates {
    /**
     * Predicate that always returns true
     * @return predicate that accepts all objects
     */
    public static <T> Predicate<T> alwaysTrue();
    
    /**
     * Predicate that always returns false
     * @return predicate that rejects all objects
     */
    public static <T> Predicate<T> alwaysFalse();
    
    /**
     * Predicate that tests for null values
     * @return predicate that accepts null objects
     */
    public static Predicate<Object> isNull();
    
    /**
     * Predicate that tests for non-null values
     * @return predicate that accepts non-null objects
     */
    public static Predicate<Object> notNull();
    
    /**
     * Predicate that tests equality with expected value
     * @param expected expected value for comparison
     * @return predicate that accepts objects equal to expected
     */
    public static Predicate<Object> equal(Object expected);
    
    /**
     * Predicate that tests inequality with expected value
     * @param expected value to compare against
     * @return predicate that accepts objects not equal to expected
     */
    public static Predicate<Object> notEqual(Object expected);
    
    /**
     * Predicate that tests if object is instance of specified class
     * @param clazz class to test against
     * @return predicate that accepts instances of specified class
     */
    public static Predicate<Object> instanceOf(Class<?> clazz);
    
    /**
     * Predicate that tests if object is assignable to specified class
     * @param clazz class to test against
     * @return predicate that accepts objects assignable to class
     */
    public static Predicate<Object> assignableFrom(Class<?> clazz);
    
    /**
     * Predicate that tests membership in collection
     * @param collection collection to test membership against
     * @return predicate that accepts objects contained in collection
     */
    public static Predicate<Object> in(Collection<?> collection);
    
    /**
     * Predicate that tests membership in iterable
     * @param iterable iterable to test membership against
     * @return predicate that accepts objects contained in iterable
     */
    public static Predicate<Object> in(Iterable<?> iterable);
    
    /**
     * Predicate that tests non-membership in collection
     * @param collection collection to test against
     * @return predicate that accepts objects not in collection
     */
    public static Predicate<Object> notIn(Collection<?> collection);
    
    /**
     * Predicate that tests non-membership in iterable
     * @param iterable iterable to test against  
     * @return predicate that accepts objects not in iterable
     */
    public static Predicate<Object> notIn(Iterable<?> iterable);
    
    // Logical operations
    /**
     * Create logical AND of predicates
     * @param predicates predicates to combine with AND
     * @return predicate that requires all predicates to be true
     */
    @SafeVarargs
    public static <T> Predicate<T> and(Predicate<? super T>... predicates);
    
    /**
     * Create logical OR of predicates
     * @param predicates predicates to combine with OR
     * @return predicate that requires any predicate to be true
     */
    @SafeVarargs  
    public static <T> Predicate<T> or(Predicate<? super T>... predicates);
    
    /**
     * Create logical NOT of predicate
     * @param predicate predicate to negate
     * @return predicate that returns opposite of input predicate
     */
    public static <T> Predicate<T> not(Predicate<? super T> predicate);
    
    // String-specific predicates
    /**
     * Predicate that tests if string starts with prefix
     * @param prefix prefix to test for
     * @return predicate for strings starting with prefix
     */
    public static Predicate<String> startsWith(String prefix);
    
    /**
     * Predicate that tests if string ends with suffix
     * @param suffix suffix to test for
     * @return predicate for strings ending with suffix
     */
    public static Predicate<String> endsWith(String suffix);
    
    /**
     * Predicate that tests if string contains substring
     * @param substring substring to test for
     * @return predicate for strings containing substring
     */
    public static Predicate<String> contains(String substring);
    
    /**
     * Predicate that tests if string matches regex pattern
     * @param pattern regex pattern to match
     * @return predicate for strings matching pattern
     */
    public static Predicate<String> matches(String pattern);
    
    // Attribute-based predicates
    /**
     * Create predicate based on attribute extraction and testing
     * @param function function to extract attribute from object
     * @param predicate predicate to test extracted attribute
     * @return composed predicate
     */
    public static <T, V> Predicate<T> attributePredicate(Function<? super T, ? extends V> function, Predicate<? super V> predicate);
    
    /**
     * Create predicate testing if attribute equals expected value
     * @param function function to extract attribute
     * @param expectedValue expected attribute value
     * @return predicate testing attribute equality
     */
    public static <T, V> Predicate<T> attributeEqual(Function<? super T, ? extends V> function, V expectedValue);
    
    /**
     * Create predicate testing if attribute is greater than value
     * @param function function to extract comparable attribute
     * @param expectedValue value to compare against
     * @return predicate testing attribute > expectedValue
     */
    public static <T, V extends Comparable<? super V>> Predicate<T> attributeGreaterThan(Function<? super T, ? extends V> function, V expectedValue);
    
    /**
     * Create predicate testing if attribute is less than value
     * @param function function to extract comparable attribute
     * @param expectedValue value to compare against
     * @return predicate testing attribute < expectedValue
     */
    public static <T, V extends Comparable<? super V>> Predicate<T> attributeLessThan(Function<? super T, ? extends V> function, V expectedValue);
    
    /**
     * Create predicate testing if attribute is between values (inclusive)
     * @param function function to extract comparable attribute
     * @param lowerBound lower bound (inclusive)
     * @param upperBound upper bound (inclusive)  
     * @return predicate testing lowerBound <= attribute <= upperBound
     */
    public static <T, V extends Comparable<? super V>> Predicate<T> attributeBetween(Function<? super T, ? extends V> function, V lowerBound, V upperBound);
}

Usage Examples:

import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.factory.Lists;

List<String> names = Lists.mutable.with("Alice", "Bob", "Charlie", "", null);

// Basic predicates
MutableList<String> nonNull = names.select(Predicates.notNull());
MutableList<String> nonEmpty = names.reject(Predicates.equal(""));

// String predicates
MutableList<String> startsWithA = names.select(Predicates.startsWith("A"));
MutableList<String> containsB = names.select(Predicates.contains("b"));

// Logical combinations
Predicate<String> validName = Predicates.and(
    Predicates.notNull(),
    Predicates.not(Predicates.equal(""))
);
MutableList<String> validNames = names.select(validName);

// Collection membership
Set<String> allowedNames = Sets.mutable.with("Alice", "Bob");
MutableList<String> allowed = names.select(Predicates.in(allowedNames));

// Attribute-based predicates
List<Person> people = Lists.mutable.with(
    new Person("Alice", 25),
    new Person("Bob", 35)  
);
MutableList<Person> adults = people.select(
    Predicates.attributeGreaterThan(Person::getAge, 18)
);

Predicates2 Factory

Factory class for two-argument predicates.

/**
 * Factory class for creating two-argument predicates
 */
public final class Predicates2 {
    /**
     * Two-argument equality predicate
     * @return predicate that tests if two arguments are equal
     */
    public static Predicate2<Object, Object> equal();
    
    /**
     * Two-argument inequality predicate
     * @return predicate that tests if two arguments are not equal
     */
    public static Predicate2<Object, Object> notEqual();
    
    /**
     * Two-argument less-than predicate for comparables
     * @return predicate that tests if first < second
     */
    public static <T extends Comparable<? super T>> Predicate2<T, T> lessThan();
    
    /**
     * Two-argument greater-than predicate for comparables
     * @return predicate that tests if first > second
     */
    public static <T extends Comparable<? super T>> Predicate2<T, T> greaterThan();
    
    /**
     * Two-argument less-than-or-equal predicate for comparables
     * @return predicate that tests if first <= second
     */
    public static <T extends Comparable<? super T>> Predicate2<T, T> lessThanOrEqualTo();
    
    /**
     * Two-argument greater-than-or-equal predicate for comparables
     * @return predicate that tests if first >= second
     */
    public static <T extends Comparable<? super T>> Predicate2<T, T> greaterThanOrEqualTo();
    
    /**
     * Predicate testing if first argument is contained in second (iterable)
     * @return predicate that tests membership
     */
    public static Predicate2<Object, Iterable<?>> in();
    
    /**
     * Predicate testing if first argument is instance of second (class)
     * @return predicate that tests instance relationship
     */
    public static Predicate2<Object, Class<?>> instanceOf();
    
    /**
     * Predicate testing if first argument is null and second is ignored
     * @return predicate that tests nullness of first argument
     */
    public static Predicate2<Object, Object> isNull();
    
    /**
     * Predicate testing if first argument is not null and second is ignored
     * @return predicate that tests non-nullness of first argument
     */
    public static Predicate2<Object, Object> notNull();
}

Functions Factory

Factory class providing common functions and function operations.

/**
 * Factory class for creating common functions
 */
public final class Functions {
    /**
     * Identity function that returns its argument unchanged
     * @return function that returns input argument
     */
    public static <T> Function<T, T> getPassThru();
    
    /**
     * Function that returns a fixed value regardless of input
     * @param value fixed value to return
     * @return function that always returns the specified value
     */
    public static <T, V> Function<T, V> getFixedValue(V value);
    
    /**
     * Function that converts objects to strings using toString()
     * @return function that converts objects to strings
     */
    public static Function<Object, String> getToString();
    
    /**
     * Function that parses strings to integers
     * @return function that converts strings to integers
     */
    public static Function<String, Integer> getStringToInteger();
    
    /**
     * Function that parses strings to doubles
     * @return function that converts strings to doubles
     */
    public static Function<String, Double> getStringToDouble();
    
    /**
     * Function that gets the class of an object
     * @return function that returns object's class
     */
    public static Function<Object, Class<?>> getToClass();
    
    /**
     * Function that gets the size/length of collections/strings/arrays
     * @return function that returns size of sized objects
     */
    public static Function<Object, Integer> getSizeOf();
    
    // Chain functions
    /**
     * Chain two functions together (composition)
     * @param function1 first function to apply
     * @param function2 second function to apply to result of first
     * @return composed function
     */
    public static <T, I, V> Function<T, V> chain(Function<? super T, ? extends I> function1, Function<? super I, ? extends V> function2);
    
    /**
     * Chain three functions together
     * @param function1 first function
     * @param function2 second function
     * @param function3 third function
     * @return composed function of all three
     */
    public static <T, I1, I2, V> Function<T, V> chain(Function<? super T, ? extends I1> function1, Function<? super I1, ? extends I2> function2, Function<? super I2, ? extends V> function3);
    
    // Conditional functions
    /**
     * Function that returns first non-null result from trying multiple functions
     * @param functions functions to try in order
     * @return function that returns first non-null result
     */
    @SafeVarargs
    public static <T, V> Function<T, V> firstNotNullValue(Function<? super T, ? extends V>... functions);
    
    /**
     * Function that returns first non-empty string from trying multiple functions
     * @param functions functions to try in order
     * @return function that returns first non-empty string result
     */
    @SafeVarargs
    public static <T> Function<T, String> firstNotEmptyStringValue(Function<? super T, String>... functions);
    
    // Conditional function application
    /**
     * Apply one of two functions based on predicate test
     * @param predicate predicate to test input
     * @param trueFunction function to apply if predicate is true
     * @param falseFunction function to apply if predicate is false
     * @return conditional function
     */
    public static <T, V> Function<T, V> ifTrue(Predicate<? super T> predicate, Function<? super T, ? extends V> trueFunction, Function<? super T, ? extends V> falseFunction);
    
    /**
     * Apply function only if predicate is true, otherwise return null
     * @param predicate predicate to test
     * @param function function to apply if true
     * @return conditional function
     */
    public static <T, V> Function<T, V> ifTrue(Predicate<? super T> predicate, Function<? super T, ? extends V> function);
    
    // Attribute extraction functions
    /**
     * Function that extracts a key using a key function, for use in maps
     * @param keyFunction function to extract key from value
     * @return function for key extraction
     */
    public static <T, K> Function<T, K> getKeyFunction(Function<? super T, K> keyFunction);
    
    /**
     * Function that extracts a value using a value function, for use in maps
     * @param valueFunction function to extract value  
     * @return function for value extraction
     */
    public static <T, V> Function<T, V> getValueFunction(Function<? super T, V> valueFunction);
    
    // Synchronization wrapper
    /**
     * Create synchronized wrapper around a function
     * @param function function to synchronize
     * @return synchronized function wrapper
     */
    public static <T, V> Function<T, V> synchronizedEach(Function<T, V> function);
    
    // Collection-specific functions
    /**
     * Function that converts iterables to lists
     * @return function that creates lists from iterables
     */
    public static <T> Function<Iterable<T>, MutableList<T>> toList();
    
    /**
     * Function that converts iterables to sets
     * @return function that creates sets from iterables
     */
    public static <T> Function<Iterable<T>, MutableSet<T>> toSet();
    
    /**
     * Function that gets the first element from an iterable
     * @return function that returns first element
     */
    public static <T> Function<Iterable<T>, T> firstOfIterable();
    
    /**
     * Function that gets the last element from an iterable
     * @return function that returns last element
     */
    public static <T> Function<Iterable<T>, T> lastOfIterable();
}

Usage Examples:

import org.eclipse.collections.impl.block.factory.Functions;

MutableList<String> strings = Lists.mutable.with("123", "456", "789");

// Basic functions
MutableList<Integer> lengths = strings.collect(Functions.getSizeOf());
MutableList<String> upperCase = strings.collect(String::toUpperCase);

// Chain functions  
Function<String, String> parseAndFormat = Functions.chain(
    Functions.getStringToInteger(),
    Object::toString
);

// Fixed value function
MutableList<String> constants = Lists.mutable.with("a", "b", "c")
    .collect(Functions.getFixedValue("constant"));

// Conditional functions
Function<String, Integer> safeParseInt = Functions.ifTrue(
    Predicates.notNull(),
    Functions.getStringToInteger(),
    Functions.getFixedValue(0)
);

// First non-null value
Function<Person, String> getAnyName = Functions.firstNotNullValue(
    Person::getNickname,
    Person::getFirstName,
    Functions.getFixedValue("Unknown")
);

Comparators Factory

Factory class for creating common comparators.

/**
 * Factory class for creating common comparators
 */
public final class Comparators {
    /**
     * Natural order comparator for comparable objects
     * @return comparator using natural ordering
     */
    public static <T extends Comparable<? super T>> Comparator<T> naturalOrder();
    
    /**
     * Reverse natural order comparator
     * @return comparator using reverse natural ordering  
     */
    public static <T extends Comparable<? super T>> Comparator<T> reverseNaturalOrder();
    
    /**
     * Comparator that compares objects by the result of a function
     * @param function function to extract comparable value
     * @return comparator based on function result
     */
    public static <T, V extends Comparable<? super V>> Comparator<T> byFunction(Function<? super T, ? extends V> function);
    
    /**
     * Comparator that compares objects by function result in reverse order
     * @param function function to extract comparable value
     * @return reverse comparator based on function result
     */
    public static <T, V extends Comparable<? super V>> Comparator<T> byFunctionReverse(Function<? super T, ? extends V> function);
    
    /**
     * Chain multiple comparators together
     * @param comparators comparators to chain
     * @return comparator that applies comparators in sequence
     */
    @SafeVarargs
    public static <T> Comparator<T> chain(Comparator<T>... comparators);
    
    /**
     * Comparator that handles nulls by placing them first
     * @param comparator comparator for non-null values
     * @return null-safe comparator with nulls first
     */
    public static <T> Comparator<T> nullsFirst(Comparator<T> comparator);
    
    /**
     * Comparator that handles nulls by placing them last
     * @param comparator comparator for non-null values
     * @return null-safe comparator with nulls last
     */
    public static <T> Comparator<T> nullsLast(Comparator<T> comparator);
    
    /**
     * Comparator for strings ignoring case
     * @return case-insensitive string comparator
     */
    public static Comparator<String> caseInsensitive();
    
    /**
     * Reverse a comparator's ordering
     * @param comparator comparator to reverse
     * @return reversed comparator
     */
    public static <T> Comparator<T> reverse(Comparator<T> comparator);
    
    /**
     * Create comparator for power set ordering
     * @param comparator element comparator
     * @return power set comparator
     */
    public static <T> Comparator<Iterable<T>> powerSet(Comparator<T> comparator);
    
    // Primitive comparators
    /**
     * Comparator for int values
     * @return int comparator
     */
    public static Comparator<Integer> intNaturalOrder();
    
    /**
     * Comparator for long values  
     * @return long comparator
     */
    public static Comparator<Long> longNaturalOrder();
    
    /**
     * Comparator for double values
     * @return double comparator  
     */
    public static Comparator<Double> doubleNaturalOrder();
}

Procedures Factory

Factory class for creating common procedures.

/**
 * Factory class for creating common procedures
 */
public final class Procedures {
    /**
     * Procedure that does nothing (no-op)
     * @return procedure that performs no action
     */
    public static <T> Procedure<T> noop();
    
    /**
     * Procedure that prints objects using System.out.println
     * @return procedure that prints to standard output
     */
    public static Procedure<Object> println();
    
    /**
     * Procedure that prints objects using System.out.print  
     * @return procedure that prints to standard output without newline
     */
    public static Procedure<Object> print();
    
    /**
     * Procedure that adds elements to a collection
     * @param collection target collection to add to
     * @return procedure that adds elements to collection
     */
    public static <T> Procedure<T> addToCollection(Collection<T> collection);
    
    /**
     * Procedure that removes elements from a collection
     * @param collection target collection to remove from
     * @return procedure that removes elements from collection
     */
    public static <T> Procedure<T> removeFromCollection(Collection<T> collection);
    
    /**
     * Procedure that throws runtime exception
     * @param runtimeException exception to throw
     * @return procedure that throws the specified exception
     */
    public static <T> Procedure<T> throwException(RuntimeException runtimeException);
    
    /**
     * Synchronized wrapper around a procedure
     * @param procedure procedure to synchronize
     * @return synchronized procedure wrapper
     */
    public static <T> Procedure<T> synchronizedEach(Procedure<T> procedure);
    
    /**
     * Chain multiple procedures together
     * @param procedures procedures to execute in sequence
     * @return procedure that executes all procedures
     */
    @SafeVarargs
    public static <T> Procedure<T> chain(Procedure<? super T>... procedures);
    
    /**
     * Conditional procedure execution
     * @param predicate condition to test
     * @param procedure procedure to execute if condition is true
     * @return conditional procedure
     */
    public static <T> Procedure<T> ifTrue(Predicate<? super T> predicate, Procedure<? super T> procedure);
    
    /**
     * Procedure that executes different procedures based on predicate
     * @param predicate condition to test
     * @param trueProcedure procedure if predicate is true
     * @param falseProcedure procedure if predicate is false
     * @return conditional procedure
     */
    public static <T> Procedure<T> ifElse(Predicate<? super T> predicate, Procedure<? super T> trueProcedure, Procedure<? super T> falseProcedure);
    
    // Object mutation procedures
    /**
     * Procedure that sets a field value using reflection
     * @param fieldName name of field to set
     * @param value value to set field to
     * @return procedure that sets field value
     */
    public static <T> Procedure<T> setFieldValue(String fieldName, Object value);
    
    /**
     * Procedure that invokes a method using reflection
     * @param methodName name of method to invoke
     * @param arguments arguments to pass to method
     * @return procedure that invokes method
     */
    public static <T> Procedure<T> invokeMethod(String methodName, Object... arguments);
}

Advanced Functional Patterns

Function Composition

Eclipse Collections supports advanced function composition patterns:

import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;

// Chain multiple transformations
Function<String, Integer> parseAndSquare = Functions.chain(
    Integer::valueOf,           // String -> Integer
    x -> x * x                 // Integer -> Integer (squared)
);

// Conditional function application
Function<String, String> safeToUpper = Functions.ifTrue(
    Predicates.notNull(),      // Only apply if not null
    String::toUpperCase,       // Transform to upper case
    Functions.getFixedValue("") // Return empty string if null
);

// First non-null result
Function<Person, String> getBestContactInfo = Functions.firstNotNullValue(
    Person::getEmail,          // Try email first
    Person::getPhoneNumber,    // Then phone
    Person::getAddress,        // Then address  
    Functions.getFixedValue("No contact info") // Default
);

List<String> inputs = Lists.mutable.with("5", "10", null, "15");
List<Integer> squares = inputs.collect(parseAndSquare); // [25, 100, null, 225]
List<String> safe = inputs.collect(safeToUpper);        // ["5", "10", "", "15"]

Custom Functional Interfaces

Create domain-specific functional interfaces:

// Custom functional interfaces for domain logic
@FunctionalInterface
interface PriceCalculator extends Function<Product, BigDecimal> {
    BigDecimal valueOf(Product product);
    
    default PriceCalculator withTax(BigDecimal taxRate) {
        return product -> this.valueOf(product).multiply(BigDecimal.ONE.add(taxRate));
    }
    
    default PriceCalculator withDiscount(BigDecimal discountPercent) {
        return product -> this.valueOf(product).multiply(BigDecimal.ONE.subtract(discountPercent.divide(BigDecimal.valueOf(100))));
    }
}

@FunctionalInterface  
interface OrderValidator extends Predicate<Order> {
    boolean accept(Order order);
    
    default OrderValidator and(OrderValidator other) {
        return order -> this.accept(order) && other.accept(order);
    }
    
    default OrderValidator or(OrderValidator other) {
        return order -> this.accept(order) || other.accept(order);
    }
}

// Usage
PriceCalculator basePrice = Product::getBasePrice;
PriceCalculator finalPrice = basePrice
    .withDiscount(BigDecimal.valueOf(10))  // 10% discount
    .withTax(BigDecimal.valueOf(0.08));    // 8% tax

OrderValidator hasItems = order -> !order.getItems().isEmpty();
OrderValidator hasCustomer = order -> order.getCustomer() != null;
OrderValidator isValid = hasItems.and(hasCustomer);

List<Product> products = getProducts();
List<BigDecimal> prices = products.collect(finalPrice);

List<Order> orders = getOrders();
List<Order> validOrders = orders.select(isValid);

Performance Considerations

Functional programming in Eclipse Collections is optimized for performance:

  1. Lazy evaluation: Use asLazy() for deferred computation
  2. Primitive specializations: Use primitive functional interfaces to avoid boxing
  3. Method references: Prefer method references over lambdas for better performance
  4. Predicate combination: Use factory methods for optimal predicate combination
// Optimized functional pipeline
MutableList<Integer> numbers = IntLists.mutable.with(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    .toList(); // Convert to object list only when needed

// Efficient lazy chain  
LazyIterable<String> result = numbers
    .asLazy()                           // Switch to lazy evaluation
    .select(x -> x % 2 == 0)           // Filter evens: [2, 4, 6, 8, 10]
    .collect(x -> x * x)               // Square: [4, 16, 36, 64, 100]
    .select(x -> x > 20)               // Filter > 20: [36, 64, 100]
    .collect(Object::toString);        // Convert to strings

// Only computed when terminal operation called
MutableList<String> computed = result.toList(); // ["36", "64", "100"]

// Primitive functional operations avoid boxing
MutableIntList primitiveNumbers = IntLists.mutable.with(1, 2, 3, 4, 5);
long sum = primitiveNumbers
    .select(x -> x % 2 == 0)          // IntPredicate - no boxing
    .collect(x -> x * x)              // IntToIntFunction - no boxing  
    .sum();                           // Primitive sum - no boxing

Install with Tessl CLI

npx tessl i tessl/maven-org-eclipse-collections--eclipse-collections

docs

advanced-features.md

core-interfaces.md

factory-methods.md

functional-programming.md

index.md

primitive-collections.md

tile.json