Class CollectionUtil

java.lang.Object
org.agrona.collections.CollectionUtil

public final class CollectionUtil extends Object
Utility functions for collection objects.
  • Constructor Details

    • CollectionUtil

      private CollectionUtil()
  • Method Details

    • getOrDefault

      public static <K, V> V getOrDefault(Map<K,V> map, K key, 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(List<V> values, 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(List<T> values, 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.