Class CollectionUtils

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

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

  • 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.
    • emptyList

      public static <E> List<E> emptyList()
      Simple passthrough to Collections.emptyList()
      Type Parameters:
      E - The element type.
      Returns:
      A new unmodifiable empty list.
    • list

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

      public static <E> ArrayList<E> list(int size)
      Convenience method for creating an ArrayList of the specified size.
      Type Parameters:
      E - The element type.
      Parameters:
      size - The initial size of the list.
      Returns:
      A new modifiable list.
    • linkedList

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

      @SafeVarargs public static <E> List<E> alist(E... values)
      Convenience method for creating an array-backed list by calling Arrays.asList(Object...).
      Type Parameters:
      E - The element type.
      Parameters:
      values - The values to initialize the list with.
      Returns:
      A new modifiable list, or null if the array was null.
    • 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.
    • set

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

      @SafeVarargs public static <E> Set<E> uset(E... values)
      Convenience method for creating an unmodifiable LinkedHashSet.
      Type Parameters:
      E - The element type.
      Parameters:
      values - The values to initialize the set with.
      Returns:
      A new unmodifiable set.
    • ulist

      @SafeVarargs public static <E> List<E> ulist(E... values)
      Convenience method for creating an unmodifiable list.
      Type Parameters:
      E - The element type.
      Parameters:
      values - The values to initialize the list with.
      Returns:
      A new unmodifiable list, or null if the array was null.
    • 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.
    • 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> ArrayList<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> ArrayList<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.
    • unmodifiable

      public static <E> List<E> unmodifiable(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.
    • unmodifiable

      public static <E> Set<E> unmodifiable(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.
    • unmodifiable

      public static <K, V> Map<K,V> unmodifiable(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.
    • 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.
    • array

      public static <E> E[] array(Collection<E> value, Class<E> componentType)
      Converts the specified collection to an array.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The collection to convert.
      componentType - The component type of the array.
      Returns:
      A new array.
    • 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.
    • 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.
    • optional

      public static <T> Optional<T> optional(T value)
      Returns an optional of the specified value.
      Type Parameters:
      T - The component type.
      Parameters:
      value - The value.
      Returns:
      A new Optional.
    • empty

      public static <T> Optional<T> empty()
      Returns an empty Optional.
      Type Parameters:
      T - The component type.
      Returns:
      An empty Optional.
    • isNotEmpty

      public static <E> boolean isNotEmpty(Collection<E> value)
      Returns true if the specified collection is not null and not empty.
      Type Parameters:
      E - The element type.
      Parameters:
      value - The value being checked.
      Returns:
      true if the specified collection is not null and not empty.
    • isNotEmpty

      public static <K, V> boolean isNotEmpty(Map<K,V> value)
      Returns true if the specified map is not null and not empty.
      Type Parameters:
      K - The key type.
      V - The value type.
      Parameters:
      value - The value being checked.
      Returns:
      true if the specified map is not null and not empty.