Class CollectionTools


  • public class CollectionTools
    extends Object
    Utility static functions for working with collections. User: Leonid.Bushuev Date: 04.09.2009 Time: 19:17:40
    • Constructor Summary

      Constructors 
      Constructor Description
      CollectionTools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      com.google.common.collect.ImmutableMap<K,​V>
      addTo​(com.google.common.collect.ImmutableMap<K,​V> map, K key, V value)  
      static <T extends Comparable<T>>
      Couple<SortedSet<T>>
      diffs​(SortedSet<T> set1, SortedSet<T> set2)
      Makes diffs between two sets, using the fast merge-anti-join algorythm.
      static boolean eq​(Object object1, Object object2)
      Compares two objects for equivalence safely (in other words - accepts nulls).
      static void filterOut​(Collection<String> strings, Pattern pattern)
      Removes strings that matches the pattern.
      static <T> boolean has​(Collection<T> items, com.google.common.base.Predicate<T> predicate)  
      static <T> com.google.common.collect.ImmutableSet<T> minus​(Set<T> minuend, Set<? super T> subtrahend)
      A simple mathematical difference between two sets.
      static <T extends Comparable<T>>
      com.google.common.collect.ImmutableSortedSet<T>
      minus​(SortedSet<T> minuend, SortedSet<? super T> subtrahend)  
      static <K,​V>
      com.google.common.collect.ImmutableMap<K,​V>
      removeFrom​(com.google.common.collect.ImmutableMap<K,​V> map, K key)  
      static <K extends Comparable,​V>
      com.google.common.collect.ImmutableSortedMap<K,​V>
      sameKeysDiffValues​(SortedMap<K,​V> map1, SortedMap<K,​V> map2)
      Walks throw the given maps and looks for entries with keys common for both maps but values are differents.
      static <K,​V>
      Couple<com.google.common.collect.ImmutableSortedMap<K,​V>>
      separateByKeyCondition​(SortedMap<K,​V> map, com.google.common.base.Predicate<K> predicate)
      Separates (discriminates) a sorted map onto two maps by key conditions.
      static <T> Set<T> setOf​(Collection<T> collection)
      Makes an immutable set of the given collection.
      static <T> List<List<T>> splitByMax​(Collection<T> items, int maxItemsPerPart)  
    • Constructor Detail

      • CollectionTools

        public CollectionTools()
    • Method Detail

      • setOf

        @NotNull
        public static <T> Set<T> setOf​(@Nullable
                                       Collection<T> collection)
        Makes an immutable set of the given collection.
        Type Parameters:
        T - type of items.
        Parameters:
        collection - collection to get copy of.
        Returns:
        immutable set, or an empty set if the source was null.
      • minus

        @NotNull
        public static <T> com.google.common.collect.ImmutableSet<T> minus​(@Nullable
                                                                          Set<T> minuend,
                                                                          @Nullable
                                                                          Set<? super T> subtrahend)
        A simple mathematical difference between two sets.
        Type Parameters:
        T - type of the item.
        Parameters:
        minuend - the minuend; null means an empty set. The set must not contain nulls.
        subtrahend - the subtrahend; null means an empty set.
        Returns:
        the set of items that presents in minuend and don't present in subtrahend.
      • minus

        @NotNull
        public static <T extends Comparable<T>> com.google.common.collect.ImmutableSortedSet<T> minus​(@Nullable
                                                                                                      SortedSet<T> minuend,
                                                                                                      @Nullable
                                                                                                      SortedSet<? super T> subtrahend)
      • diffs

        @NotNull
        public static <T extends Comparable<T>> Couple<SortedSet<T>> diffs​(@Nullable
                                                                           SortedSet<T> set1,
                                                                           @Nullable
                                                                           SortedSet<T> set2)
        Makes diffs between two sets, using the fast merge-anti-join algorythm.

        Both sets should use the same comparator.

        Type Parameters:
        T - items type.
        Parameters:
        set1 - the first set (nulls inside the set are not allowed).
        set2 - the second set (nulls inside the set are not allowed).
        Returns:
        pair with two elements:
        • the "a" element is a set that contains items from set1 which not present in set2 (or an empty set),
        • the "b" element is a set that contains items from set2 which not present in set1 (or an empty set);
        result sets can be immutable.
      • sameKeysDiffValues

        @NotNull
        public static <K extends Comparable,​V> com.google.common.collect.ImmutableSortedMap<K,​V> sameKeysDiffValues​(@NotNull
                                                                                                                                SortedMap<K,​V> map1,
                                                                                                                                @NotNull
                                                                                                                                SortedMap<K,​V> map2)
        Walks throw the given maps and looks for entries with keys common for both maps but values are differents. Uses the fast merge-join algorythm.

        The result map will contain keys with values from the set2.

        For example, when the first map is

        • "moskvich" -> "red"
        • "uaz" -> "khaki"
        • "volga" -> "black"
        • "zaporozhets" -> "yellow"
        • "zhiguli" -> "blue"
        and the second map is
        • "kamaz" -> "orange"
        • "moskvich" -> "red"
        • "subaru" -> "indigo"
        • "uaz" -> "khaki"
        • "volga" -> "white"
        • "zaporozhets" -> "red"
        the result will be
        • "volga" -> "white"
        • "zaporozhets" -> "red"

        Type Parameters:
        K - type of the key.
        V - type of the value.
        Parameters:
        map1 - the first map.
        map2 - the second map.
        Returns:
        result map with entries from the second map and comparator got from the secind map too, or an empty map if no results.
      • splitByMax

        @NotNull
        public static <T> List<List<T>> splitByMax​(@NotNull
                                                   Collection<T> items,
                                                   int maxItemsPerPart)
      • separateByKeyCondition

        public static <K,​V> Couple<com.google.common.collect.ImmutableSortedMap<K,​V>> separateByKeyCondition​(@NotNull
                                                                                                                         SortedMap<K,​V> map,
                                                                                                                         @NotNull
                                                                                                                         com.google.common.base.Predicate<K> predicate)
        Separates (discriminates) a sorted map onto two maps by key conditions.
        Type Parameters:
        K - key type.
        V - value type.
        Parameters:
        map - source map to separate.
        predicate - key predicate.
        Returns:
        couple of maps: map Couple.a refers to entries wich keys match the predicate, and map Couple.b refers to entries wich keys don't match the predicate.
      • has

        public static <T> boolean has​(@NotNull
                                      Collection<T> items,
                                      @NotNull
                                      com.google.common.base.Predicate<T> predicate)
      • filterOut

        public static void filterOut​(@NotNull
                                     Collection<String> strings,
                                     @NotNull
                                     Pattern pattern)
        Removes strings that matches the pattern.
        Parameters:
        strings - writeable collection of strings to remove from.
        pattern - patther for undesired strings.
        Since:
        7.1
      • addTo

        @NotNull
        public static <K,​V> com.google.common.collect.ImmutableMap<K,​V> addTo​(@NotNull
                                                                                          com.google.common.collect.ImmutableMap<K,​V> map,
                                                                                          @NotNull
                                                                                          K key,
                                                                                          @NotNull
                                                                                          V value)
      • removeFrom

        @NotNull
        public static <K,​V> com.google.common.collect.ImmutableMap<K,​V> removeFrom​(@NotNull
                                                                                               com.google.common.collect.ImmutableMap<K,​V> map,
                                                                                               K key)
      • eq

        public static boolean eq​(@Nullable
                                 Object object1,
                                 @Nullable
                                 Object object2)
        Compares two objects for equivalence safely (in other words - accepts nulls).
        Parameters:
        object1 - the first object.
        object2 - the second object.
        Returns:
        true if objects are equal.