Class CollectionUtil


  • public final class CollectionUtil
    extends java.lang.Object
    Utility functions for collection objects.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CollectionUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      V
      getOrDefault​(java.util.Map<K,​V> map, K key, java.util.function.Function<K,​V> supplier)
      A getOrDefault that doesn't create garbage if its suppler is non-capturing.
      static <T> int removeIf​(java.util.List<T> values, java.util.function.Predicate<T> predicate)
      Remove element from a list if it matches a predicate.
      static <V> int sum​(java.util.List<V> values, java.util.function.ToIntFunction<V> function)
      Garbage free sum function.
      static void validateLoadFactor​(float loadFactor)
      Validate that a load factor is in the range of 0.1 to 0.9.
      static void validatePositivePowerOfTwo​(int value)
      Validate that a number is a power of two.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CollectionUtil

        private CollectionUtil()
    • Method Detail

      • getOrDefault

        public static <K,​V> V getOrDefault​(java.util.Map<K,​V> map,
                                                 K key,
                                                 java.util.function.Function<K,​V> supplier)
        A getOrDefault that doesn't create garbage if its suppler is non-capturing.
        Type Parameters:
        K - type of the key
        V - type of the value
        Parameters:
        map - to perform the lookup on.
        key - on which the lookup is done.
        supplier - of the default value if one is not found.
        Returns:
        the value if found or a new default which as been added to the map.
      • sum

        public static <V> int sum​(java.util.List<V> values,
                                  java.util.function.ToIntFunction<V> function)
        Garbage free sum function.

        Note: the list must implement RandomAccess to be efficient.

        Type Parameters:
        V - the value to add up
        Parameters:
        values - the list of input values
        function - function that map each value to an int
        Returns:
        the sum of all the int values returned for each member of the list.
      • validateLoadFactor

        public static void validateLoadFactor​(float loadFactor)
        Validate that a load factor is in the range of 0.1 to 0.9.

        Load factors in the range 0.5 - 0.7 are recommended for open-addressing with linear probing.

        Parameters:
        loadFactor - to be validated.
      • validatePositivePowerOfTwo

        public static void validatePositivePowerOfTwo​(int value)
        Validate that a number is a power of two.
        Parameters:
        value - to be validated.
      • removeIf

        public static <T> int removeIf​(java.util.List<T> values,
                                       java.util.function.Predicate<T> predicate)
        Remove element from a list if it matches a predicate.

        Note: the list must implement RandomAccess to be efficient.

        Type Parameters:
        T - type of the value.
        Parameters:
        values - to be iterated over.
        predicate - to test the value against
        Returns:
        the number of items remove.