Package es.urjc.etsii.grafo.util
Class CollectionUtil
java.lang.Object
es.urjc.etsii.grafo.util.CollectionUtil
Util methods to manipulate collections and arrays that are not part of the standard java API
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerateIntegerList
(int end) Generate a list of integers between 0, and a given value.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 liststatic <T> void
reverseFragment
(List<T> list, int start, int end) Reverse a fragment in a list, from start to end (inclusive)static void
Randomly permute the specified list using the specified source of randomness.static void
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.
-
Constructor Details
-
CollectionUtil
public CollectionUtil()
-
-
Method Details
-
reverseFragment
Reverse a fragment in a list, from start to end (inclusive)- Type Parameters:
T
- List type- Parameters:
list
- list to reversestart
- start index, inclusiveend
- end index, inclusive
-
reverse
Reverse a list- Parameters:
list
- list to reverse
-
pickRandom
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
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
Return a primitive array with all the numbers of the given collection.- Parameters:
c
- Input collection- Returns:
- Primitive array
-
toDoubleArray
Return a primitive array with all the numbers of the given collection.- Parameters:
c
- Input collection- Returns:
- Primitive array
-
toLongArray
Return a primitive array with all the numbers of the given collection.- Parameters:
c
- Input collection- Returns:
- Primitive array
-
swap
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 eitheri
orj
is out of range (i < 0 || i >= list.size() || j < 0 || j >= list.size()).- Since:
- 1.4
-
shuffle
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 theRandomAccess
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 theset
operation.
-
generateIntegerList
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 valueend
- the exclusive upper bound- Returns:
- a sequential List for the range of int elements
-
generateIntegerList
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
-