Class ArrayUtils

  • All Implemented Interfaces:

    
    public final class ArrayUtils
    
                        
    • Constructor Detail

    • Method Detail

      • fastEachReversed

         final static <L extends List<E>, RandomAccess, E extends Any> Unit fastEachReversed(L $self, Integer beginAt, Function1<E, Unit> f)

        forEach re-implementation that doesn't allocate any memory, but it traverses the list backwards.

        Utilizes the RandomAccess trait.

        Parameters:
        f - The function to apply to each element.
        Since:

        0.18.5

      • noneAre

         final static <L extends List<E>, RandomAccess, E extends Any> Boolean noneAre(L $self, Function1<E, Boolean> f)

        Returns true if no elements match the given predicate.

        Note this function will return true if the list is empty, according to the principle of Vacuous truth, because there are no elements that match the given predicate.

      • allAre

         final static <L extends List<E>, RandomAccess, E extends Any> Boolean allAre(L $self, Function1<E, Boolean> f)

        Returns true if all elements match the given predicate.

        Note this function will return true if the list is empty, according to the principle of Vacuous truth, and the fact that there are no elements that don't match the given predicate.

      • sortedByDescending

         final static <L extends MutableList<E>, RandomAccess, E extends Any, R extends Comparable<R>> L sortedByDescending(L $self, Function1<E, R> selector)

        Returns a list of all elements sorted descending according to natural sort order of the value returned by specified selector function.

        The sort is stable. It means that equal elements preserve their order relative to each other after sorting.

      • sumOf

         final static <L extends List<E>, RandomAccess, E extends Any> Float sumOf(L $self, Function1<E, Float> selector)

        Returns the sum of all values produced by selector function applied to each element in the collection, for floats.

        Parameters:
        selector - a function that extracts a Float property of an element
        Returns:

        the sum of all values produced by selector

      • addOrReplace

         final static <E extends Any> E addOrReplace(List<E> $self, E item, Integer index)

        This function will add the given item to the end of the list if it doesn't exist, or replace it if it does.

        Returns:

        the old value if it was replaced, or null if it was added.

      • putIfAbsent

         final static <E extends Any> E putIfAbsent(List<E> $self, E item)

        If the specified item is not already present, appends it to the end of this list, and returns null. Else, returns the item in this list.

        Parameters:
        item - the item to add
        Returns:

        the previous value associated in this list, or null if was not found and added successfully.

        Since:

        0.23.2

      • moveElement

         final static <E extends Any> Unit moveElement(Array<E> $self, Integer from, Integer to)

        Moves the given element from the from index to the to index.

        Note: this method makes absolutely no attempt to verify if the given indices are valid.

        Parameters:
        from - the index of the element to move
        to - the index to move the element to
      • append

         final static <E extends Any> Array<E> append(Array<E> $self, E element, Boolean stillPutOnFail)

        Append element to the end of this array, that is, the first empty index.

        Parameters:
        element - the element to append
        stillPutOnFail - set this to true if you want it to add the element (causes a reallocation!) even if the array is full.
      • asArrayList

         final static <T extends Any> ArrayList<T> asArrayList(Iterable<T> $self)

        Return this iterable as an ArrayList. Note: if it is already an ArrayList, it will be returned as-is.

      • clearUsing

         final static <E extends Any> Unit clearUsing(Collection<E> $self, Function1<E, Unit> action)

        Clears the collection using the action, avoiding ConcurrentModificationException.

        This works by allocating a new list and iterating over it, removing each element from the original collection.

        Parameters:
        action - should be used to directly remove itself from the collection.
      • indexOfOrDie

         final static <E extends Any> Integer indexOfOrDie(List<E> $self, E element, Function0<Void> or)

        Returns the index of the element in the list or runs the or parameter if the element is not found.

        This returns Nothing, and is used by default to throw an exception; hence why it's called "or die".