Class CollectionUtil

java.lang.Object
es.urjc.etsii.grafo.util.CollectionUtil

public class CollectionUtil extends Object
Util methods to manipulate collections and arrays that are not part of the standard java API
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<Integer>
    Generate a list of integers between 0, and a given value.
    static List<Integer>
    generateIntegerList(int start, int end)
    returns a sequential ordered List from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.
    static <T> T
    pickRandom(List<T> list)
    Picks a random element from the given list.
    static <T> T
    pickRandom(Set<T> set)
    Picks a random element from the given set.
    static void
    Reverse a list
    static <T> void
    reverseFragment(List<T> list, int start, int end)
    Reverse a fragment in a list, from start to end (inclusive)
    static void
    shuffle(List<?> list)
    Randomly permute the specified list using the specified source of randomness.
    static void
    swap(List<?> list, int i, int j)
    From the official javadocs Swaps the elements at the specified positions in the specified list.
    static double[]
    Return a primitive array with all the numbers of the given collection.
    static int[]
    Return a primitive array with all the numbers of the given collection.
    static long[]
    Return a primitive array with all the numbers of the given collection.

    Methods inherited from class java.lang.Object

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

    • CollectionUtil

      public CollectionUtil()
  • Method Details

    • reverseFragment

      public static <T> void reverseFragment(List<T> list, int start, int end)
      Reverse a fragment in a list, from start to end (inclusive)
      Type Parameters:
      T - List type
      Parameters:
      list - list to reverse
      start - start index, inclusive
      end - end index, inclusive
    • reverse

      public static void reverse(List<Integer> list)
      Reverse a list
      Parameters:
      list - list to reverse
    • pickRandom

      public static <T> T pickRandom(Set<T> set)
      Picks a random element from the given set. Each element has the same probability of being chosen.
      Type Parameters:
      T - Set type
      Parameters:
      set - Set where random element will be chosen from
      Returns:
      Chosen element
    • pickRandom

      public static <T> T pickRandom(List<T> list)
      Picks a random element from the given list. Each element has the same probability of being chosen.
      Type Parameters:
      T - List type
      Parameters:
      list - List where random element will be chosen from
      Returns:
      Chosen element
    • toIntArray

      public static int[] toIntArray(Collection<Integer> c)
      Return a primitive array with all the numbers of the given collection.
      Parameters:
      c - Input collection
      Returns:
      Primitive array
    • toDoubleArray

      public static double[] toDoubleArray(Collection<Double> c)
      Return a primitive array with all the numbers of the given collection.
      Parameters:
      c - Input collection
      Returns:
      Primitive array
    • toLongArray

      public static long[] toLongArray(Collection<Long> c)
      Return a primitive array with all the numbers of the given collection.
      Parameters:
      c - Input collection
      Returns:
      Primitive array
    • swap

      public static void swap(List<?> list, int i, int j)
      From the official javadocs Swaps the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)
      Parameters:
      list - The list in which to swap elements.
      i - the index of one element to be swapped.
      j - the index of the other element to be swapped.
      Throws:
      IndexOutOfBoundsException - if either i or j is out of range (i < 0 || i >= list.size() || j < 0 || j >= list.size()).
      Since:
      1.4
    • shuffle

      public static void shuffle(List<?> list)
      Randomly permute the specified list using the specified source of randomness. All permutations occur with equal likelihood assuming that the source of randomness is fair. This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive. This method runs in linear time. If the specified list does not implement the RandomAccess interface and is large, this implementation dumps the specified list into an array before shuffling it, and dumps the shuffled array back into the list. This avoids the quadratic behavior that would result from shuffling a "sequential access" list in place.
      Parameters:
      list - the list to be shuffled.
      Throws:
      UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.
    • generateIntegerList

      public static List<Integer> generateIntegerList(int start, int end)
      returns a sequential ordered List from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1. If the start is greater or equal than the end, the returned list will be empty.
      Parameters:
      start - the (inclusive) initial start value
      end - the exclusive upper bound
      Returns:
      a sequential List for the range of int elements
    • generateIntegerList

      public static List<Integer> generateIntegerList(int end)
      Generate a list of integers between 0, and a given value. If end number is less than 0, the returned list will be empty.
      Parameters:
      end - End value
      Returns:
      List of integers