Class CollectionLiterals


  • public final class CollectionLiterals
    extends Object

    Overview

    Provides a number of methods simplifying the task of creating populated Collections. In essence its doing the same compared to the corresponding com.google.common.collect types but with different semantics (like naming, types) and is designed as a one-stop utility class. It differentiates between the subtypes and mutability / immutability. This class is complementary to the corresponding guava types.

    Lists

      assertMutable(CollectionLiterals#mutableList("1"));
      assertMutable(CollectionLiterals#mutableList("1", "2"));
      assertMutable(CollectionLiterals#mutableList(Arrays.asList("1", "2").stream()));
     
      assertImmutable(CollectionLiterals#immutableList("1"));
      assertImmutable(CollectionLiterals#immutableList("1", "2"));
      assertImmutable(CollectionLiterals#immutableList(Arrays.asList("1", "2").stream()));
     

    Sets

      assertMutable(CollectionLiterals#mutableSet("1"));
      assertMutable(CollectionLiterals#mutableSet("1", "2"));
      assertMutable(CollectionLiterals#mutableSet(Arrays.asList("1", "2").stream()));
     
      assertImmutable(CollectionLiterals#immutableSet("1"));
      assertImmutable(CollectionLiterals#immutableSet("1", "2"));
      assertImmutable(CollectionLiterals#immutableSet(Arrays.asList("1", "2").stream()));
     

    Maps

     assertMutable(CollectionLiterals#mutableMap());
     assertMutable(CollectionLiterals#mutableMap("1", "1-1"));
     assertMutable(CollectionLiterals#mutableMap("1", "1-1", "2", "2-2", "3", "3-3", "4", "4-4"));
     
     assertImmutable(CollectionLiterals#immutableMap());
     assertImmutable(CollectionLiterals#immutableMap("1", "1-1"));
     assertImmutable(CollectionLiterals#immutableMap("1", "1-1", "2", "2-2", "3", "3-3", "4", "4-4"));
     
    Author:
    Oliver Wolff
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> List<E> immutableList()
      Creates an immutable List instance.
      static <E> List<E> immutableList​(E element)
      Creates an immutable List instance containing the given element.
      static <E> List<E> immutableList​(E... elements)
      Creates an immutable List instance containing the given elements.
      static <E> List<E> immutableList​(Iterable<? extends E> elements)
      Creates an immutable List instance containing the given elements.
      static <E> List<E> immutableList​(Collection<? extends E> elements)
      Creates an immutable List instance containing the given elements.
      static <E> List<E> immutableList​(Iterator<? extends E> elements)
      Creates an immutable List instance containing the given elements.
      static <E> List<E> immutableList​(Stream<? extends E> elements)
      Creates an immutable List instance containing the given elements.
      static <K,​V>
      Map<K,​V>
      immutableMap()
      immutableMap.
      static <K,​V>
      Map<K,​V>
      immutableMap​(Map<K,​V> source)
      static <K,​V>
      Map<K,​V>
      immutableMap​(Stream<Map.Entry<K,​V>> elements)
      Copies the entries of the given map stream into a new map which is then returned as an unmodifiable map.
      static <K,​V>
      Map<K,​V>
      immutableMap​(K k, V v)
      Convenience method for the inline creation of a map with values
      static <K,​V>
      Map<K,​V>
      immutableMap​(K k1, V v1, K k2, V v2)
      Convenience method for the inline creation of a map with values
      static <K,​V>
      Map<K,​V>
      immutableMap​(K k1, V v1, K k2, V v2, K k3, V v3)
      Convenience method for the inline creation of a map with values
      static <K,​V>
      Map<K,​V>
      immutableMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Convenience method for the inline creation of a map with values
      static <E> Set<E> immutableSet()
      immutableSet.
      static <E> Set<E> immutableSet​(E element)
      Creates an immutable Set instance containing the given elements.
      static <E> Set<E> immutableSet​(E... elements)
      Creates an immutable Set instance containing the given elements.
      static <E> Set<E> immutableSet​(Iterable<? extends E> elements)
      Creates an immutable Set instance containing the given elements.
      static <E> Set<E> immutableSet​(Iterator<? extends E> elements)
      Creates an immutable Set instance containing the given elements.
      static <E> Set<E> immutableSet​(Stream<? extends E> elements)
      Creates an immutable Set instance containing the given elements.
      static <E> SortedSet<E> immutableSortedSet()
      immutableSortedSet.
      static <E> SortedSet<E> immutableSortedSet​(E element)
      Creates an immutable SortedSet instance containing the given elements.
      static <E> SortedSet<E> immutableSortedSet​(E... elements)
      Creates an immutable SortedSet instance containing the given elements.
      static <E> SortedSet<E> immutableSortedSet​(Iterable<? extends E> elements)
      Creates an immutable SortedSet instance containing the given elements.
      static <E> SortedSet<E> immutableSortedSet​(Iterator<? extends E> elements)
      Creates an immutable SortedSet instance containing the given elements.
      static <E> SortedSet<E> immutableSortedSet​(Stream<? extends E> elements)
      Creates an immutable SortedSet instance containing the given elements.
      static <E> List<E> mutableList()
      mutableList.
      static <E> List<E> mutableList​(E element)
      Creates a mutable List instance containing the given element
      static <E> List<E> mutableList​(E... elements)
      Creates a mutable List instance containing the given elements.
      static <E> List<E> mutableList​(Iterable<? extends E> elements)
      Creates a mutable List instance containing the given elements.
      static <E> List<E> mutableList​(Collection<? extends E> elements)
      Creates a mutable List instance containing the given elements.
      static <E> List<E> mutableList​(Iterator<? extends E> elements)
      Creates a mutable List instance containing the given elements.
      static <E> List<E> mutableList​(Stream<? extends E> elements)
      Creates a mutable List instance containing the given elements.
      static <K,​V>
      Map<K,​V>
      mutableMap()
      mutableMap.
      static <K,​V>
      Map<K,​V>
      mutableMap​(K k, V v)
      Convenience method for the inline creation of a map with values
      static <K,​V>
      Map<K,​V>
      mutableMap​(K k1, V v1, K k2, V v2)
      Convenience method for the inline creation of a map with values
      static <K,​V>
      Map<K,​V>
      mutableMap​(K k1, V v1, K k2, V v2, K k3, V v3)
      Convenience method for the inline creation of a map with values
      static <K,​V>
      Map<K,​V>
      mutableMap​(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
      Convenience method for the inline creation of a map with values
      static <E> Set<E> mutableSet()
      mutableSet.
      static <E> Set<E> mutableSet​(E element)
      mutableSet.
      static <E> Set<E> mutableSet​(E... elements)
      mutableSet.
      static <E> Set<E> mutableSet​(Iterable<? extends E> elements)
      Creates a mutable Set instance containing the given elements.
      static <E> Set<E> mutableSet​(Collection<? extends E> elements)
      Creates a mutable Set instance containing the given elements.
      static <E> Set<E> mutableSet​(Iterator<? extends E> elements)
      Creates a mutable Set instance containing the given elements.
      static <E> Set<E> mutableSet​(Stream<? extends E> elements)
      Creates a mutable Set instance containing the given elements.
      static <E> SortedSet<E> mutableSortedSet()
      mutableSortedSet.
      static <E> SortedSet<E> mutableSortedSet​(E element)
      mutableSortedSet.
      static <E> SortedSet<E> mutableSortedSet​(E... elements)
      mutableSortedSet.
      static <E> SortedSet<E> mutableSortedSet​(Iterable<? extends E> elements)
      Creates a mutable SortedSet instance containing the given elements.
      static <E> SortedSet<E> mutableSortedSet​(Iterator<? extends E> elements)
      Creates a mutable SortedSet instance containing the given elements.
      static <E> SortedSet<E> mutableSortedSet​(Stream<? extends E> elements)
      Creates a mutable SortedSet instance containing the given elements.
    • Method Detail

      • mutableList

        public static <E> List<E> mutableList()

        mutableList.

        Type Parameters:
        E - a E class
        Returns:
        a newly created empty ArrayList
      • mutableList

        @SafeVarargs
        public static <E> List<E> mutableList​(E... elements)
        Creates a mutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added
        Returns:
        the mutable List with the given elements
      • mutableList

        public static <E> List<E> mutableList​(E element)
        Creates a mutable List instance containing the given element
        Type Parameters:
        E - a E class
        Parameters:
        element - to be added
        Returns:
        the mutable List with the given element
      • mutableList

        public static <E> List<E> mutableList​(Iterable<? extends E> elements)
        Creates a mutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable List with the given elements
      • mutableList

        public static <E> List<E> mutableList​(Collection<? extends E> elements)
        Creates a mutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable List with the given elements
      • mutableList

        public static <E> List<E> mutableList​(Stream<? extends E> elements)
        Creates a mutable List instance containing the given elements. Caution: The stream will be consumed by this operation
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null an empty mutable List will be returned
        Returns:
        the mutable List with the given elements
      • mutableList

        public static <E> List<E> mutableList​(Iterator<? extends E> elements)
        Creates a mutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable List with the given elements
      • immutableList

        public static <E> List<E> immutableList()
        Creates an immutable List instance. Convenience method for Collections.emptyList()
        Type Parameters:
        E - a E class
        Returns:
        the immutable List without any element
      • immutableList

        @SafeVarargs
        public static <E> List<E> immutableList​(E... elements)
        Creates an immutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable List with the given elements
      • immutableList

        public static <E> List<E> immutableList​(E element)
        Creates an immutable List instance containing the given element.
        Type Parameters:
        E - a E class
        Parameters:
        element - to be wrapped, must not be null
        Returns:
        the immutable List with the given elements
      • immutableList

        public static <E> List<E> immutableList​(Iterable<? extends E> elements)
        Creates an immutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable List with the given elements
      • immutableList

        public static <E> List<E> immutableList​(Collection<? extends E> elements)
        Creates an immutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable List with the given elements. It implicitly creates a copy
      • immutableList

        public static <E> List<E> immutableList​(Stream<? extends E> elements)
        Creates an immutable List instance containing the given elements. Caution: The stream will be consumed by this operation
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable List with the given elements
      • immutableList

        public static <E> List<E> immutableList​(Iterator<? extends E> elements)
        Creates an immutable List instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable List with the given elements
      • mutableSet

        public static <E> Set<E> mutableSet()

        mutableSet.

        Type Parameters:
        E - a E class
        Returns:
        a newly created empty HashSet
      • mutableSet

        public static <E> Set<E> mutableSet​(E element)

        mutableSet.

        Type Parameters:
        E - a E class
        Parameters:
        element - to be added. If it is null it will not be added
        Returns:
        a newly created empty HashSet with the given elements
      • mutableSet

        @SafeVarargs
        public static <E> Set<E> mutableSet​(E... elements)

        mutableSet.

        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added
        Returns:
        a newly created empty HashSet with the given elements
      • mutableSet

        public static <E> Set<E> mutableSet​(Iterable<? extends E> elements)
        Creates a mutable Set instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable Set with the given elements
      • mutableSet

        public static <E> Set<E> mutableSet​(Collection<? extends E> elements)
        Creates a mutable Set instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable Set with the given elements
      • mutableSet

        public static <E> Set<E> mutableSet​(Iterator<? extends E> elements)
        Creates a mutable Set instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable Set with the given elements
      • mutableSet

        public static <E> Set<E> mutableSet​(Stream<? extends E> elements)
        Creates a mutable Set instance containing the given elements. Caution: The stream will be consumed by this operation
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable Set will be returned
        Returns:
        the mutable Set with the given elements
      • immutableSet

        public static <E> Set<E> immutableSet​(E element)
        Creates an immutable Set instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        element - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • immutableSet

        @SafeVarargs
        public static <E> Set<E> immutableSet​(E... elements)
        Creates an immutable Set instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • immutableSet

        public static <E> Set<E> immutableSet​(Iterable<? extends E> elements)
        Creates an immutable Set instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped
        Returns:
        the immutable Set with the given elements
      • immutableSet

        public static <E> Set<E> immutableSet​(Iterator<? extends E> elements)
        Creates an immutable Set instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • immutableSet

        public static <E> Set<E> immutableSet​(Stream<? extends E> elements)
        Creates an immutable Set instance containing the given elements. Caution: The stream will be consumed by this operation
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • mutableSortedSet

        public static <E> SortedSet<E> mutableSortedSet​(E element)

        mutableSortedSet.

        Type Parameters:
        E - a E class
        Parameters:
        element - to be added. If it is null an empty SortedSet will be returned
        Returns:
        a newly created empty TreeSet with the given element
      • mutableSortedSet

        @SafeVarargs
        public static <E> SortedSet<E> mutableSortedSet​(E... elements)

        mutableSortedSet.

        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added
        Returns:
        a newly created empty TreeSet with the given elements
      • mutableSortedSet

        public static <E> SortedSet<E> mutableSortedSet​(Iterable<? extends E> elements)
        Creates a mutable SortedSet instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable TreeSet with the given elements
      • mutableSortedSet

        public static <E> SortedSet<E> mutableSortedSet​(Iterator<? extends E> elements)
        Creates a mutable SortedSet instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable Set with the given elements
      • mutableSortedSet

        public static <E> SortedSet<E> mutableSortedSet​(Stream<? extends E> elements)
        Creates a mutable SortedSet instance containing the given elements. Caution: The stream will be consumed by this operation
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be added. If it is null and empty mutable list will be returned
        Returns:
        the mutable Set with the given elements
      • immutableSortedSet

        public static <E> SortedSet<E> immutableSortedSet​(E element)
        Creates an immutable SortedSet instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        element - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • immutableSortedSet

        @SafeVarargs
        public static <E> SortedSet<E> immutableSortedSet​(E... elements)
        Creates an immutable SortedSet instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • immutableSortedSet

        public static <E> SortedSet<E> immutableSortedSet​(Iterable<? extends E> elements)
        Creates an immutable SortedSet instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • immutableSortedSet

        public static <E> SortedSet<E> immutableSortedSet​(Iterator<? extends E> elements)
        Creates an immutable SortedSet instance containing the given elements.
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • immutableSortedSet

        public static <E> SortedSet<E> immutableSortedSet​(Stream<? extends E> elements)
        Creates an immutable SortedSet instance containing the given elements. Caution: The stream will be consumed by this operation
        Type Parameters:
        E - a E class
        Parameters:
        elements - to be wrapped, must not be null
        Returns:
        the immutable Set with the given elements
      • mutableMap

        public static <K,​V> Map<K,​V> mutableMap()

        mutableMap.

        Type Parameters:
        K - a K class
        V - a V class
        Returns:
        an empty mutable Map
      • mutableMap

        public static <K,​V> Map<K,​V> mutableMap​(K k,
                                                            V v)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k - key to be added
        v - value to be added
        Returns:
        a mutable Map with the given elements
      • mutableMap

        public static <K,​V> Map<K,​V> mutableMap​(K k1,
                                                            V v1,
                                                            K k2,
                                                            V v2)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k1 - key to be added
        v1 - value to be added
        k2 - key to be added
        v2 - value to be added
        Returns:
        a mutable Map with the given elements
      • mutableMap

        public static <K,​V> Map<K,​V> mutableMap​(K k1,
                                                            V v1,
                                                            K k2,
                                                            V v2,
                                                            K k3,
                                                            V v3)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k1 - key to be added
        v1 - value to be added
        k2 - key to be added
        v2 - value to be added
        k3 - key to be added
        v3 - value to be added
        Returns:
        a mutable Map with the given elements
      • mutableMap

        public static <K,​V> Map<K,​V> mutableMap​(K k1,
                                                            V v1,
                                                            K k2,
                                                            V v2,
                                                            K k3,
                                                            V v3,
                                                            K k4,
                                                            V v4)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k1 - key to be added
        v1 - value to be added
        k2 - key to be added
        v2 - value to be added
        k3 - key to be added
        v3 - value to be added
        k4 - key to be added
        v4 - value to be added
        Returns:
        a mutable Map with the given elements
      • immutableMap

        public static <K,​V> Map<K,​V> immutableMap()

        immutableMap.

        Type Parameters:
        K - a K class
        V - a V class
        Returns:
        an empty mutable Map
      • immutableMap

        public static <K,​V> Map<K,​V> immutableMap​(Stream<Map.Entry<K,​V>> elements)
        Copies the entries of the given map stream into a new map which is then returned as an unmodifiable map.
        Type Parameters:
        K - key type
        V - value type
        Parameters:
        elements - to be copied
        Returns:
        unmodifiable map with entries form the given stream
      • immutableMap

        public static <K,​V> Map<K,​V> immutableMap​(K k,
                                                              V v)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k - key to be added
        v - value to be added
        Returns:
        an immutable Map with the given elements
      • immutableMap

        public static <K,​V> Map<K,​V> immutableMap​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k1 - key to be added
        v1 - value to be added
        k2 - key to be added
        v2 - value to be added
        Returns:
        an immutable Map with the given elements
      • immutableMap

        public static <K,​V> Map<K,​V> immutableMap​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k1 - key to be added
        v1 - value to be added
        k2 - key to be added
        v2 - value to be added
        k3 - key to be added
        v3 - value to be added
        Returns:
        an immutable Map with the given elements
      • immutableMap

        public static <K,​V> Map<K,​V> immutableMap​(K k1,
                                                              V v1,
                                                              K k2,
                                                              V v2,
                                                              K k3,
                                                              V v3,
                                                              K k4,
                                                              V v4)
        Convenience method for the inline creation of a map with values
        Type Parameters:
        K - a K class
        V - a V class
        Parameters:
        k1 - key to be added
        v1 - value to be added
        k2 - key to be added
        v2 - value to be added
        k3 - key to be added
        v3 - value to be added
        k4 - key to be added
        v4 - value to be added
        Returns:
        an immutable Map with the given elements