Logo
Menu

CollectionUtils” Methods

CollectionUtils.chunk(collection, [size=1])

sourcemaven central repository

Creates a collection of elements split into groups the length of size. If collection can't be split evenly, the final chunk will be the remaining elements.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to process.
  2. size (number): The length of each chunk.

Returns

(Collections): Returns the new collection of chunks.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {

    System.out.println(CollectionUtils.chunk(null, 2));
    // => []
    
    List<String> input = Arrays.asList("a", "b", "c", "d");
    int size = 2;
    List<List<String>> result = CollectionUtils.chunk(input, 2);
    System.out.println(result);
    // => [["a", "b"], ["c", "d"]]
    
    }
}
Try in REPL

CollectionUtils.compact(collection)

sourcemaven central repository

Creates a collection with all falsey values removed. The values (false, null, 0, "", NaN) are falsey.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to compact.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {

    System.out.println(CollectionUtils.compact(null));
    // => []
    
    List<String> input = Arrays.asList("a", null, "", "d");
    List<String> result = CollectionUtils.compact(input);
    System.out.println(result);
    // => ["a", "d"]
    
    }
}
Try in REPL

CollectionUtils.concat(collection, [values])

sourcemaven central repository

Creates a new collection concatenating collection with any additional collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to concatenate.
  2. [values] (...Collections): The collection to concatenate.

Returns

(Collections): Returns the new concatenated collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {

    System.out.println(CollectionUtils.concat(null, Arrays.asList(1, 2, 3)));
    // => [1, 2, 3]
    
    List<Integer> input = Arrays.asList(1);
    List<Integer> result = CollectionUtils.concat(input, Arrays.asList(2, 3, 4), Arrays.asList(5, 6, null));
    System.out.println(result);
    // => [1, 2, 3, 4, 5, 6]
    
    }
}
Try in REPL

CollectionUtils.difference(collection, [values])

sourcemaven central repository

Creates a new collection with elements from the first collection that are not in any of the other given collections.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [values] (...Collections): The collection of values to exclude.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {

    System.out.println(CollectionUtils.compact(null));
    // => []
    
    List<String> input = Arrays.asList("a", null, "", "d");
    List<String> result = CollectionUtils.compact(input);
    System.out.println(result);
    // => ["a", "d"]
    
    }
}
Try in REPL

CollectionUtils.differenceBy(collection, [predicate], [values])

sourcemaven central repository

Creates a new collection with elements from the first collection that are not in any of the other given collections. The comparison is performed based on a criterion derived using the provided predicate function.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [predicate] (Predicate): The function invoked per iteration.
  3. [values] (...Collections): The collection to exclude.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {

    System.out.println(CollectionUtils.differenceBy(null, x -> x > 2 , Arrays.asList(1, 2)));
    // => []
    
    System.out.println(CollectionUtils.differenceBy(Arrays.asList(1, 2), x -> x > 2 , null));
    // => [1, 2]
    
    List<Double> input = Arrays.asList(2.1, 1.2);
    List<Double> exclude = Arrays.asList(2.3, 3.4);
    List<Double> result = CollectionUtils.differenceBy(input, x -> x < 2, exclude);
    System.out.println(result);
    // => [1.2]
    
    }
}
Try in REPL

CollectionUtils.differenceWith(collection, [comparator], [values])

sourcemaven central repository

Finds the difference between a collection and multiple other collections, based on a custom comparator.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [comparator] (BiPredicate): The comparator invoked per element.
  3. [values] (...Collections): The collection to exclude.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {

    System.out.println(CollectionUtils.differenceWith(null, Objects::equals, Arrays.asList(1, 2)));
    // => []
    
    System.out.println(CollectionUtils.differenceWith(Arrays.asList(1, 2), Objects::equals, null));
    // => [1, 2]
    
    List<String> input = Arrays.asList("Apple", "Banana", "Cherry");
    List<String> exclude = Arrays.asList("banana", "cherry");
    List<String> result = CollectionUtils.differenceWith(input, String::equalsIgnoreCase, exclude);
    System.out.println(result);
    // => ["Apple"];
    
    }
}
Try in REPL

CollectionUtils.drop(collection, [n])

sourcemaven central repository

Creates a slice of collection with n elements dropped from the beginning.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [n] (int): The number of elements to drop.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {
    
    System.out.println(CollectionUtils.drop(null, 1));
    // => []
    
    List<Integer> list = Arrays.asList(1, 2, 3);
    List<Integer> result = CollectionUtils.drop(list, 2);
    System.out.println(result);
    // => [3]
    
    }
}
Try in REPL

CollectionUtils.dropRight(collection, [n])

sourcemaven central repository

Creates a slice of collection with n elements dropped from the end.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [n] (int): The number of elements to drop.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;
public class Main {
  public static void main(String[] args) {
    
    System.out.println(CollectionUtils.dropRight(null, 1));
    // => []
    
    List<Integer> list = Arrays.asList(1, 2, 3);
    List<Integer> result = CollectionUtils.dropRight(list, 2);
    System.out.println(result);
    // => [1]
    
    }
}
Try in REPL

CollectionUtils.dropRightWhile(collection, [predicate])

sourcemaven central repository

Creates a slice of the collection with elements dropped from the end until.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.dropRightWhile(null, (Integer num) -> num.compareTo(3) < 0));
        // => []

        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.dropRightWhile(collection, (Integer num) -> num.compareTo(3) > 0);
        System.out.println(result);
        // => [1, 2, 3]
    }
}
Try in REPL

CollectionUtils.dropWhile(collection, [predicate])

sourcemaven central repository

Creates a slice of the collection with elements dropped from beginning until the predicate returns false.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.dropWhile(null, (Integer num) -> num.compareTo(3) < 0));
        // => []

        List<Integer> collection = Arrays.asList(1, 2, 3, 4);
        List<Integer> result = CollectionUtils.dropWhile(collection, (Integer num) -> num.compareTo(3) < 0);
        System.out.println(result);
        // => [3, 4]
    }
}
Try in REPL

CollectionUtils.fill(list, value, [start], [end])

sourcemaven central repository

Fills elements of list with value from start to end.

Since

1.0.2

Arguments

  1. list (List): The list to modify.
  2. [value] (<T>): The value to fill into the list.
  3. [start] (int): The start position.
  4. [end] (int): The end position.

Returns

(List): Returns list.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.fill(null, 7, 0, 4));
        // => []

        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.fill(collection, 0, 1, 4);
        System.out.println(result);
        // => [1, 0, 0, 0, 5]
    }
}
Try in REPL

CollectionUtils.findIndex(collection, [predicate], [fromIndex])

sourcemaven central repository

Finds the index of the first element in the collection that satisfies the provided predicate, starting from the specified index.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [predicate] (Predicate): The function invoked per iteration.
  3. [fromIndex] (int): The index to search from.

Returns

(int): Returns the index of the found element, else -1.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.findIndex(null, (Integer x) -> x == 3, 0));
        // => -1

        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        int result = CollectionUtils.findIndex(collection, (Integer x) -> x.equals(3), 0);
        System.out.println(result);
        // => 2
    }
}
Try in REPL

CollectionUtils.findLastIndex(collection, [predicate], [fromIndex])

sourcemaven central repository

Finds the index of the last element in the collection that satisfies the provided predicate, starting from the specified index.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [predicate] (Predicate): The function invoked per iteration.
  3. [fromIndex] (int): The index to search from.

Returns

(int): Returns the index of the found element, else -1.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.findLastIndex(null, (Integer x) -> x == 3, 0));
        // => -1

        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        int result = CollectionUtils.findLastIndex(collection, (Integer x) -> x.equals(3), 4);
        System.out.println(result);
        // => 2
    }
}
Try in REPL

CollectionUtils.head(collection)

sourcemaven central repository

Gets the first element of collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.

Returns

(Optional<T>): Returns the first element of the collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.head(null));
        // => Optional.empty()
        
        List<String> input = Arrays.asList(null, "second");
        System.out.println(CollectionUtils.head(input));
        // => Optional.empty()

        List<Integer> collection = Arrays.asList(1, 2, 3);
        System.out.println(CollectionUtils.head(collection));
        // => Optional[1]
    }
}
Try in REPL

CollectionUtils.flatten(collection)

sourcemaven central repository

Flattens collection a single level deep.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to flatten.

Returns

(Collections): Returns the new flattened collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.flatten(null));
        // []
        
        List<Object> collection = Arrays.asList(Arrays.asList(1, 2, null), Arrays.asList(3, 4, null));
        List<Object> result = CollectionUtils.flatten(collection);
        System.out.println(result);
        // => [1, 2, null, 3, 4, null]
    }
}
Try in REPL

CollectionUtils.flattenDeep(collection)

sourcemaven central repository

Recursively flattens collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to flatten.

Returns

(Collections): Returns the new flattened collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.flattenDeep(null));
        // []
        
        List<Object> collection = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, Arrays.asList(4, 5)));
        // [[1,2], [3, [4,5]]]
        List<Object> result = CollectionUtils.flattenDeep(collection);
        System.out.println(result);
        // => [1, 2, 3, 4, 5]
    }
}
Try in REPL

CollectionUtils.flattenDepth(collection, [depth])

sourcemaven central repository

Recursively flattens collection up to depth times.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to flatten.
  2. depth (int): The maximum recursion depth.

Returns

(Collections): Returns the new flattened collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.flattenDepth(null, 1));
        // []
        
        List<Object> collection = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, Arrays.asList(4, 5)));
        // [[1,2], [3, [4,5]]]
        List<Object> result = CollectionUtils.flattenDepth(collection, 1);
        System.out.println(result);
        // => [1, 2, 3, [4, 5]]
    }
}
Try in REPL

CollectionUtils.fromPairs(pairs)

sourcemaven central repository

This method returns a Map composed of key-value pairs.

Since

1.0.2

Arguments

  1. pairs (List<List<T>>): The list of key-value pairs to convert into a map.

Returns

(Map<T, T>): Returns the new map.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.fromPairs(null));
        // => empty Map
        
        List<List<String>> pairs = Arrays.asList(
          Arrays.asList("key1", "value1"),
          Arrays.asList("key2"),
          Arrays.asList("key3", "value3")
        );
        Map<String, String> result = CollectionUtils.fromPairs(pairs);
        System.out.println(result);
        // => Map of { "key1"="value1", "key3"="value3" }
    }
}
Try in REPL

CollectionUtils.indexOf(collection, [value])

sourcemaven central repository

Gets the index at which the first occurrence of value is found in collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [value] (<T>): The value to search for.

Returns

(int): Returns the index of the matched value, else -1.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.indexOf(null, 3));
        // => -1
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        int result = CollectionUtils.indexOf(collection, 3);
        System.out.println(result);
        // => 2
    }
}
Try in REPL

CollectionUtils.indexOf(collection, [value], [fromIndex])

sourcemaven central repository

Gets the index at which the first occurrence of value is found in collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [value] (<T>): The value to search for.
  3. [fromIndex] (int): The index to search from.

Returns

(int): Returns the index of the matched value, else -1.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.indexOf(null, 3, 0));
        // => -1
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        int result = CollectionUtils.indexOf(collection, 3, 1);
        System.out.println(result);
        // => 2
    }
}
Try in REPL

CollectionUtils.initial(collection)

sourcemaven central repository

Gets all but the last element of collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.initial(null));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4);
        List<Integer> result = CollectionUtils.initial(collection);
        System.out.println(result);
        // => [1, 2, 3]
    }
}
Try in REPL

CollectionUtils.intersection(collection, [values])

sourcemaven central repository

Creates a collection of unique values that are included in all given collections.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [values] (...Collections): The values to inspect.

Returns

(Collections): Returns the new collection of intersecting values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.intersection(null, Arrays.asList(1, 2, 3, 4)));
        // => []
        
        System.out.println(CollectionUtils.intersection(Arrays.asList(1, 2, 3, 4),  null));
        // => []
        
        List<Integer> collection1 = Arrays.asList(1, 2, null);
        List<Integer> collection2 = Arrays.asList(2, 3);
        List<Integer> collection3 = Arrays.asList(2, 4, 5);
        List<Integer> result = CollectionUtils.intersection(collection1, collection2, collection3);
        System.out.println(result);
        // => [2]
    }
}
Try in REPL

CollectionUtils.intersectionBy(collection, [predicate], [values])

sourcemaven central repository

Returns a new collection containing the elements that are present in all the provided collections, based on the result of applying the predicate function to generate the criterion for comparison.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [predicate] (Predicate): The function invoked per iteration.
  3. [values] (...Collections): The collection to inspect.

Returns

(Collections): Returns the new collection of intersecting values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.intersectionBy(null, x -> x % 2 == 0, Arrays.asList(1, 2, 3, 4)));
        // => []
        
        System.out.println(CollectionUtils.intersectionBy(Arrays.asList(1, 2, 3, 4), x -> x % 2 == 0, null));
        // => []
        
        List<Integer> collection1 = Arrays.asList(1, 2, 3, 4);
        List<Integer> collection2 = Arrays.asList(2, 3, 4, 5);
        List<Integer> collection3 = Arrays.asList(3, 4, 6);
        List<Integer> result = CollectionUtils.intersectionBy(collection1, x -> x % 2 == 0, collection2, collection3);
        System.out.println(result);
        // => [4]
    }
}
Try in REPL

CollectionUtils.intersectionWith(collection, [comparator], [values])

sourcemaven central repository

Computes the intersection of multiple collections using a custom comparator.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [comparator] (BiPredicate): The comparator invoked per element.
  3. [values] (...Collections): The values to inspect.

Returns

(Collections): Returns the new collection of intersecting values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.intersectionWith(null, (a, b) -> a == b, Arrays.asList(1, 2, 3, 4)));
        // => []
        
        System.out.println(CollectionUtils.intersectionWith(Arrays.asList(1, 2, 3, 4), (a, b) -> a == b, null));
        // => []
        
        List<Integer> collection1 = Arrays.asList(1, 2, 3, 4);
        List<Integer> collection2 = Arrays.asList(2, 3, 4, 5);
        List<Integer> collection3 = Arrays.asList(3, 4, 6);
        List<Integer> result = CollectionUtils.intersectionWith(collection1, Objects::equals, collection2, collection3);
        System.out.println(result);
        // => [3, 4]
    }
}
Try in REPL

CollectionUtils.join(collection, [separator=','])

sourcemaven central repository

Converts all elements in collection into a string separated by separator.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [separator] (String): The element separator.

Returns

(String): Returns the joined string.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        System.out.println(CollectionUtils.join(null, ","));
        // => ""
        
        List<String> collection1 = Arrays.asList("apple", "banana", "cherry");
        System.out.println(CollectionUtils.join(collection1, null));
        // => "apple,banana,cherry"
        
        List<Integer> collection2 = Arrays.asList(1, 2, 3);
        System.out.println(CollectionUtils.join(collection2, "-"));
        // => "1-2-3"
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        String result = CollectionUtils.join(collection, ", ");
        System.out.println(result);
        // => "apple, banana, cherry"
    }
}
Try in REPL

CollectionUtils.last(collection)

sourcemaven central repository

Gets the last element of collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.

Returns

(Optional<T>): Returns the last element of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.last(null));
        // => Optional.empty();
        
        List<Integer> collection = Arrays.asList(1, 2, 3);
        Optional<Integer> result = CollectionUtils.last(collection);
        System.out.println(result);
        // => Optional[3]
    }
}
Try in REPL

CollectionUtils.lastIndexOf(collection, value)

sourcemaven central repository

Finds the last index of a value in a collection, searching from right to left.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [value] (<T>): The value to search for.

Returns

(int): Returns the index of the found element, else -1.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.lastIndexOf(null, 2));
        // => -1
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 2);
        int result = CollectionUtils.lastIndexOf(collection, 2);
        System.out.println(result);
        // => 3
    }
}
Try in REPL

CollectionUtils.lastIndexOf(collection, value, [fromIndex])

sourcemaven central repository

Finds the last index of a value in a collection, searching from right to left.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [value] (<T>): The value to search for.
  3. [fromIndex] (int): The index to search from.

Returns

(int): Returns the index of the found element, else -1.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.lastIndexOf(null, 2, 3));
        // => -1
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 2);
        int result = CollectionUtils.lastIndexOf(collection, 2, 3);
        System.out.println(result);
        // => 3
    }
}
Try in REPL

CollectionUtils.nth(collection, [n])

sourcemaven central repository

Gets the element at index n of collection. If n is negative, the nth element from the end is returned.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [n] (int): The index of the element to return.

Returns

(Optional<T>): Returns the nth element of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.nth(null, 1));
        // => Optional.empty();
        
        List<String> collection1 = Arrays.asList("apple", null, "cherry");
        System.out.println(CollectionUtils.nth(collection1, 1));
        // => Optional.empty();
        
        List<String> collection2 = Arrays.asList("apple", "banana", "cherry");
        Optional<String> result = CollectionUtils.nth(collection2, 1);
        System.out.println(result);
        // => Optional[banana]
    }
}
Try in REPL

CollectionUtils.pull(collection, [values])

sourcemaven central repository

Removes all elements from the collection that are present in the specified values.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to modify.
  2. [values] (...Collections): The values to remove.

Returns

(Collections): Returns collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        
        CollectionUtils.pull(collection, "banana", "cherry");
        System.out.println(collection);
        // => collection = ["apple"]
    }
}
Try in REPL

CollectionUtils.pullAll(collection, values)

sourcemaven central repository

Removes all elements from the collection that are present in any of the specified values.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to modify.
  2. [values] (...Collections): The values to remove.

Returns

(Collections): Returns collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        
        CollectionUtils.pullAll(collection, Arrays.asList("banana", "cherry"));
        System.out.println(collection);
        // => collection = ["apple"]
    }
}
Try in REPL

CollectionUtils.pullAllBy(collection, values, [iteratee])

sourcemaven central repository

Removes elements from the collection based on the result of applying an iteratee function to each element and value.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to modify.
  2. [values] (...Collections): The values to remove.
  3. [iteratee] (Function): The iteratee invoked per element.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        
        CollectionUtils.pullAll(collection, Arrays.asList("banana", "cherry"));
        System.out.println(collection);
        // => collection = ["apple"]
    }
}
Try in REPL

CollectionUtils.pullAllWith(collection, values, [comparator])

sourcemaven central repository

Removes elements from the collection that match any element in the values collection based on the provided comparator.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to modify.
  2. [values] (...Collections): The values to remove.
  3. [comparator] (BiPredicate): The comparator invoked per element.

Returns

(Collections): Returns collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        
        CollectionUtils.pullAllWith(collection, Arrays.asList("banana", "cherry"), String::equalsIgnoreCase);
        System.out.println(collection);
        // => collection = ["apple"]
    }
}
Try in REPL

CollectionUtils.pullAt(collection, [indexes])

sourcemaven central repository

Removes and returns the elements from the collection at the specified indexes.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to modify.
  2. [indexes] (List<Integer>): The indexes of elements to remove.

Returns

(Collections): Returns the new collection of removed elements.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.pullAt(null, Arrays.asList(1, 3)));
        // => []
        
        System.out.println(CollectionUtils.pullAt(Arrays.asList(1, 3), null));
        // => []
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry", "date");
        List<String> result = CollectionUtils.pullAt(collection, Arrays.asList(1, 3));
        System.out.println(result);
        // => result = ["banana", "date"]
    }
}
Try in REPL

CollectionUtils.remove(collection, [predicate])

sourcemaven central repository

Removes and returns all elements from the collection that match the given predicate.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to modify.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Collections): Returns the new collection of removed elements.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.remove(null, s -> ((String) s).startsWith("b")));

        // => []
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry", "date");
        List<String> result = CollectionUtils.remove(collection, s -> ((String) s).startsWith("b"));
        System.out.println(result);
        // => result = ["banana"]
    }
}
Try in REPL

CollectionUtils.reverse(collection)

sourcemaven central repository

Reverses collection so that the first element becomes the last, the second element becomes the second to last, and so on.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to modify.

Returns

(Collections): Returns collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.reverse(null));
        // => []
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        List<String> result = CollectionUtils.reverse(collection);
        System.out.println(result);
        // => ["cherry", "banana", "apple"]
    }
}
Try in REPL

CollectionUtils.slice(collection, [start])

sourcemaven central repository

Creates a slice of the collection from the specified start index to the end.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to slice.
  2. [start] (int): The start position.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.slice(null, 1));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.slice(collection, 1);
        System.out.println(result);
        // => [2, 3, 4, 5]
    }
}
Try in REPL

CollectionUtils.slice(collection, [start], [end])

sourcemaven central repository

Creates a slice of the collection from the specified start index to the end.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to slice.
  2. [start] (int): The start position.
  3. [end] (int): The end position.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.slice(null, 1, 4));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.slice(collection, 1, 4);
        System.out.println(result);
        // => [2, 3, 4]
    }
}
Try in REPL

CollectionUtils.tail(collection)

sourcemaven central repository

Gets all but the first element of collection.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.tail(null));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.tail(collection);
        System.out.println(result);
        // => result = [2, 3, 4, 5]
    }
}
Try in REPL

CollectionUtils.take(collection, [n])

sourcemaven central repository

Creates a slice of collection with n elements taken from the beginning.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [n] (int): The number of elements to take.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.take(null, 3));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.take(collection, 3);
        System.out.println(result);
        // => [1, 2, 3]
    }
}
Try in REPL

CollectionUtils.takeRight(collection, [n])

sourcemaven central repository

Creates a slice of collection with n elements taken from the end.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [n] (int): The number of elements to take.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.takeRight(null, 3));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.takeRight(collection, 3);
        System.out.println(result);
        // => [3, 4, 5]
    }
}
Try in REPL

CollectionUtils.takeRightWhile(collection, [predicate])

sourcemaven central repository

Creates a slice of collection with elements taken from the end. Elements are taken until predicate returns falsey.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.takeRightWhile(null, (Integer num) -> num.compareTo(2) < 0));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.takeRightWhile(collection, (Integer num) -> num.compareTo(2) > 0);
        System.out.println(result);
        // => [3, 4, 5]
    }
}
Try in REPL

CollectionUtils.takeWhile(collection, [predicate])

sourcemaven central repository

Creates a slice of collection with elements taken from the beginning. Elements are taken until predicate returns falsey.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to query.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Collections): Returns the slice of collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.takeWhile(null, (Integer num) -> num.compareTo(2) < 0));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.takeWhile(collection, (Integer num) -> num.compareTo(4) < 0);
        System.out.println(result);
        // => [1, 2, 3]
    }
}
Try in REPL

CollectionUtils.union([collection])

sourcemaven central repository

Creates an code>collection of unique values.

Since

1.0.2

Arguments

  1. collection (...Collections): The collection to inspect.

Returns

(Collections): Returns the new collection of combined values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.union(null));
        // => []
        
        System.out.println(CollectionUtils.union(null, Arrays.asList(1, 2, 3)));
        // => [1, 2, 3]
        
        List<Integer> collection1 = Arrays.asList(1, 2, 3);
        List<Integer> collection2 = Arrays.asList(3, 4, 5);
        List<Integer> result = CollectionUtils.union(collection1, collection2);
        System.out.println(result);
        // => [1, 2, 3, 4, 5]
    }
}
Try in REPL

CollectionUtils.unionBy([iteratee], [collection])

sourcemaven central repository

Combines multiple collections into a single collection, applying a function to each element to determine its uniqueness and removing duplicates.

Since

1.0.2

Arguments

  1. [iteratee] (Function): The iteratee invoked per element.
  2. collection (...Collections): The collection to inspect.

Returns

(Collections): Returns the new collection of combined values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.unionBy(s -> ((String) s).length(), null));
        // => []
        
        System.out.println(CollectionUtils.unionBy(s -> ((String) s).length(), null, Arrays.asList("apple", "banana")));
        // => [1, 2, 3]
        
        List<String> list1 = Arrays.asList("apple", "banana");
         List<String> list2 = Arrays.asList("cherry", "date");
         List<String> result = CollectionUtils.unionBy(String::length, list1, list2);
        System.out.println(result);
        // => ["apple", "banana", "date"]
    }
}
Try in REPL

CollectionUtils.unionWith([comparator], [collection])

sourcemaven central repository

Combines multiple collections into a single collection, using a comparator to determine the uniqueness of elements, removing duplicates based on the comparison.

Since

1.0.2

Arguments

  1. comparator (BiPredicate): The comparator invoked per element.
  2. collection (...Collections): The collection to inspect.

Returns

(Collections): Returns the new collection of combined values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.unionBy(s -> ((String) s).length(), null));
        // => []
        
        System.out.println(CollectionUtils.unionBy(s -> ((String) s).length(), null, Arrays.asList("apple", "banana")));
        // => [1, 2, 3]
        
        List<String> list1 = Arrays.asList("apple", "banana");
         List<String> list2 = Arrays.asList("cherry", "date");
         List<String> result = CollectionUtils.unionBy(String::length, list1, list2);
        System.out.println(result);
        // => ["apple", "banana", "date"]
    }
}
Try in REPL

CollectionUtils.uniq(collection)

sourcemaven central repository

Returns a collection with unique elements, removing duplicates.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.

Returns

(Collections): Returns the new duplicate free collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.uniq(null));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 2, 3, 3, 3);
        List<Integer> result = CollectionUtils.uniq(collection);
        System.out.println(result);
        // => [1, 2, 3]
    }
}
Try in REPL

CollectionUtils.uniqBy(collection, [iteratee])

sourcemaven central repository

Returns a collection with unique elements based on a specific criterion, removing duplicates.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [iteratee] (Function): The iteratee invoked per element.

Returns

(Collections): Returns the new duplicate free collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.uniqBy(null, value -> value));
        // => []
        
        List<String> collection = Arrays.asList(
    "apple", "banana", "apple", "cherry", "banana"
);
        List<String> result = CollectionUtils.uniqBy(collection, s -> s);
        System.out.println(result);
        // => ["apple", "banana", "cherry"]
    }
}
Try in REPL

CollectionUtils.uniqWith(collection, [comparator])

sourcemaven central repository

Returns a collection with unique elements based on a specific criterion, removing duplicates.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [comparator] (BiPredicate): The comparator invoked per element.

Returns

(Collections): Returns the new duplicate free collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.uniqWith(null, Integer::equals));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 2, 3, 3, 3);
        List<Integer> result = CollectionUtils.uniqWith(collection, (a, b) -> a.equals(b));
        System.out.println(result);
        // => [1, 2, 3]
    }
}
Try in REPL

CollectionUtils.unzip(collection)

sourcemaven central repository

Unzips a collection of collections into a list of lists by grouping elements at the same index.

Since

1.0.2

Arguments

  1. collection (Collections): The collection of grouped elements to process.

Returns

(Collection<? extends Collection<? extends T>>): Returns the new collection of regrouped elements.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.unzip(null));
        // => []
        
        List<List<Integer>> grouped = Arrays.asList(
            Arrays.asList(1, 2, 3),
            Arrays.asList(4, 5, 6),
            Arrays.asList(7, 8, 9)
        );
        List<List<Integer>> result = CollectionUtils.unzip(grouped);
        System.out.println(result);
        // => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
    }
}
Try in REPL

CollectionUtils.unzipWith(collection, [iteratee])

sourcemaven central repository

Unzips a collection of collections into a list of lists, and applies a combine function to each list of unzipped elements.

Since

1.0.2

Arguments

  1. collection (Collection<? extends Collection<? extends T>>): The collection of grouped elements to process.
  2. [iteratee] (Function<List<T>, T>): The function to combine regrouped values.

Returns

(Collections): Returns the new collection of regrouped elements.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.unzipWith(Collections.emptyList(), group -> 
            group.stream().mapToInt(num -> ((Integer) num).intValue()).sum()));
        // => []

        List<List<Integer>> grouped = Arrays.asList(
            Arrays.asList(1, 2, 3),
            Arrays.asList(4, 5, 6),
            Arrays.asList(7, 8, 9)
        );

        List<Integer> result = CollectionUtils.unzipWith(grouped, group -> 
            group.stream().mapToInt(num -> ((Integer) num).intValue()).sum());
        
        System.out.println(result);
        // => [12, 15, 18]
    }
}
Try in REPL

CollectionUtils.without(collection, [values])

sourcemaven central repository

Creates a collection excluding all given values.

Since

1.0.2

Arguments

  1. collection (Collections): The collection to inspect.
  2. [values] (...T): The values to exclude.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.without(null));
        // => []

        List<Integer> collection = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.without(collection, 2, 4);
        System.out.println(result);
        // => [1, 3, 5]
    }
}
Try in REPL

CollectionUtils.xor([collections])

sourcemaven central repository

Creates a collection of unique values that is the symmetric difference of the given collections.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.xor(null));
        // => []

        List<Integer> collection1 = Arrays.asList(1, 2, 3);
        List<Integer> collection2 = Arrays.asList(3, 4, 5);
        List<Integer> result = CollectionUtils.xor(collection1, collection2);
        System.out.println(result);
        // => [1, 2, 4, 5]
    }
}
Try in REPL

CollectionUtils.xorBy([iteratee], [collections])

sourcemaven central repository

This method is like xor except that it accepts iteratee which is invoked for each element of each collection to generate the criterion by which they're compared.

Since

1.0.2

Arguments

  1. [iteratee] (Function): The iteratee invoked per element.
  2. [collection] (...Collections): The collection to inspect.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.xorBy(Integer::doubleValue, null));
        // => []

        List<Integer> collection1 = Arrays.asList(1, 2, 3);
        List<Integer> collection2 = Arrays.asList(3, 4, 5);
        List<Integer> result = CollectionUtils.xorBy(Integer::doubleValue, collection1, collection2);
        System.out.println(result);
        // => [1, 2, 4, 5]
    }
}
Try in REPL

CollectionUtils.xorWith([comparator], [collections])

sourcemaven central repository

This method is like xor except that it accepts comparator which is invoked to compare elements of collections.

Since

1.0.2

Arguments

  1. [comparator] (BiPredicate): The comparator invoked per element.
  2. [collection] (...Collections): The collection to inspect.

Returns

(Collections): Returns the new collection of filtered values.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.xorWith(Integer::equals, null));
        // => []

        List<Integer> collection1 = Arrays.asList(1, 2, 3);
        List<Integer> collection2 = Arrays.asList(3, 4, 5);
        List<Integer> result = CollectionUtils.xorWith((a, b) -> a.equals(b), collection1, collection2);
        System.out.println(result);
        // => [1, 2, 4, 5]
    }
}
Try in REPL

CollectionUtils.zip([collections])

sourcemaven central repository

Creates a collection of grouped elements, the first of which contains the first elements of the given collections, the second of which contains the second elements of the given collections, and so on.

Since

1.0.2

Arguments

  1. [collection] (...Collections): The collection to process.

Returns

(Collections): Returns the new collection of grouped elements.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.zip(null));
        // => []

        List<Integer> collection1 = Arrays.asList(1, 2, 3);
        List<Integer> collection2 = Arrays.asList(4, 5, 6, 7);
        List<List<Integer>> result = CollectionUtils.zip(collection1, collection2);
        System.out.println(result);
        // => [[1, 4], [2, 5], [3, 6], [null, 7]]
    }
}
Try in REPL

CollectionUtils.zipWith([iteratee], [collections])

sourcemaven central repository

This method is like zip except that it accepts iteratee to specify how grouped values should be combined.

Since

1.0.2

Arguments

  1. [iteratee] (Function<List<T>, R>): The function to combine grouped values.
  2. [collection] (...Collections): The collection to process.

Returns

(Collections): Returns the new collection of grouped elements.

Example

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        Function<List<Integer>, String> iteratee = group -> group.stream().map(String::valueOf).collect(Collectors.joining("-"));
        
        System.out.println(CollectionUtils.zipWith(iteratee, null));
        // => []

        List<Integer> collection1 = Arrays.asList(1, 2, 3);
        List<Integer> collection2 = Arrays.asList(4, 5, 6);
        List<String> result = CollectionUtils.zipWith(iteratee, collection1, collection2);
        System.out.println(result);
        // => ["1-4", "2-5", "3-6"]
    }
}
Try in REPL

CollectionUtils.zipObject([props], [values])

sourcemaven central repository

Combines two collections into a map where the first collection provides the keys and the second provides the values. If the second collection has fewer elements than the first, null is used for missing values.

Since

1.0.2

Arguments

  1. [props] (Collections): The property identifiers.
  2. [values] (Collections): The property values.

Returns

(Map): Returns the new map.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {

        List<String> keys = Arrays.asList("a", "b", "c");
        List<Integer> values = Arrays.asList(1, 2);
        Map<String, Integer> result = CollectionUtils.zipObject(keys, values);
        System.out.println(result);
        // => {"a": 1, "b": 2, "c": null}
    }
}
Try in REPL

CollectionUtils.countBy(collection, [iteratee])

sourcemaven central repository

Counts the occurrences of each unique key in a collection based on the provided key mapper. Returns a map where the keys are the result of applying the keyMapper to each element of the collection, and the values are the count of occurrences of each key.

Since

1.0.2

Arguments

  1. [props] (Collections): The collection to iterate over.
  2. [iteratee] (Function<? super T, ? extends K>): The iteratee to transform keys.

Returns

(Map): Returns the new map.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<String> collection = Arrays.asList("apple", "banana", "apple", "apple", "orange");
        Map<String, Long> result = CollectionUtils.countBy(collection, String::toUpperCase);
        System.out.println(result);
        // => {"APPLE": 3, "BANANA": 1, "ORANGE": 1}
    }
}
Try in REPL

CollectionUtils.every(collection, [predicate])

sourcemaven central repository

Checks if predicate returns truthy for all elements of collection.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to iterate over.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(boolean): Returns true if all elements pass the predicate check, else false.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.every(null, (Integer num) -> num.compareTo(3) < 0));
        // => false
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
        boolean result = CollectionUtils.every(numbers, (Integer num) -> num.compareTo(0) > 0);
        System.out.println(result);
        // => true
    }
}
Try in REPL

CollectionUtils.every(map, [biPredicate])

sourcemaven central repository

Checks if predicate returns truthy for all elements of map.

Since

1.0.2

Arguments

  1. [map] (Map<K, V>): The map to iterate over.
  2. [biPredicate] (BiPredicate): The biPredicate to apply to each entry (key, value).

Returns

(boolean): Returns true if all elements pass the predicate check, else false.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.every(null, (Integer num) -> num.compareTo(3) < 0));
        // => false
        
        Map<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        boolean result = CollectionUtils.every(map.values(), value -> value > 0);
        System.out.println(result);
        // => true
    }
}
Try in REPL

CollectionUtils.filter(collection, [predicate])

sourcemaven central repository

Iterates over elements of collection, returning an array of all elements predicate returns truthy.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to iterate over.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Collections): Returns the new filtered collection.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.filter(null, (Integer x) -> x % 2 == 0));
        // => []
        
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.filter(list, (Integer x) -> x % 2 == 0);
        System.out.println(result);
        // => [2, 4]
    }
}
Try in REPL

CollectionUtils.filter(map, [predicate])

sourcemaven central repository

Filters elements of a map based on a BiPredicate applied to entries (key-value pairs).

Since

1.0.2

Arguments

  1. [map] (Map<K, V>): The map to iterate over.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Map): Returns a new Map.

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        Map<String, Integer> map = Map.of("a", 1, "b", 2, "c", 3);
        Map<String, Integer> result = CollectionUtils.filter(map, (key, value) -> value > 1);
        System.out.println(result);
        // => {b=2, c=3}
    }
}
Try in REPL

CollectionUtils.find(collection, [predicate])

sourcemaven central repository

Finds the first element in a collection that satisfies the predicate.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Optional<T>): Returns the matched element, else Optional.empty().

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.find(null, (String s) -> s.startsWith("b")));
        // => Optional.empty();
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        Optional<String> result = CollectionUtils.find(collection, (String s) -> s.startsWith("b"));
        System.out.println(result);
        // => Optional[banana]
    }
}
Try in REPL

CollectionUtils.findLast(collection, [predicate])

sourcemaven central repository

Finds the last element in a collection that satisfies the predicate.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Optional<T>): Returns the matched element, else Optional.empty().

Example

import java.util.*;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.findLast(null, (String s) -> s.startsWith("b")));
        // => Optional.empty();
        
        List<String> collection = Arrays.asList("apple", "banana", "cherry");
        Optional<String> result = CollectionUtils.findLast(collection, (String s) -> s.startsWith("b"));
        System.out.println(result);
        // => Optional[banana]
    }
}
Try in REPL

CollectionUtils.flatMap(collection, [iteratee])

sourcemaven central repository

Flattens a collection of collections by applying the given function to each element, and collecting the results into a single list. The function transforms each element into a collection, and the results of these collections are then flattened into a single list.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.
  2. [iteratee] (Function<? super T, ? extends Collection<? extends R>>): The function invoked per iteration.

Returns

(Collection): Returns the new flattened collection.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        Function<Integer, Collection<String>> iteratee = num -> Arrays.asList("Item " + num);
        
        System.out.println(CollectionUtils.flatMap(null, iteratee));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3);
        List<String> result = CollectionUtils.flatMap(collection, iteratee);
        System.out.println(result);
        // => ["Item 1", "Item 2", "Item 3"]
    }
}
Try in REPL

CollectionUtils.flatMapDeep(collection, [iteratee])

sourcemaven central repository

This method is like flatMap except that it recursively flattens the mapped results.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.
  2. [iteratee] (Function<? super T, ? extends Collection<? extends R>>): The function invoked per iteration.

Returns

(Collection): Returns the new flattened collection.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        Function<Integer, Collection<String>> iteratee = num -> Arrays.asList("Item " + num);
        
        System.out.println(CollectionUtils.flatMapDeep(null, iteratee));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3);
        List<Object> result = CollectionUtils.flatMapDeep(collection, iteratee);
        System.out.println(result);
        // => ["Item 1", "Item 2", "Item 3"]
    }
}
Try in REPL

CollectionUtils.flatMapDepth(collection, [iteratee], [depth])

sourcemaven central repository

This method is like flatMap except that it recursively flattens the mapped results.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.
  2. [iteratee] (Function<? super T, ? extends Collection<? extends R>>): The function invoked per iteration.
  3. [depth] (int): The maximum recursion depth.

Returns

(Collection): Returns the new flattened collection.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        Function<Integer, Collection<String>> iteratee = num -> Arrays.asList("Item " + num);
        
        System.out.println(CollectionUtils.flatMapDepth(null, iteratee, 2));
        // => []
        
        List<Integer> collection = Arrays.asList(1, 2, 3);
        List<Object> result = CollectionUtils.flatMapDepth(collection, iteratee, 2);
        System.out.println(result);
        // => ["Item 1", "Item 2", "Item 3"]
    }
}
Try in REPL

CollectionUtils.forEach(collection, [iteratee])

sourcemaven central repository

Iterates over elements of a collection and invokes the given iteratee for each element.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.
  2. [iteratee] (Consumer<? super T>): The function invoked per iteration.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<Integer> collection = Arrays.asList(1, 2, 3);
        CollectionUtils.forEach(collection, num -> System.out.println("Element: " + num));
        // => Element: 1
        // => Element: 2
        // => Element: 3
    }
}
Try in REPL

CollectionUtils.forEach(map, [iteratee])

sourcemaven central repository

Iterates over elements of a map (key-value pairs) and invokes the given iteratee for each entry.

Since

1.0.2

Arguments

  1. [map] (Map): The map to inspect.
  2. [iteratee] (BiConsumer<? super K, ? super V>): The function invoked per iteration.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        Map<String, Integer> map = Map.of("a", 1, "b", 2);
        CollectionUtils.forEach(map, (key, value) -> System.out.println(key + " = " + value));
        // => a = 1
        // => b = 2
    }
}
Try in REPL

CollectionUtils.forEachRight(collection, [iteratee])

sourcemaven central repository

Iterates over elements of a collection from right to left and invokes the given iteratee for each element.

Since

1.0.2

Arguments

  1. [collection] (Collections): The collection to inspect.
  2. [iteratee] (Consumer<? super T>): The function invoked per iteration.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<Integer> collection = Arrays.asList(1, 2, 3);
        CollectionUtils.forEachRight(collection, num -> System.out.println("Element: " + num));
        // => Element: 3
        // => Element: 2
        // => Element: 1
    }
}
Try in REPL

CollectionUtils.forEachRight(map, [iteratee])

sourcemaven central repository

Iterates over elements of a map (key-value pairs) from right to left and invokes the given iteratee for each entry.

Since

1.0.2

Arguments

  1. [map] (Map): The map to inspect.
  2. [iteratee] (BiConsumer<? super K, ? super V>): The function invoked per iteration.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        Map<String, Integer> map = Map.of("a", 1, "b", 2);
        CollectionUtils.forEachRight(map, (key, value) -> System.out.println(key + " = " + value));
        // => b = 2
        // => a = 1
    }
}
Try in REPL

CollectionUtils.groupBy(collection, [iteratee])

sourcemaven central repository

Groups elements of the given collection by the result of applying the given iteratee

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. [iteratee] (Function<? super T, ? extends K>): The iteratee to transform keys.

Returns

(Map): Returns the new map.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        List<String> words = Arrays.asList("apple", "banana", "apricot", "blueberry");
        Map<Character, List<String>> grouped = CollectionUtils.groupBy(words, word -> word.charAt(0));
        System.out.println(grouped);
        // => {a=[apple, apricot], b=[banana, blueberry]}
    }
}
Try in REPL

CollectionUtils.includes(collection, value)

sourcemaven central repository

Checks if the given value is in the collection.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to inspect.
  2. value (T): The value to search for.

Returns

(boolean): Returns true if value is found, else false.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        CollectionUtils.includes(null, "banana");
        // result = false
        
        List<String> words = Arrays.asList("apple", "banana", "apricot");
        boolean result = CollectionUtils.includes(words, "banana");
        System.out.println(result);
        // result = true
    }
}
Try in REPL

CollectionUtils.keyBy(collection, [iteratee])

sourcemaven central repository

Creates a Map composed of keys generated from the results of running each element of the collection through the given iteratee function. The corresponding value of each key is the last element responsible for generating that key.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. iteratee (Function<T, R>): The iteratee to transform keys.

Returns

(Map): Returns a new map.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.keyBy(null, String::length));
        // Collections.emptyMap();
        
        List<String> words = Arrays.asList("apple", "banana", "apricot");
        Map<Integer, String> result = CollectionUtils.keyBy(words, String::length);
        System.out.println(result);
        // result = {5=apple, 6=banana, 7=apricot}
    }
}
Try in REPL

CollectionUtils.map(collection, [iteratee])

sourcemaven central repository

Creates a list of values by running each element in the collection through the given iteratee.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. iteratee (Function<T, R>): The iteratee to transform keys.

Returns

(Collection): Returns the new mapped collection.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.map(null, String::length));
        // => []
        
        List<String> words = Arrays.asList("apple", "banana", "apricot");
        List<Integer> result = CollectionUtils.map(words, String::length);
        System.out.println(result);
        // result = [5, 6, 7]
    }
}
Try in REPL

CollectionUtils.orderBy(collection, [comparator])

sourcemaven central repository

Sorts the collection based on a given predicate that determines the sorting order.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. comparator (Comparator<? super T>): The comparator to sort by.

Returns

(Collection): Returns the new sorted collection.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.orderBy(null, Integer::compareTo));
        // => []
        
        List<Integer> numbers = Arrays.asList(5, 2, 8, 3);
        List<Integer> result = CollectionUtils.orderBy(numbers, Integer::compareTo);
        System.out.println(result);
        // result = [2, 3, 5, 8]
    }
}
Try in REPL

CollectionUtils.partition(collection, [predicate])

sourcemaven central repository

Partitions the given collection into two groups based on the predicate: one group for elements where the predicate returns true, and the other for false.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. predicate (Predicate): The function invoked per iteration.

Returns

(Collection): Returns the collection of grouped elements.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.partition(null, (Integer n) -> n % 2 == 0));
        // => []
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<List<Integer>> result = CollectionUtils.partition(numbers, (Integer n) -> n % 2 == 0);
        System.out.println(result);
        // result = [[2, 4], [1, 3, 5]]
    }
}
Try in REPL

CollectionUtils.reduce(collection, [identity], [accumulator])

sourcemaven central repository

Reduces the collection to a single value by applying the provided accumulator function to each element, where each successive invocation is supplied the return value of the previous.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. [identity] (T): The initial value.
  3. [accumulator] (BinaryOperator): The function invoked per iteration.

Returns

(T): Returns the accumulated value.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.reduce(null, 0, Integer::sum));
        // => 0
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int result = CollectionUtils.reduce(numbers, 0, Integer::sum);
        System.out.println(result);
        // result = 15
    }
}
Try in REPL

CollectionUtils.reduce(collection, [accumulator])

sourcemaven central repository

Reduces a collection to a single value without an identity.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. [accumulator] (BinaryOperator): The function invoked per iteration.

Returns

(Optional<T>): Returns an Optional of the accumulated value.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.reduce(null, Integer::sum));
        // => Optional.empty();
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Optional<Integer> result = CollectionUtils.reduce(numbers, Integer::sum);
        System.out.println(result);
        // result = Optional[15]
    }
}
Try in REPL

CollectionUtils.reduceRight(collection, [identity], [accumulator])

sourcemaven central repository

Reduces the collection from right to left to a single value by applying the provided accumulator function to each element, where each successive invocation is supplied the return value of the previous.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. [identity] (T): The initial value.
  3. [accumulator] (BinaryOperator): The function invoked per iteration.

Returns

(T): Returns the accumulated value.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.reduceRight(null, 0, Integer::sum));
        // => 0
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        int result = CollectionUtils.reduceRight(numbers, 0, Integer::sum);
        System.out.println(result);
        // result = 15
    }
}
Try in REPL

CollectionUtils.reduceRight(collection, [accumulator])

sourcemaven central repository

Reduces a collection to a single value without an identity.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. [accumulator] (BinaryOperator): The function invoked per iteration.

Returns

(Optional<T>): Returns an Optional of the accumulated value.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.reduceRight(null, Integer::sum));
        // => Optional.empty();
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Optional<Integer> result = CollectionUtils.reduceRight(numbers, Integer::sum);
        System.out.println(result);
        // result = Optional[15]
    }
}
Try in REPL

CollectionUtils.reject(collection, [predicate])

sourcemaven central repository

Filters out elements from the collection that do not satisfy the given predicate.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(Collection): Returns the new filtered collection.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.reject(null, (Integer n) -> n % 2 == 0));
        // => [];
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.reject(numbers, (Integer n) -> n % 2 == 0);
        System.out.println(result);
        // result = [1, 3, 5]
    }
}
Try in REPL

CollectionUtils.sample(collection)

sourcemaven central repository

Gets a random element from the collection.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to sample.

Returns

(Optional<T>): Returns an Optional of the random element.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.sample(null));
        // => Optional.empty();
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Optional<Integer> result = CollectionUtils.sample(numbers);
        System.out.println(result);
        // result = A random element from the list (e.g., 3)
    }
}
Try in REPL

CollectionUtils.sampleSize(collection, [n])

sourcemaven central repository

Randomly selects a specified number of elements from the given collection.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to sample.
  2. [n] (int): The number of elements to sample.

Returns

(Collection): Returns a collection the random elements.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.sampleSize(null, 3));
        // => [];
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.sampleSize(numbers, 3);
        System.out.println(result);
        // result = A random list of 3 elements, e.g., [2, 5, 1]
    }
}
Try in REPL

CollectionUtils.shuffle(collection)

sourcemaven central repository

Creates a shuffled version of the given collection using the Fisher-Yates shuffle algorithm.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to shuffle.

Returns

(Collection): Returns the new shuffled collection.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.sampleSize(null, 3));
        // => [];
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = CollectionUtils.shuffle(numbers);
        System.out.println(result);
        // result = A randomly shuffled list, e.g., [3, 1, 4, 2, 5]
    }
}
Try in REPL

CollectionUtils.size(collection)

sourcemaven central repository

Gets the size of the given collection. For arrays and strings, it returns the length. For objects, it returns the number of own properties. For Maps and Sets, it returns their size.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to inspect.

Returns

(int): Returns the collection size.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.size(null));
        // => 0;
        
        System.out.println(CollectionUtils.size(Arrays.asList(1, 2, 3, 4)));
        // => 4;
        
        System.out.println(CollectionUtils.size("Hello"));
        // result = 5
    }
}
Try in REPL

CollectionUtils.some(collection, [predicate])

sourcemaven central repository

Checks if the predicate returns true for **any** element of the collection. Iteration is stopped once the predicate returns true.

Since

1.0.2

Arguments

  1. collection (Collection): The collection to iterate over.
  2. [predicate] (Predicate): The function invoked per iteration.

Returns

(boolean): Returns true if any element passes the predicate check, else false.

Example

import java.util.*;
import java.util.function.Function;
import io.javadash.CollectionUtils;

public class Main {
    public static void main(String[] args) {
        
        System.out.println(CollectionUtils.some(null, (Integer num) -> num % 2 == 0));
        // => false
        
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
        boolean result = CollectionUtils.some(numbers, num -> num % 2 == 0);
        System.out.println(result);
        // result = true
    }
}
Try in REPL

DateUtils” Methods

DateUtils.now()

sourcemaven central repository

Returns the current time in milliseconds since the Unix epoch (January 1, 1970). This is equivalent to System.currentTimeMillis().

Since

1.0.2

Arguments

    Returns

    (int): The current time in milliseconds.

    Example

    import java.util.*;
    import io.javadash.DateUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            long currentTime = DateUtils.now();
            System.out.println("Current time in milliseconds: " + currentTime);
        }
    }
    
    Try in REPL

    FunctionUtils” Methods

    FunctionUtils.after([n], [func])

    sourcemaven central repository

    Returns a Consumer that runs the provided Runnable after being called a specified number of times. The Runnable will be executed when the accept method is invoked n times.

    Since

    1.0.2

    Arguments

    1. [n] (int): The number of times the <code>accept</code> method needs to be called before the <code>Runnable</code> is executed.
    2. [func] (Runnable): The <code>Runnable</code> to execute after the specified number of calls.

    Returns

    (Consumer): A Consumer that tracks the number of calls and executes the Runnable when the threshold is reached.

    Example

    import java.util.*;
    import io.javadash.FunctionUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            Consumer<Void> consumer = FunctionUtils.after(3, () -> System.out.println("Executed"));
            consumer.accept(null); // Nothing happens
        }
    }
    
    Try in REPL

    LangUtils” Methods

    LangUtils.isEmpty([value])

    sourcemaven central repository

    Checks if the given value is considered empty. The method handles various types of objects, including Strings, Collections, Maps, and Arrays, and returns true if the value is empty, null, or has no elements.

    Since

    1.0.2

    Arguments

    1. [value] (Object): The value to check for emptiness.

    Returns

    (boolean): Returns true if value is empty, else false.

    Example

    import java.util.*;
    import io.javadash.LangUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            List<Integer> numbers = Arrays.asList();
            boolean result = LangUtils.isEmpty(numbers);
            System.out.println(result);
            // Output: true
        }
    }
    
    Try in REPL

    StringUtils” Methods

    StringUtils.includes(str, value)

    sourcemaven central repository

    Checks if the given value is in the string (substring match).

    Since

    1.0.2

    Arguments

    1. str (String): The string to search within.
    2. value (String): The value to search for in the string.

    Returns

    (boolean): Return true if the string contains the specified value; otherwise false.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String sentence = "Hello, world!";
            boolean result = StringUtils.includes(sentence, "world");
            System.out.println(result);
            // Output: true
        }
    }
    
    Try in REPL

    StringUtils.camelCase(str)

    sourcemaven central repository

    Converts a given string to camel case.

    Since

    1.0.2

    Arguments

    1. str (String): The string to convert to camelCase.

    Returns

    (String): Returns the camel cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String sentence = "hello world example";
            String result = StringUtils.camelCase(sentence);
            System.out.println(result);
            // Output: helloWorldExample
        }
    }
    
    Try in REPL

    StringUtils.capitalize(str)

    sourcemaven central repository

    Capitalizes the first letter of a given string and makes the rest lowercase.

    Since

    1.0.2

    Arguments

    1. str (String): The string to capitalize.

    Returns

    (String): Returns the capitalized string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String word = "hello";
            String result = StringUtils.capitalize(word);
            System.out.println(result);
            // Output: Hello
        }
    }
    
    Try in REPL

    StringUtils.deburr(str)

    sourcemaven central repository

    Deburrs a string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.

    Since

    1.0.2

    Arguments

    1. str (String): The string to deburr.

    Returns

    (String): Returns the deburred string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String word = "résumé";
            String result = StringUtils.deburr(word);
            System.out.println(result);
            // Output: resume
        }
    }
    
    Try in REPL

    StringUtils.endsWith(str, target)

    sourcemaven central repository

    Checks if string ends with the given target string.

    Since

    1.0.2

    Arguments

    1. str (String): The string to inspect.
    2. target (String): The string to search for.

    Returns

    (boolean): Returns true if string ends with target, else false.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String sentence = "Hello, world!";
            boolean result = StringUtils.endsWith(sentence, "world!");
            System.out.println(result);
            // Output: true
        }
    }
    
    Try in REPL

    StringUtils.escape(str)

    sourcemaven central repository

    Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.

    Since

    1.0.2

    Arguments

    1. str (String): The string to escape.

    Returns

    (String): Returns the escaped string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String rawString = "Hello & welcome!";
            String result = StringUtils.escape(rawString);
            System.out.println(result);
            // Output: "Hello &amp; welcome!"
        }
    }
    
    Try in REPL

    StringUtils.escapeRegExp(str)

    sourcemaven central repository

    Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.

    Since

    1.0.2

    Arguments

    1. str (String): The string to escape.

    Returns

    (String): Returns the escaped string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String rawString = "Hello (world)!";
            String result = StringUtils.escapeRegExp(rawString);
            System.out.println(result);
            // Output: "Hello \\(world\\)!"
        }
    }
    
    Try in REPL

    StringUtils.kebabCase(str)

    sourcemaven central repository

    Converts string to kebab case.

    Since

    1.0.2

    Arguments

    1. str (String): The string to convert.

    Returns

    (String): Returns the kebab cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String input = "HelloWorld Example";
            String result = StringUtils.kebabCase(input);
            System.out.println(result);
            // Output: "hello-world-example"
        }
    }
    
    Try in REPL

    StringUtils.lowerCase(str)

    sourcemaven central repository

    Converts string to lowercase with words separated by spaces.

    Since

    1.0.2

    Arguments

    1. str (String): The string to convert.

    Returns

    (String): Returns the lower cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String input = "Hello World Example";
            String result = StringUtils.lowerCase(input);
            System.out.println(result);
            // Output: "hello world example"
        }
    }
    
    Try in REPL

    StringUtils.lowerFirst(str)

    sourcemaven central repository

    Converts the first character of string to lower case.

    Since

    1.0.2

    Arguments

    1. str (String): The string to convert.

    Returns

    (String): Returns the converted string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String input = "Hello";
            String result = StringUtils.lowerFirst(input);
            System.out.println(result);
            // Output: "hello"
        }
    }
    
    Try in REPL

    StringUtils.pad([str=''], [length=0], [chars=' '])

    sourcemaven central repository

    Pads string on the left and right sides if it's shorter than length. Padding characters are truncated if they can't be evenly divided by length.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to pad.
    2. [length=0] (int): The padding length.
    3. [chars=' '] (String): The string used as padding.

    Returns

    (String): Returns the padded string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.pad("hello", 10, "*");
            System.out.println(result);
            // Output: "**hello***"
        }
    }
    
    Try in REPL

    StringUtils.padStart([str=''], [length=0], [chars=' '])

    sourcemaven central repository

    Pads string on the left side if it's shorter than length. Padding characters are truncated if they exceed length.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to pad.
    2. [length=0] (int): The padding length.
    3. [chars=' '] (String): The string used as padding.

    Returns

    (String): Returns the padded string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.padStart("hello", 10, "*");
            System.out.println(result);
            // Output: "*****hello"
        }
    }
    
    Try in REPL

    StringUtils.padEnd([str=''], [length=0], [chars=' '])

    sourcemaven central repository

    Pads string on the right side if it's shorter than length. Padding characters are truncated if they exceed length.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to pad.
    2. [length=0] (int): The padding length.
    3. [chars=' '] (String): The string used as padding.

    Returns

    (String): Returns the padded string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.padEnd("hello", 10, "*");
            System.out.println(result);
            // Output: "hello*****"
        }
    }
    
    Try in REPL

    StringUtils.repeat([string=''], [n])

    sourcemaven central repository

    Repeats the given string n times.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to repeat.
    2. [n] (int): The number of times to repeat the string.

    Returns

    (String): Returns the repeated string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.repeat("abc", 3);
            System.out.println(result);
            // Output: "abcabcabc"
        }
    }
    
    Try in REPL

    StringUtils.replace([string=''], pattern, replacement)

    sourcemaven central repository

    Replaces matches for pattern in string with replacement.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to modify.
    2. [pattern] (String): The pattern to replace.
    3. [replacement] (String): The match replacement.

    Returns

    (String): Returns the modified string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.replace("Hello World", "World", "Java");
            System.out.println(result);
            // Output: "Hello Java"
        }
    }
    
    Try in REPL

    StringUtils.snakeCase([string=''])

    sourcemaven central repository

    Converts string to snake case.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to convert.

    Returns

    (String): Returns the snake cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
             String result = StringUtils.snakeCase("HelloWorld Example");
            System.out.println(result);
            // Output: "hello_world_example"
        }
    }
    
    Try in REPL

    StringUtils.split([string=''], separator)

    sourcemaven central repository

    Splits string by separator.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to split.
    2. separator (String): The separator pattern to split by.

    Returns

    (List<String>): Returns the string segments.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            List<String> result = StringUtils.split("apple,banana,orange", ",");
            System.out.println(result);
            // Output: ["apple", "banana", "orange"]
        }
    }
    
    Try in REPL

    StringUtils.startCase([string=''])

    sourcemaven central repository

    Converts string to start case.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to convert.

    Returns

    (String: Returns the start cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.startCase("hello world!");
            System.out.println(result);
            // Output: "Hello World"
        }
    }
    
    Try in REPL

    StringUtils.startsWith([string=''], [target])

    sourcemaven central repository

    Checks if string starts with the given target string.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to inspect.
    2. [target] (String): The string to search for.

    Returns

    (boolean: Returns true if string starts with target, else false.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            boolean result = StringUtils.startsWith("hello world", "hello");
            System.out.println(result);  
            // Output: true
        }
    }
    
    Try in REPL

    StringUtils.toLower([string=''])

    sourcemaven central repository

    Converts string, as a whole, to lower case.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to convert.

    Returns

    (String: Returns the lower cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String lowerValue = StringUtils.toLower("HELLO");
            System.out.println(result);  
            // Output: hello
        }
    }
    
    Try in REPL

    StringUtils.toUpper([string=''])

    sourcemaven central repository

    Converts string, as a whole, to upper case.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to convert.

    Returns

    (String: Returns the upper cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String upperValue = StringUtils.toUpper("hello");
            System.out.println(result);  
            // Output: HELLO
        }
    }
    
    Try in REPL

    StringUtils.trim([string=''], [chars=whitespace])

    sourcemaven central repository

    Removes leading and trailing whitespace or specified characters from string.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to trim.
    2. [chars=whitespace] (String): The characters to trim.

    Returns

    (String: Returns the trimmed string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
             String result = StringUtils.trim("  hello world  ", " ");
            System.out.println(result);  
            // Output: "hello world"
        }
    }
    
    Try in REPL

    StringUtils.trimStart([string=''], [chars=whitespace])

    sourcemaven central repository

    Removes leading whitespace or specified characters from string.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to trim.
    2. [chars=whitespace] (String): The characters to trim.

    Returns

    (String: Returns the trimmed string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
             String result = StringUtils.trimStart("  hello world  ", " ");
            System.out.println(result);  
            // Output: "hello world  "
        }
    }
    
    Try in REPL

    StringUtils.trimEnd([string=''], [chars=whitespace])

    sourcemaven central repository

    Removes leading whitespace or specified characters from string.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to trim.
    2. [chars=whitespace] (String): The characters to trim.

    Returns

    (String: Returns the trimmed string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
             String result = StringUtils.trimEnd("  hello world  ", " ");
            System.out.println(result);  
            // Output: "  hello world"
        }
    }
    
    Try in REPL

    StringUtils.upperCase([string=''])

    sourcemaven central repository

    Converts string, as space separated words, to upper case.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to convert.

    Returns

    (String: Returns the upper cased string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.upperCase("hello world 123");
            System.out.println(result);  
            // Output: "HELLO WORLD 123"
        }
    }
    
    Try in REPL

    StringUtils.upperFirst([string=''])

    sourcemaven central repository

    Converts the first character of string to upper case.

    Since

    1.0.2

    Arguments

    1. [str=''] (String): The string to convert.

    Returns

    (String: Returns the converted string.

    Example

    import java.util.*;
    import io.javadash.StringUtils;
    
    public class Main {
        public static void main(String[] args) {
            
            String result = StringUtils.upperFirst("hello");
            System.out.println(result);  
            // Output: "Hello"
        }
    }
    
    Try in REPL