CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-all

A comprehensive Java utility library providing static method wrappers for common operations to reduce API learning costs and improve development efficiency

Pending
Overview
Eval results
Files

collection-utilities.mddocs/

Collection Utilities

Comprehensive collection manipulation utilities through CollUtil, ListUtil, SetUtil, and MapUtil classes, providing creation, filtering, transformation, and analysis operations.

Import

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.collection.SetUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IterUtil;

Collection Creation

List Creation

// Create lists
public static <T> List<T> newArrayList();
public static <T> List<T> newArrayList(T... values);
public static <T> List<T> newArrayList(Collection<T> collection);
public static <T> List<T> newArrayList(Iterable<T> iterable);

// Create with capacity
public static <T> List<T> newArrayList(int initialCapacity);

// Create other list types
public static <T> LinkedList<T> newLinkedList();
public static <T> LinkedList<T> newLinkedList(T... values);

Set Creation

// Create sets
public static <T> Set<T> newHashSet();
public static <T> Set<T> newHashSet(T... values);
public static <T> Set<T> newHashSet(Collection<T> collection);

// Create other set types
public static <T> LinkedHashSet<T> newLinkedHashSet();
public static <T> TreeSet<T> newTreeSet();

Map Creation

// Create maps
public static <K, V> Map<K, V> newHashMap();
public static <K, V> Map<K, V> newHashMap(int size, boolean isOrder);

// Create with initial data
public static <K, V> Map<K, V> of(K key, V value);
public static <K, V> Map<K, V> of(K key1, V value1, K key2, V value2);
// ... more overloads for up to 5 key-value pairs

// Builder pattern
public static <K, V> MapBuilder<K, V> builder();
public static <K, V> MapBuilder<K, V> builder(Map<K, V> map);

// Create other map types
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap();
public static <K, V> TreeMap<K, V> newTreeMap();
public static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap();

Usage Examples:

// Create collections with initial data
List<String> names = CollUtil.newArrayList("Alice", "Bob", "Charlie");
Set<Integer> numbers = CollUtil.newHashSet(1, 2, 3, 4, 5);
Map<String, Integer> scores = MapUtil.of(
    "Alice", 85,
    "Bob", 92,
    "Charlie", 78
);

// Builder pattern for maps
Map<String, Object> config = MapUtil.builder(new HashMap<String, Object>())
    .put("timeout", 5000)
    .put("retries", 3)
    .put("debug", true)
    .build();

Collection Validation

Empty and Null Checks

// Check if empty
public static boolean isEmpty(Collection<?> collection);
public static boolean isNotEmpty(Collection<?> collection);

// Check if empty for maps
public static boolean isEmpty(Map<?, ?> map);
public static boolean isNotEmpty(Map<?, ?> map);

// Check if empty for iterables
public static boolean isEmpty(Iterable<?> iterable);

Size Operations

// Get size safely
public static int size(Collection<?> collection);
public static int size(Map<?, ?> map);
public static int size(Iterable<?> iterable);

// Check size conditions
public static boolean hasItem(Iterable<?> iterable);

Collection Manipulation

Adding and Removing Elements

// Add elements safely
public static <T> List<T> addAll(Collection<T> coll, T... elements);
public static <T> List<T> addAll(Collection<T> coll, Iterable<T> iterable);

// Remove elements
public static <T> boolean removeAny(Collection<T> coll, T... elements);
public static <T> boolean removeAll(Collection<T> coll, Collection<T> elements);

// Clear collections safely
public static void clear(Collection<?>... collections);

Collection Conversion

// Convert between collection types
public static <T> List<T> toList(Iterable<T> iterable);
public static <T> Set<T> toSet(Iterable<T> iterable);

// Convert arrays
public static <T> List<T> toList(T[] array);
public static <T> Set<T> toSet(T[] array);

// Convert with mapping
public static <T, R> List<R> map(Iterable<T> iterable, Function<T, R> mapper);
public static <T, R> Set<R> mapToSet(Iterable<T> iterable, Function<T, R> mapper);

Filtering and Searching

Filtering Operations

// Filter collections
public static <T> List<T> filter(Collection<T> collection, Predicate<T> predicate);
public static <T> List<T> filterNew(Collection<T> collection, Filter<T> filter);

// Find operations
public static <T> T findOne(Iterable<T> collection, Predicate<T> predicate);
public static <T> int findIndex(Collection<T> collection, Predicate<T> predicate);

// Check conditions
public static <T> boolean contains(Collection<T> collection, T value);
public static <T> boolean containsAny(Collection<T> collection1, Collection<T> collection2);
public static <T> boolean containsAll(Collection<T> collection1, Collection<T> collection2);

Collection Matching

// Match operations
public static <T> boolean allMatch(Iterable<T> iterable, Predicate<T> predicate);
public static <T> boolean anyMatch(Iterable<T> iterable, Predicate<T> predicate);
public static <T> boolean noneMatch(Iterable<T> iterable, Predicate<T> predicate);

Usage Examples:

List<User> users = getUserList();

// Filter active users
List<User> activeUsers = CollUtil.filter(users, User::isActive);

// Find first admin user
User admin = CollUtil.findOne(users, user -> "admin".equals(user.getRole()));

// Check if any user is under 18
boolean hasMinors = CollUtil.anyMatch(users, user -> user.getAge() < 18);

// Get user names
List<String> names = CollUtil.map(users, User::getName);

Set Operations

Set Mathematics

// Union, intersection, difference
public static <T> Set<T> union(Collection<T> coll1, Collection<T> coll2);
public static <T> Set<T> intersection(Collection<T> coll1, Collection<T> coll2);
public static <T> Set<T> difference(Collection<T> coll1, Collection<T> coll2);

// Disjoint check
public static boolean disjoint(Collection<?> coll1, Collection<?> coll2);

Usage Examples:

Set<String> set1 = CollUtil.newHashSet("A", "B", "C");
Set<String> set2 = CollUtil.newHashSet("B", "C", "D");

Set<String> unionSet = CollUtil.union(set1, set2);         // [A, B, C, D]
Set<String> intersect = CollUtil.intersection(set1, set2); // [B, C]
Set<String> diff = CollUtil.difference(set1, set2);       // [A]

Map Operations

Map Utilities

// Get values safely
public static <K, V> V get(Map<K, V> map, K key, V defaultValue);
public static <K, V> V get(Map<K, V> map, K key, Supplier<V> defaultSupplier);

// Put operations
public static <K, V> V putIfAbsent(Map<K, V> map, K key, V value);
public static <K, V> Map<K, V> putAll(Map<K, V> map, Map<K, V> otherMap);

// Remove operations
public static <K, V> V removeAny(Map<K, V> map, K... keys);

// Check operations
public static boolean isEmpty(Map<?, ?> map);
public static boolean isNotEmpty(Map<?, ?> map);

Map Transformation

// Reverse map (swap keys and values)
public static <K, V> Map<V, K> reverse(Map<K, V> map);

// Filter maps
public static <K, V> Map<K, V> filter(Map<K, V> map, Predicate<Entry<K, V>> predicate);

// Transform maps
public static <K, V, R> Map<K, R> transformValue(Map<K, V> map, Function<V, R> transformer);
public static <K, V, R> Map<R, V> transformKey(Map<K, V> map, Function<K, R> transformer);

MapBuilder Class

public class MapBuilder<K, V> {
    // Add entries
    public MapBuilder<K, V> put(K key, V value);
    public MapBuilder<K, V> putAll(Map<K, V> map);
    
    // Conditional puts
    public MapBuilder<K, V> putIfAbsent(K key, V value);
    public MapBuilder<K, V> putIfNotNull(K key, V value);
    
    // Build result
    public Map<K, V> build();
}

Usage Examples:

Map<String, Object> userMap = MapUtil.builder(new HashMap<String, Object>())
    .put("id", 1)
    .put("name", "John")
    .putIfNotNull("email", user.getEmail())
    .build();

// Safe get with default
String role = MapUtil.get(userMap, "role", "user");

// Transform map values
Map<String, String> upperNames = MapUtil.transformValue(nameMap, String::toUpperCase);

Iterator Utilities

Iterator Operations

// Check if empty
public static boolean isEmpty(Iterator<?> iterator);
public static boolean hasNext(Iterator<?> iterator);

// Get elements
public static <T> T getFirst(Iterable<T> iterable);
public static <T> T getLast(Iterable<T> iterable);
public static <T> T get(Iterable<T> iterable, int index);

// Convert to collections
public static <T> List<T> toList(Iterator<T> iterator);
public static <T> Set<T> toSet(Iterator<T> iterator);

// Join elements
public static <T> String join(Iterator<T> iterator, CharSequence delimiter);
public static <T> String join(Iterable<T> iterable, CharSequence delimiter);

Pagination and Slicing

List Pagination

// Paginate lists
public static <T> List<List<T>> split(List<T> list, int size);
public static <T> List<T> sub(List<T> list, int start, int end);
public static <T> List<T> page(List<T> list, int pageNo, int pageSize);

Usage Examples:

List<Integer> numbers = CollUtil.range(1, 100); // [1, 2, 3, ..., 100]

// Split into chunks of 10
List<List<Integer>> chunks = CollUtil.split(numbers, 10);

// Get page 3 with 20 items per page  
List<Integer> page3 = CollUtil.page(numbers, 3, 20); // [41, 42, ..., 60]

// Get sublist
List<Integer> subset = CollUtil.sub(numbers, 10, 20); // [11, 12, ..., 20]

Collection Generation

Range and Sequence Generation

// Generate number ranges
public static List<Integer> range(int stop);
public static List<Integer> range(int start, int stop);
public static List<Integer> range(int start, int stop, int step);

// Generate repeated elements
public static <T> List<T> repeat(T element, int count);

Usage Examples:

// Generate ranges
List<Integer> oneToTen = CollUtil.range(1, 11);        // [1, 2, 3, ..., 10]
List<Integer> evens = CollUtil.range(2, 21, 2);       // [2, 4, 6, ..., 20]

// Repeat elements
List<String> dots = CollUtil.repeat(".", 5);          // [".", ".", ".", ".", "."]

All collection utilities are null-safe and handle edge cases gracefully. They provide consistent behavior across different collection types and offer both mutable and immutable operation variants where appropriate.

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-all

docs

additional-utilities.md

bean-object-manipulation.md

collection-utilities.md

core-string-operations.md

cryptographic-operations.md

database-access.md

date-time-handling.md

file-io-operations.md

http-client-operations.md

index.md

json-processing.md

tile.json