Class CollectionUtils

java.lang.Object
org.apache.juneau.internal.CollectionUtils

public class CollectionUtils extends Object
Utility methods for collections.
See Also:
  • Method Details

    • setFrom

      public static <E> Set<E> setFrom(Collection<E> val)
      Creates a new set from the specified collection.
      Type Parameters:
      E - The element type.
      Parameters:
      val - The value to copy from.
      Returns:
      A new LinkedHashSet, or null if the input was null.
    • copyOf

      public static <E> Set<E> copyOf(Set<E> val)
      Creates a new set from the specified collection.
      Type Parameters:
      E - The element type.
      Parameters:
      val - The value to copy from.
      Returns:
      A new LinkedHashSet, or null if the input was null.
    • copyOf

      public static <E> Collection<E> copyOf(Collection<E> val)
      Creates a new collection from the specified collection.
      Type Parameters:
      E - The element type.
      Parameters:
      val - The value to copy from.
      Returns:
      A new LinkedHashSet, or null if the input was null.
    • copyOf

      public static <K, V> Map<K,V> copyOf(Map<K,V> val)
      Creates a new map from the specified map.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      val - The value to copy from.
      Returns:
      A new LinkedHashMap, or null if the input was null.
    • mapBuilder

      public static <K, V> MapBuilder<K,V> mapBuilder(Map<K,V> addTo)
      Instantiates a new builder on top of the specified map.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      addTo - The map to add to.
      Returns:
      A new builder on top of the specified map.
    • mapBuilder

      public static <K, V> MapBuilder<K,V> mapBuilder(Class<K> keyType, Class<V> valueType, Type... valueTypeArgs)
      Instantiates a new builder of the specified map type.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      keyType - The key type.
      valueType - The value type.
      valueTypeArgs - The value type args.
      Returns:
      A new builder on top of the specified map.
    • listBuilder

      public static <E> ListBuilder<E> listBuilder(List<E> addTo)
      Instantiates a new builder on top of the specified list.
      Type Parameters:
      E - The element type.
      Parameters:
      addTo - The list to add to.
      Returns:
      A new builder on top of the specified list.
    • listBuilder

      public static <E> ListBuilder<E> listBuilder(Class<E> elementType, Type... elementTypeArgs)
      Instantiates a new builder of the specified list type.
      Type Parameters:
      E - The element type.
      Parameters:
      elementType - The element type.
      elementTypeArgs - The element type args.
      Returns:
      A new builder on top of the specified list.
    • setBuilder

      public static <E> SetBuilder<E> setBuilder(Set<E> addTo)
      Instantiates a new builder on top of the specified set.
      Type Parameters:
      E - The element type.
      Parameters:
      addTo - The set to add to.
      Returns:
      A new builder on top of the specified set.
    • setBuilder

      public static <E> SetBuilder<E> setBuilder(Class<E> elementType, Type... elementTypeArgs)
      Instantiates a new builder of the specified set.
      Type Parameters:
      E - The element type.
      Parameters:
      elementType - The element type.
      elementTypeArgs - The element type args.
      Returns:
      A new builder on top of the specified set.
    • listOf

      @SafeVarargs public static <E> List<E> listOf(Class<E> elementType, E... values)
      Convenience method for creating an ArrayList.
      Type Parameters:
      E - The element type.
      Parameters:
      elementType - The element type.
      values - The values to initialize the list with.
      Returns:
      A new modifiable list.
    • listFrom

      public static <E> ArrayList<E> listFrom(Collection<E> value)
      Creates an ArrayList copy from a collection.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to copy from.
      Returns:
      A new modifiable list.
    • mapFrom

      public static <K, V> LinkedHashMap<K,V> mapFrom(Map<K,V> value)
      Creates an ArrayList copy from a collection.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      value - The collection to copy from.
      Returns:
      A new modifiable list.
    • listFrom

      public static <E> ArrayList<E> listFrom(Collection<E> value, boolean nullIfEmpty)
      Creates an ArrayList copy from a collection.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to copy from.
      nullIfEmpty - If true will return null if the collection is empty.
      Returns:
      A new modifiable list.
    • setOf

      @SafeVarargs public static <E> LinkedHashSet<E> setOf(Class<E> elementType, E... values)
      Convenience method for creating a LinkedHashSet.
      Type Parameters:
      E - The element type.
      Parameters:
      elementType - The element type.
      values - The values to initialize the set with.
      Returns:
      A new modifiable set.
    • sortedSet

      @SafeVarargs public static <E> TreeSet<E> sortedSet(E... values)
      Convenience method for creating a TreeSet.
      Type Parameters:
      E - The element type.
      Parameters:
      values - The values to initialize the set with.
      Returns:
      A new modifiable set.
    • sortedSetFrom

      public static <E> TreeSet<E> sortedSetFrom(Collection<E> value)
      Creates a new TreeSet from the specified collection.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The value to copy from.
      Returns:
      A new TreeSet, or null if the input was null.
    • sortedSetFrom

      public static <E> TreeSet<E> sortedSetFrom(Collection<E> value, boolean nullIfEmpty)
      Creates a new TreeSet from the specified collection.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The value to copy from.
      nullIfEmpty - If true returns null if the collection is empty.
      Returns:
      A new TreeSet, or null if the input was null.
    • map

      public static <K, V> LinkedHashMap<K,V> map()
      Convenience method for creating a LinkedHashMap.
      Type Parameters:
      K - The key type.
      V - The value type.
      Returns:
      A new modifiable map.
    • mapOf

      public static <K, V> LinkedHashMap<K,V> mapOf(Class<K> keyType, Class<V> valueType)
      Convenience method for creating a LinkedHashMap.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      keyType - The key type.
      valueType - The value type.
      Returns:
      A new modifiable map.
    • map

      public static <K, V> LinkedHashMap<K,V> map(K k1, V v1)
      Convenience method for creating a LinkedHashMap.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      k1 - Key 1.
      v1 - Value 1.
      Returns:
      A new modifiable map.
    • map

      public static <K, V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2)
      Convenience method for creating a LinkedHashMap.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      k1 - Key 1.
      v1 - Value 1.
      k2 - Key 2.
      v2 - Value 2.
      Returns:
      A new modifiable map.
    • map

      public static <K, V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3)
      Convenience method for creating a LinkedHashMap.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      k1 - Key 1.
      v1 - Value 1.
      k2 - Key 2.
      v2 - Value 2.
      k3 - Key 3.
      v3 - Value 3.
      Returns:
      A new modifiable map.
    • sortedMap

      public static <K, V> TreeMap<K,V> sortedMap()
      Convenience method for creating a TreeMap.
      Type Parameters:
      K - The key type.
      V - The value type.
      Returns:
      A new modifiable set.
    • copyOf

      public static <E> ArrayList<E> copyOf(List<E> value)
      Convenience method for copying a list.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The list to copy.
      Returns:
      A new modifiable list.
    • sortedList

      @SafeVarargs public static <E> List<E> sortedList(E... values)
      Convenience method for creating an ArrayList and sorting it.
      Type Parameters:
      E - The element type.
      Parameters:
      values - The values to initialize the list with.
      Returns:
      A new modifiable list.
    • sortedList

      public static <E> List<E> sortedList(Comparator<E> comparator, E[] values)
      Convenience method for creating an ArrayList and sorting it.
      Type Parameters:
      E - The element type.
      Parameters:
      comparator - The comparator to use to sort the list.
      values - The values to initialize the list with.
      Returns:
      A new modifiable list.
    • sortedList

      public static <E> ArrayList<E> sortedList(Comparator<E> comparator, Collection<E> value)
      Convenience method for creating an ArrayList and sorting it.
      Type Parameters:
      E - The element type.
      Parameters:
      comparator - The comparator to use to sort the list.
      value - The values to initialize the list with.
      Returns:
      A new modifiable list.
    • synced

      public static <E> List<E> synced(List<E> value)
      Wraps the specified list in Collections.unmodifiableList(List).
      Type Parameters:
      E - The element type.
      Parameters:
      value - The list to wrap.
      Returns:
      The wrapped list.
    • synced

      public static <E> Set<E> synced(Set<E> value)
      Wraps the specified set in Collections.unmodifiableSet(Set).
      Type Parameters:
      E - The element type.
      Parameters:
      value - The set to wrap.
      Returns:
      The wrapped set.
    • synced

      public static <K, V> Map<K,V> synced(Map<K,V> value)
      Wraps the specified map in Collections.unmodifiableMap(Map).
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      value - The map to wrap.
      Returns:
      The wrapped map.
    • forEachReverse

      public static <E> void forEachReverse(List<E> value, Consumer<E> action)
      Iterates the specified list in reverse order.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The list to iterate.
      action - The action to perform.
    • forEachReverse

      public static <E> void forEachReverse(E[] value, Consumer<E> action)
      Iterates the specified array in reverse order.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The array to iterate.
      action - The action to perform.
    • addAll

      @SafeVarargs public static <E> Set<E> addAll(Set<E> value, E... entries)
      Adds all the specified values to the specified collection. Creates a new set if the value is null.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to add to.
      entries - The entries to add.
      Returns:
      The set.
    • addAll

      @SafeVarargs public static <E> SortedSet<E> addAll(SortedSet<E> value, E... entries)
      Adds all the specified values to the specified collection. Creates a new set if the value is null.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to add to.
      entries - The entries to add.
      Returns:
      The set.
    • addAll

      @SafeVarargs public static <E> List<E> addAll(List<E> value, E... entries)
      Adds all the specified values to the specified collection. Creates a new set if the value is null.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to add to.
      entries - The entries to add.
      Returns:
      The set.
    • addAll

      public static <E> List<E> addAll(List<E> value, List<E> entries)
      Adds all the specified values to the specified collection. Creates a new set if the value is null.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to add to.
      entries - The entries to add.
      Returns:
      The set.
    • prependAll

      @SafeVarargs public static <E> List<E> prependAll(List<E> value, E... entries)
      Adds all the specified values to the specified collection. Creates a new set if the value is null.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to add to.
      entries - The entries to add.
      Returns:
      The set.
    • last

      public static <E> E last(List<E> l)
      Returns the last entry in a list.
      Type Parameters:
      E - The element type.
      Parameters:
      l - The list.
      Returns:
      The last element, or null if the list is null or empty.
    • last

      public static <E> E last(E[] l)
      Returns the last entry in an array.
      Type Parameters:
      E - The element type.
      Parameters:
      l - The array.
      Returns:
      The last element, or null if the array is null or empty.
    • copyOf

      public static <K, V> Map<K,V> copyOf(Map<K,V> m, Function<? super V,? extends V> valueMapper)
      Makes a deep copy of the specified map.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      m - The map to copy.
      valueMapper - The function to apply to each value in the map.
      Returns:
      A new map with the same keys as the specified map, but with values transformed by the specified function. Null if the map being copied was null.
    • copyOf

      public static <K, V> Map<K,V> copyOf(Map<K,V> m, Function<? super V,? extends V> valueMapper, Supplier<Map<K,V>> mapFactory)
      Makes a deep copy of the specified map.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      m - The map to copy.
      valueMapper - The function to apply to each value in the map.
      mapFactory - The factory for creating the map.
      Returns:
      A new map with the same keys as the specified map, but with values transformed by the specified function. Null if the map being copied was null.
    • copyOf

      public static <E> List<E> copyOf(List<E> l, Function<? super E,? extends E> valueMapper)
      Makes a deep copy of the specified list.
      Type Parameters:
      E - The entry type.
      Parameters:
      l - The list to copy.
      valueMapper - The function to apply to each value in the list.
      Returns:
      A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
    • copyOf

      public static <E> List<E> copyOf(List<E> l, Function<? super E,? extends E> valueMapper, Supplier<List<E>> listFactory)
      Makes a deep copy of the specified list.
      Type Parameters:
      E - The entry type.
      Parameters:
      l - The list to copy.
      valueMapper - The function to apply to each value in the list.
      listFactory - The factory for creating the list.
      Returns:
      A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
    • copyOf

      public static <E> Set<E> copyOf(Set<E> l, Function<? super E,? extends E> valueMapper)
      Makes a deep copy of the specified list.
      Type Parameters:
      E - The entry type.
      Parameters:
      l - The list to copy.
      valueMapper - The function to apply to each value in the list.
      Returns:
      A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
    • copyOf

      public static <E> Set<E> copyOf(Set<E> l, Function<? super E,? extends E> valueMapper, Supplier<Set<E>> setFactory)
      Makes a deep copy of the specified list.
      Type Parameters:
      E - The entry type.
      Parameters:
      l - The list to copy.
      valueMapper - The function to apply to each value in the list.
      setFactory - The factory for creating sets.
      Returns:
      A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.