Class CollectionHelpers

java.lang.Object
net.apartium.cocoabeans.CollectionHelpers

public class CollectionHelpers extends Object
Helper class to aid working with java collections
  • Constructor Details

    • CollectionHelpers

      public CollectionHelpers()
  • Method Details

    • randomEntry

      public static <E> E randomEntry(Collection<E> collection, Random random)
      Picks a random entry from collection
      Type Parameters:
      E - collection element type
      Parameters:
      collection - collection
      Returns:
      null if collection is empty, the first entry if there is only one or a random entry
    • pickEntry

      public static <E> E pickEntry(Collection<E> collection, int index)
      Attempts to pick entry from Collection by index. WARNING: Resulting entry of this object is not guaranteed to be the same across multiple attempts even if given arguments are the same.
      Type Parameters:
      E - collection type
      Parameters:
      collection - collection
      index - index to pick
      Returns:
      attempt to pick given element from set
    • containsStartsWith

      public static boolean containsStartsWith(Collection<String> collection, String value, String appendToElement)
      Checks whether given string contains AT LEAST ONE OF the values in given collection
      Parameters:
      collection - collection
      value - string
      appendToElement - string to append to the beginning of each collection element
      Returns:
      true if string contains at least one of the values in given collection, else false.
    • equalsArray

      public static boolean equalsArray(Object[] arr0, Object[] arr1)
      Checks for array content equality, regardless of order
      Parameters:
      arr0 - first array
      arr1 - second array
      Returns:
      true if equals, else false
    • equalsList

      public static boolean equalsList(List<?> list0, List<?> list1)
      Checks for list content equality, regardless of order
      Parameters:
      list0 - first list
      list1 - second list
      Returns:
      true if equals, else false
    • equalsCollections

      @AvailableSince("0.0.39") public static boolean equalsCollections(Collection<?> a, Collection<?> b)
      Determines whether two collections are equal, and in the same order
      Parameters:
      a - the first collection
      b - the second collection
      Returns:
      true if equals, otherwise false
    • range

      public static List<Integer> range(int start, int end, int step)
      Construct a new instance with values consisting of specified range
      Parameters:
      start - range start
      end - range end
      step - amount to step each time
      Returns:
      a new linked list instance
    • range

      @AvailableSince("0.0.45") public static List<Double> range(double start, double end, double step)
      Construct a new instance with values consisting of specified range
      Parameters:
      start - range start
      end - range end
      step - amount to step each time
      Returns:
      a new instance of list that has elements from start to end with jumps of step
    • addElementSorted

      public static <E> void addElementSorted(List<E> list, E element, Comparator<E> comparator)
    • mergeInto

      @AvailableSince("0.0.37") public static <K, V> void mergeInto(Map<K,V> map, Map<K,V> other)
      Put all entries from other map into map if key is not present
      Type Parameters:
      K - key type
      V - value type
      Parameters:
      map - map to put into
      other - map to get entries from
    • isSorted

      @AvailableSince("0.0.39") public static <T> boolean isSorted(List<T> list, Comparator<? super T> comparator)
      Determines whether a list is sorted according to provided comparator
      Type Parameters:
      T - list element type
      Parameters:
      list - list to check against
      comparator - comparator
      Returns:
      true if sorted, else false