CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-core

Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.

Pending
Overview
Eval results
Files

collection-operations.mddocs/

Collection Operations

Utilities for creating, manipulating, and transforming Java collections with null-safe operations through the CollUtil class.

Capabilities

Collection Validation

Check if collections are null or empty with null-safe operations.

/**
 * Check if a collection is empty (null or size == 0)
 * @param collection the collection to check
 * @return true if the collection is empty or null
 */
public static <T> boolean isEmpty(Collection<T> collection);

/**
 * Check if a collection is not empty
 * @param collection the collection to check
 * @return true if the collection is not empty and not null
 */
public static <T> boolean isNotEmpty(Collection<T> collection);

/**
 * Return empty set if provided set is null, otherwise return original set
 * @param set the set to check
 * @return original set or empty set if null
 */
public static <T> Set<T> emptyIfNull(Set<T> set);

/**
 * Return empty list if provided list is null, otherwise return original list
 * @param list the list to check
 * @return original list or empty list if null
 */
public static <T> List<T> emptyIfNull(List<T> list);

Usage Examples:

import cn.hutool.core.collection.CollUtil;
import java.util.*;

// Validation
List<String> list1 = null;
List<String> list2 = new ArrayList<>();
List<String> list3 = Arrays.asList("a", "b", "c");

boolean isEmpty1 = CollUtil.isEmpty(list1);    // true
boolean isEmpty2 = CollUtil.isEmpty(list2);    // true
boolean isEmpty3 = CollUtil.isEmpty(list3);    // false

boolean notEmpty1 = CollUtil.isNotEmpty(list1); // false
boolean notEmpty2 = CollUtil.isNotEmpty(list3); // true

// Safe operations
List<String> safe = CollUtil.emptyIfNull(list1); // Returns Collections.emptyList()

Collection Creation

Create various types of collections with convenient factory methods.

/**
 * Create a new ArrayList with initial values
 * @param values initial values for the list
 * @return new ArrayList containing the values
 */
public static <T> List<T> newArrayList(T... values);

/**
 * Create a new LinkedList with initial values
 * @param values initial values for the list
 * @return new LinkedList containing the values
 */
public static <T> LinkedList<T> newLinkedList(T... values);

/**
 * Create a new HashSet with initial values
 * @param values initial values for the set
 * @return new HashSet containing the values
 */
public static <T> Set<T> newHashSet(T... values);

/**
 * Create a new LinkedHashSet with initial values
 * @param values initial values for the set
 * @return new LinkedHashSet containing the values
 */
public static <T> Set<T> newLinkedHashSet(T... values);

/**
 * Create a new concurrent collection
 * @param values initial values
 * @return new CopyOnWriteArrayList containing the values
 */
public static <T> CopyOnWriteArrayList<T> newCopyOnWriteArrayList(T... values);

Usage Examples:

// Create collections with initial values
List<String> list = CollUtil.newArrayList("a", "b", "c");
Set<Integer> numbers = CollUtil.newHashSet(1, 2, 3, 4, 5);
LinkedList<String> linkedList = CollUtil.newLinkedList("first", "second");

// Thread-safe collections
CopyOnWriteArrayList<String> concurrent = CollUtil.newCopyOnWriteArrayList("item1", "item2");

Set Operations

Perform mathematical set operations like union, intersection, and difference.

/**
 * Union of two collections (preserves duplicates by count)
 * @param coll1 first collection
 * @param coll2 second collection
 * @return union collection as ArrayList
 */
public static <T> Collection<T> union(Collection<T> coll1, Collection<T> coll2);

/**
 * Intersection of two collections
 * @param coll1 first collection
 * @param coll2 second collection
 * @return intersection collection as ArrayList
 */
public static <T> Collection<T> intersection(Collection<T> coll1, Collection<T> coll2);

/**
 * Difference of two collections (elements in coll1 but not in coll2)
 * @param coll1 first collection
 * @param coll2 second collection
 * @return difference collection as ArrayList
 */
public static <T> Collection<T> subtract(Collection<T> coll1, Collection<T> coll2);

/**
 * Symmetric difference (elements in either collection but not in both)
 * @param coll1 first collection
 * @param coll2 second collection
 * @return symmetric difference as ArrayList
 */
public static <T> Collection<T> disjunction(Collection<T> coll1, Collection<T> coll2);

Usage Examples:

List<String> list1 = CollUtil.newArrayList("a", "b", "c", "c");
List<String> list2 = CollUtil.newArrayList("b", "c", "d");

// Set operations
Collection<String> union = CollUtil.union(list1, list2);         // [a, b, c, c, d]
Collection<String> intersection = CollUtil.intersection(list1, list2); // [b, c]
Collection<String> difference = CollUtil.subtract(list1, list2);  // [a, c]
Collection<String> symmetric = CollUtil.disjunction(list1, list2); // [a, c, d]

Collection Transformation

Transform collections using mapping functions and filters.

/**
 * Map collection elements to new values
 * @param collection source collection
 * @param mapper function to transform elements
 * @return new list with transformed elements
 */
public static <T, R> List<R> map(Collection<T> collection, Function<T, R> mapper);

/**
 * Filter collection elements based on predicate
 * @param collection source collection
 * @param predicate filter condition
 * @return new list with filtered elements
 */
public static <T> List<T> filter(Collection<T> collection, Predicate<T> predicate);

/**
 * Extract field values from collection of objects
 * @param collection collection of objects
 * @param fieldName field name to extract
 * @return list of field values
 */
public static <T> List<Object> getFieldValues(Collection<T> collection, String fieldName);

Collection Conversion

Convert between different collection types and to arrays.

/**
 * Convert collection to array
 * @param collection source collection
 * @param componentType array component type
 * @return array containing collection elements
 */
public static <T> T[] toArray(Collection<T> collection, Class<T> componentType);

/**
 * Convert iterable to list
 * @param iterable source iterable
 * @return new ArrayList containing iterable elements
 */
public static <T> List<T> toList(Iterable<T> iterable);

/**
 * Convert array to list
 * @param array source array
 * @return new ArrayList containing array elements
 */
public static <T> List<T> toList(T[] array);

Collection Utilities

Additional utility methods for collection manipulation.

/**
 * Get first element of collection
 * @param collection the collection
 * @return first element or null if empty
 */
public static <T> T getFirst(Collection<T> collection);

/**
 * Get last element of collection
 * @param collection the collection
 * @return last element or null if empty
 */
public static <T> T getLast(Collection<T> collection);

/**
 * Reverse the order of elements in a list
 * @param list the list to reverse
 * @return the same list with reversed order
 */
public static <T> List<T> reverse(List<T> list);

/**
 * Sort collection using natural ordering
 * @param collection collection to sort
 * @return new sorted list
 */
public static <T extends Comparable<T>> List<T> sort(Collection<T> collection);

/**
 * Sort collection using custom comparator
 * @param collection collection to sort
 * @param comparator comparator for sorting
 * @return new sorted list
 */
public static <T> List<T> sort(Collection<T> collection, Comparator<T> comparator);

Usage Examples:

List<String> names = CollUtil.newArrayList("Alice", "Bob", "Charlie");

// Collection utilities
String first = CollUtil.getFirst(names);     // "Alice"
String last = CollUtil.getLast(names);       // "Charlie"

// Transformation
List<Integer> lengths = CollUtil.map(names, String::length); // [5, 3, 7]
List<String> filtered = CollUtil.filter(names, name -> name.length() > 3); // ["Alice", "Charlie"]

// Sorting
List<String> sorted = CollUtil.sort(names); // Natural alphabetical order
List<String> reversed = CollUtil.reverse(CollUtil.newArrayList(names)); // ["Charlie", "Bob", "Alice"]

// Conversion
String[] array = CollUtil.toArray(names, String.class);
List<String> backToList = CollUtil.toList(array);

Specialized Collection Classes

CollUtil also provides access to specialized collection implementations:

// Queue implementations
ArrayBlockingQueue<T> newArrayBlockingQueue(int capacity);
LinkedBlockingDeque<T> newLinkedBlockingDeque();

// Thread-safe collections
CopyOnWriteArrayList<T> newCopyOnWriteArrayList(T... values);

Integration with Iterator Utilities

CollUtil works closely with IterUtil for iterator-based operations. Many operations that work with iterators are provided in IterUtil rather than CollUtil to avoid duplication.

Install with Tessl CLI

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

docs

collection-operations.md

compression-operations.md

date-time-operations.md

file-io-operations.md

index.md

object-operations.md

random-operations.md

string-operations.md

type-conversion.md

url-operations.md

xml-operations.md

tile.json