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

factory-methods.mddocs/

Factory Methods

Eclipse Collections provides static factory classes as the primary entry points for creating all collection types. These factories offer convenient methods for creating mutable, immutable, and fixed-size collections with various initialization options.

Required Imports

import java.util.stream.Stream;
import org.eclipse.collections.api.block.function.Function0;

Capabilities

Lists Factory

Main entry point for creating Lists of all types, located in org.eclipse.collections.impl.factory.Lists.

/**
 * Main factory class for creating Lists
 */
public final class Lists {
    public static final ImmutableListFactory immutable;
    public static final MutableListFactory mutable;
    public static final FixedSizeListFactory fixedSize;
    public static final MultiReaderListFactory multiReader;
    
    /**
     * Adapt a JDK List to Eclipse Collections MutableList
     * @param list JDK List to adapt
     * @return MutableList wrapping the input list
     */
    public static <T> MutableList<T> adapt(List<T> list);
}

interface MutableListFactory {
    /**
     * Create an empty mutable list
     * @return empty MutableList
     */
    <T> MutableList<T> empty();
    
    /**
     * Create a mutable list with elements
     * @param elements varargs elements to include
     * @return MutableList containing the elements
     */
    <T> MutableList<T> with(T... elements);
    
    /**
     * Create a mutable list from an iterable
     * @param items iterable to copy from
     * @return MutableList containing all items
     */
    <T> MutableList<T> withAll(Iterable<? extends T> items);
    
    /**
     * Create a mutable list with initial capacity
     * @param initialCapacity initial capacity
     * @return empty MutableList with specified capacity
     */
    <T> MutableList<T> withInitialCapacity(int initialCapacity);
    
    /**
     * Create a mutable list from a Stream
     * @param stream Stream to copy from
     * @return MutableList containing all stream elements
     */
    <T> MutableList<T> fromStream(Stream<? extends T> stream);
    
    /**
     * Create a mutable list with generated values
     * @param size number of elements to generate
     * @param factory function to generate each element
     * @return MutableList with generated elements
     */
    <T> MutableList<T> withNValues(int size, Function0<? extends T> factory);
    
    /**
     * Create a mutable list copying from an array
     * @param array array to copy from
     * @return MutableList containing all array elements
     */
    <T> MutableList<T> wrapCopy(T... array);
}

interface ImmutableListFactory {
    /**
     * Create an empty immutable list
     * @return empty ImmutableList
     */
    <T> ImmutableList<T> empty();
    
    /**
     * Create an immutable list with elements
     * @param elements varargs elements to include
     * @return ImmutableList containing the elements
     */
    <T> ImmutableList<T> of(T... elements);
    
    /**
     * Create an immutable list from an iterable
     * @param items iterable to copy from
     * @return ImmutableList containing all items
     */
    <T> ImmutableList<T> ofAll(Iterable<? extends T> items);
}

interface FixedSizeListFactory {
    /**
     * Create a fixed-size list with elements
     * @param elements varargs elements to include
     * @return FixedSizeList that cannot be resized
     */
    <T> FixedSizeList<T> with(T... elements);
}

Usage Examples:

import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.ImmutableList;

// Create mutable lists
MutableList<String> emptyList = Lists.mutable.empty();
MutableList<String> names = Lists.mutable.with("Alice", "Bob", "Charlie");
MutableList<Integer> fromIterable = Lists.mutable.withAll(Arrays.asList(1, 2, 3));

// Create immutable lists  
ImmutableList<String> immutableNames = Lists.immutable.of("Alice", "Bob", "Charlie");
ImmutableList<Integer> immutableNumbers = Lists.immutable.ofAll(Arrays.asList(1, 2, 3));

// Adapt JDK collections
List<String> jdkList = new ArrayList<>(Arrays.asList("X", "Y", "Z"));
MutableList<String> adapted = Lists.adapt(jdkList);

// Fixed-size lists
FixedSizeList<String> fixed = Lists.fixedSize.with("A", "B", "C");
// fixed.add("D"); // Would throw UnsupportedOperationException

Sets Factory

Main entry point for creating Sets and performing set operations, located in org.eclipse.collections.impl.factory.Sets.

/**
 * Main factory class for creating Sets and set operations
 */
public final class Sets {
    public static final ImmutableSetFactory immutable;
    public static final MutableSetFactory mutable;
    public static final FixedSizeSetFactory fixedSize;
    public static final MultiReaderSetFactory multiReader;
    
    /**
     * Adapt a JDK Set to Eclipse Collections MutableSet
     * @param set JDK Set to adapt
     * @return MutableSet wrapping the input set
     */
    public static <T> MutableSet<T> adapt(Set<T> set);
    
    /**
     * Compute union of two sets
     * @param setA first set
     * @param setB second set  
     * @return new MutableSet containing union
     */
    public static <E> MutableSet<E> union(Set<? extends E> setA, Set<? extends E> setB);
    
    /**
     * Compute intersection of two sets
     * @param setA first set
     * @param setB second set
     * @return new MutableSet containing intersection
     */
    public static <E> MutableSet<E> intersect(Set<? extends E> setA, Set<? extends E> setB);
    
    /**
     * Compute difference of two sets (setA - setB)
     * @param minuend set to subtract from
     * @param subtrahend set to subtract
     * @return new MutableSet containing difference
     */
    public static <E> MutableSet<E> difference(Set<? extends E> minuend, Set<? extends E> subtrahend);
    
    /**
     * Compute symmetric difference of two sets
     * @param setA first set
     * @param setB second set
     * @return new MutableSet containing symmetric difference
     */
    public static <E> MutableSet<E> symmetricDifference(Set<? extends E> setA, Set<? extends E> setB);
    
    /**
     * Generate power set of a set
     * @param set input set
     * @return MutableSet of all subsets
     */
    public static <T> MutableSet<MutableSet<T>> powerSet(Set<T> set);
    
    /**
     * Compute cartesian product of two sets
     * @param set1 first set
     * @param set2 second set
     * @return LazyIterable of pairs representing cartesian product
     */
    public static <A, B> LazyIterable<Pair<A, B>> cartesianProduct(Set<A> set1, Set<B> set2);
    
    /**
     * Test if one set is a subset of another
     * @param candidate potential subset
     * @param superset potential superset
     * @return true if candidate is subset of superset
     */
    public static <E> boolean isSubsetOf(Set<? extends E> candidate, Set<? extends E> superset);
    
    /**
     * Test if one set is a proper subset of another
     * @param candidate potential proper subset  
     * @param superset potential superset
     * @return true if candidate is proper subset of superset
     */
    public static <E> boolean isProperSubsetOf(Set<? extends E> candidate, Set<? extends E> superset);
}

interface MutableSetFactory {
    <T> MutableSet<T> empty();
    <T> MutableSet<T> with(T... elements);
    <T> MutableSet<T> withAll(Iterable<? extends T> items);
    <T> MutableSet<T> withInitialCapacity(int initialCapacity);
}

interface ImmutableSetFactory {
    <T> ImmutableSet<T> empty();
    <T> ImmutableSet<T> of(T... elements);
    <T> ImmutableSet<T> ofAll(Iterable<? extends T> items);
}

Usage Examples:

import org.eclipse.collections.impl.factory.Sets;

// Basic set creation
MutableSet<String> colors = Sets.mutable.with("red", "green", "blue");
ImmutableSet<Integer> numbers = Sets.immutable.of(1, 2, 3, 4, 5);

// Set algebra operations
Set<String> setA = Sets.mutable.with("A", "B", "C");  
Set<String> setB = Sets.mutable.with("B", "C", "D");

MutableSet<String> union = Sets.union(setA, setB); // [A, B, C, D]
MutableSet<String> intersection = Sets.intersect(setA, setB); // [B, C]  
MutableSet<String> difference = Sets.difference(setA, setB); // [A]
MutableSet<String> symmetric = Sets.symmetricDifference(setA, setB); // [A, D]

// Subset testing
boolean isSubset = Sets.isSubsetOf(
    Sets.mutable.with("A", "B"), 
    Sets.mutable.with("A", "B", "C")
); // true

// Power set
MutableSet<MutableSet<String>> powerSet = Sets.powerSet(Sets.mutable.with("X", "Y"));
// Contains: [], [X], [Y], [X,Y]

Maps Factory

Main entry point for creating Maps, located in org.eclipse.collections.impl.factory.Maps.

/**
 * Main factory class for creating Maps
 */
public final class Maps {
    public static final ImmutableMapFactory immutable;
    public static final MutableMapFactory mutable;
    public static final FixedSizeMapFactory fixedSize;
    
    /**
     * Adapt a JDK Map to Eclipse Collections MutableMap
     * @param map JDK Map to adapt  
     * @return MutableMap wrapping the input map
     */
    public static <K, V> MutableMap<K, V> adapt(Map<K, V> map);
}

interface MutableMapFactory {
    /**
     * Create an empty mutable map
     * @return empty MutableMap
     */
    <K, V> MutableMap<K, V> empty();
    
    /**
     * Create a mutable map with one key-value pair
     * @param key the key
     * @param value the value
     * @return MutableMap containing the pair
     */
    <K, V> MutableMap<K, V> with(K key, V value);
    
    /**
     * Create a mutable map with two key-value pairs
     * @param key1 first key
     * @param value1 first value  
     * @param key2 second key
     * @param value2 second value
     * @return MutableMap containing the pairs
     */
    <K, V> MutableMap<K, V> with(K key1, V value1, K key2, V value2);
    
    /**
     * Create a mutable map with three key-value pairs
     */
    <K, V> MutableMap<K, V> with(K key1, V value1, K key2, V value2, K key3, V value3);
    
    /**
     * Create a mutable map with four key-value pairs  
     */
    <K, V> MutableMap<K, V> with(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4);
    
    /**
     * Create a mutable map from pairs
     * @param keyValuePairs alternating keys and values
     * @return MutableMap containing the pairs
     */
    <K, V> MutableMap<K, V> of(Object... keyValuePairs);
    
    /**
     * Create a mutable map with initial capacity
     * @param initialCapacity initial capacity
     * @return empty MutableMap with specified capacity
     */
    <K, V> MutableMap<K, V> withInitialCapacity(int initialCapacity);
}

interface ImmutableMapFactory {
    <K, V> ImmutableMap<K, V> empty();
    <K, V> ImmutableMap<K, V> of(K key, V value);
    <K, V> ImmutableMap<K, V> of(K key1, V value1, K key2, V value2);
    <K, V> ImmutableMap<K, V> of(K key1, V value1, K key2, V value2, K key3, V value3);
    <K, V> ImmutableMap<K, V> of(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4);
    <K, V> ImmutableMap<K, V> ofAll(Map<? extends K, ? extends V> map);
}

Bags Factory

Main entry point for creating Bags (multisets), located in org.eclipse.collections.impl.factory.Bags.

/**
 * Main factory class for creating Bags (multisets)
 */
public final class Bags {
    public static final ImmutableBagFactory immutable;
    public static final MutableBagFactory mutable;
    public static final MultiReaderBagFactory multiReader;
}

interface MutableBagFactory {
    <T> MutableBag<T> empty();
    <T> MutableBag<T> with(T... elements);
    <T> MutableBag<T> withAll(Iterable<? extends T> items);
}

interface ImmutableBagFactory {
    <T> ImmutableBag<T> empty();
    <T> ImmutableBag<T> of(T... elements);
    <T> ImmutableBag<T> ofAll(Iterable<? extends T> items);
}

Stacks Factory

Main entry point for creating Stacks, located in org.eclipse.collections.impl.factory.Stacks.

/**
 * Main factory class for creating Stacks
 */
public final class Stacks {
    public static final ImmutableStackFactory immutable;
    public static final MutableStackFactory mutable;
}

interface MutableStackFactory {
    <T> MutableStack<T> empty();
    <T> MutableStack<T> with(T... elements);
    <T> MutableStack<T> withAll(Iterable<? extends T> items);
    <T> MutableStack<T> withReversed(T... elements);
}

interface ImmutableStackFactory {
    <T> ImmutableStack<T> empty();
    <T> ImmutableStack<T> of(T... elements);
    <T> ImmutableStack<T> ofAll(Iterable<? extends T> items);
    <T> ImmutableStack<T> ofReversed(T... elements);
}

Multimaps Factory

Main entry point for creating Multimaps, located in org.eclipse.collections.impl.factory.Multimaps.

/**
 * Main factory class for creating Multimaps
 */
public final class Multimaps {
    public static final ImmutableMultimaps immutable;
    public static final MutableMultimaps mutable;
}

/**
 * Container for immutable multimap factories
 */  
public final class ImmutableMultimaps {
    public static final ImmutableListMultimapFactory list;
    public static final ImmutableSetMultimapFactory set;
    public static final ImmutableSortedSetMultimapFactory sortedSet;
    public static final ImmutableBagMultimapFactory bag;
    public static final ImmutableSortedBagMultimapFactory sortedBag;
}

/**
 * Container for mutable multimap factories
 */
public final class MutableMultimaps {
    public static final MutableListMultimapFactory list;
    public static final MutableSetMultimapFactory set;
    public static final MutableSortedSetMultimapFactory sortedSet;
    public static final MutableBagMultimapFactory bag;
    public static final MutableSortedBagMultimapFactory sortedBag;
}

interface MutableListMultimapFactory {
    <K, V> MutableListMultimap<K, V> empty();
    <K, V> MutableListMultimap<K, V> with(K key, V value);
    <K, V> MutableListMultimap<K, V> withAll(Multimap<? extends K, ? extends V> multimap);
}

interface ImmutableListMultimapFactory {
    <K, V> ImmutableListMultimap<K, V> empty();
    <K, V> ImmutableListMultimap<K, V> of(K key, V value);
    <K, V> ImmutableListMultimap<K, V> ofAll(Multimap<? extends K, ? extends V> multimap);
}

Usage Examples:

import org.eclipse.collections.impl.factory.Multimaps;

// Create multimaps
MutableListMultimap<String, Integer> scores = Multimaps.mutable.list.empty();
scores.put("Alice", 95);
scores.put("Alice", 87);
scores.put("Bob", 92);

ImmutableSetMultimap<String, String> tags = Multimaps.immutable.set.of(
    "java", "programming",
    "java", "collections", 
    "eclipse", "java"
);

// Sorted multimaps maintain order
MutableSortedSetMultimap<String, Integer> sorted = Multimaps.mutable.sortedSet.empty();

Common Factory Patterns

All factory interfaces follow consistent patterns:

Creation Methods

  • empty() - Create empty collection
  • with(elements...) / of(elements...) - Create with elements
  • withAll(iterable) / ofAll(iterable) - Create from iterable
  • withInitialCapacity(size) - Create with capacity (where applicable)

Naming Conventions

  • Mutable factories use with* methods
  • Immutable factories use of* methods
  • Fixed-size collections cannot be resized after creation
  • Multi-reader collections are optimized for concurrent read access

Thread Safety

  • Standard collections are not thread-safe
  • MultiReader collections are optimized for multiple readers with single writer
  • Immutable collections are inherently thread-safe

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