Class AoCollections


  • public class AoCollections
    extends Object
    General-purpose collection utilities and constants.
    Author:
    AO Industries, Inc.
    • Field Detail

      • EMPTY_SORTED_SET

        public static final SortedSet<?> EMPTY_SORTED_SET
    • Method Detail

      • emptySortedSet

        public static final <T> SortedSet<T> emptySortedSet()
        Java 1.8: use standard version
      • singletonSortedSet

        public static <T> SortedSet<T> singletonSortedSet​(T o)
      • optimalUnmodifiableCollection

        public static <T> Collection<T> optimalUnmodifiableCollection​(Collection<? extends T> collection)
        Gets the optimal implementation for unmodifiable collection. If the collection is already unmodifiable, returns the same collection. If collection is empty, uses Collections.emptyList. If collection has one element, uses Collections.singletonList. Otherwise, wraps the collection with Collections.unmodifiableCollection.
      • asCollection

        public static <E> Collection<E> asCollection​(Iterable<E> iterable)
        Gets a collection from an iterable. Casts the iterable to collection, if possible. Otherwise builds a new list from the iterable, maintaining iteration order.
      • unmodifiableCopyCollection

        public static <T> Collection<T> unmodifiableCopyCollection​(Collection<? extends T> collection)
        Performs defensive shallow copy and returns unmodifiable collection.
      • unmodifiableCopyCollection

        public static <T> Collection<T> unmodifiableCopyCollection​(Iterable<? extends T> iter)
        Performs defensive shallow copy and returns unmodifiable collection.
      • asList

        public static <E> List<E> asList​(Iterable<E> iterable)
        Gets a list from an iterable. Casts the iterable to list, if possible. Otherwise builds a new list from the iterable, maintaining iteration order.
      • unmodifiableCopyList

        public static <T> List<T> unmodifiableCopyList​(Collection<? extends T> collection)
        Performs defensive shallow copy and returns unmodifiable list.
      • unmodifiableCopyList

        public static <T> List<T> unmodifiableCopyList​(Iterable<? extends T> iter)
        Performs defensive shallow copy and returns unmodifiable list.
      • optimalUnmodifiableSet

        public static <T> Set<T> optimalUnmodifiableSet​(Set<? extends T> set)
        Gets the optimal implementation for unmodifiable set. If set is empty, uses Collections.emptySet. If set has one element, uses Collections.singleton. Otherwise, wraps the set with Collections.unmodifiableSet.
      • asSet

        public static <E> Set<E> asSet​(Iterable<E> iterable)
        Gets a set from an iterable. Casts the iterable to set, if possible. Otherwise builds a new set from the iterable, maintaining iteration order.
      • unmodifiableCopySet

        public static <T> Set<T> unmodifiableCopySet​(Collection<? extends T> collection)
        Performs defensive shallow copy and returns unmodifiable set. The iteration order of the original set is maintained.
      • unmodifiableCopySet

        public static <T> Set<T> unmodifiableCopySet​(Iterable<? extends T> iter)
        Performs defensive shallow copy and returns unmodifiable set. The iteration order of the original set is maintained.
      • optimalUnmodifiableSortedSet

        public static <T> SortedSet<T> optimalUnmodifiableSortedSet​(SortedSet<T> sortedSet)
        Gets the optimal implementation for unmodifiable sorted set. If sorted set is empty, uses emptySortedSet. If sorted set has one element, uses singletonSortedSet. Otherwise, wraps the sorted set with Collections.unmodifiableSortedSet.
      • asSortedSet

        public static <E> SortedSet<E> asSortedSet​(Iterable<E> iterable)
        Gets a sorted set from an iterable. Casts the iterable to sorted set, if possible. Otherwise builds a new sorted set from the iterable, in natural ordering.
      • unmodifiableCopySortedSet

        public static <T> SortedSet<T> unmodifiableCopySortedSet​(Collection<? extends T> collection)
        Performs defensive shallow copy and returns unmodifiable sorted set.
      • unmodifiableCopySortedSet

        public static <T> SortedSet<T> unmodifiableCopySortedSet​(Iterable<? extends T> iter)
        Performs defensive shallow copy and returns unmodifiable sorted set.
      • optimalUnmodifiableMap

        public static <K,​V> Map<K,​V> optimalUnmodifiableMap​(Map<? extends K,​? extends V> map)
        Gets the optimal implementation for unmodifiable map. If map is empty, uses Collections.emptyMap. If map has one element, uses Collections.singletonMap. Otherwise, wraps the map with Collections.unmodifiableMap.
      • unmodifiableCopyMap

        public static <K,​V> Map<K,​V> unmodifiableCopyMap​(Map<? extends K,​? extends V> map)
        Performs defensive shallow copy and returns unmodifiable map. The iteration order of the original set is maintained.
      • optimalUnmodifiableSortedMap

        public static <K,​V> SortedMap<K,​V> optimalUnmodifiableSortedMap​(SortedMap<K,​? extends V> sortedMap)
        Gets the optimal implementation for unmodifiable sorted map. If sorted map is empty, uses emptySortedMap. If sorted map has one element, uses singletonSortedMap. Otherwise, wraps the sorted map with Collections.unmodifiableSortedMap.
      • unmodifiableCopySortedMap

        public static <K,​V> SortedMap<K,​V> unmodifiableCopySortedMap​(Map<K,​? extends V> map)
        Performs defensive shallow copy and returns unmodifiable sorted map.
      • unmodifiableIterator

        public static <E> Iterator<E> unmodifiableIterator​(Iterator<? extends E> iter)
        Wraps an iterator to make it unmodifiable.
      • peekIterator

        public static <E> AoCollections.PeekIterator<E> peekIterator​(Iterator<? extends E> iter)
        Wraps the provided iterator, allowing peek of first element. Does not support null elements.
      • equals

        public static boolean equals​(Collection<?> collection1,
                                     Collection<?> collection2)
        Two collections are considered equal when they are the same size and have the same elements in the same iteration order. If both collections are null they are also considered equal.
      • hashCode

        public static int hashCode​(Iterable<?> iterable)
        Computes the hashCode of a collection in a manner consistent with AbstractList.
        See Also:
        AbstractList.hashCode()
      • filter

        public static <E,​R extends E> List<R> filter​(List<? extends E> list,
                                                           Class<? extends R> clazz)
        Filters a list for all elements of a given class.
      • filterByValue

        public static <K,​V> Set<K> filterByValue​(Map<? extends K,​? extends V> map,
                                                       V value)
        Returns a modifiable set of all the keys in a map that match the given value. This is a copy of the keys and will not write-through or be altered by the original map. The set will have the same iteration order as the original map.
      • filterByValue

        public static <K,​V> SortedSet<K> filterByValue​(SortedMap<K,​? extends V> map,
                                                             V value)
        Returns a modifiable sorted set of all the keys in a sorted map that match the given value. This is a copy of the keys and will not write-through or be altered by the original map. The set uses the same comparator as the original map.