- java.lang.Object
-
- org.arakhne.afc.util.ListUtil
-
public final class ListUtil extends Object
Utilities on lists.- Since:
- 0.4
- Version:
- 17.0 2020-01-04 14:41:38
- Author:
- Stéphane GALLAND
- Maven Group Id:
- org.arakhne.afc.core
- Maven Artifact Id:
- util
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> int
add(List<E> list, Comparator<? super E> comparator, E data, boolean allowMultipleOccurencesOfSameValue, boolean allowReplacement)
Add the given element in the main list using a dichotomic algorithm.static <E> int
addIfAbsent(List<E> list, Comparator<? super E> comparator, E data)
Add the given element in the main list using a dichotomic algorithm if the element is not already present.static <T> int
ceilingIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the leastest element in this list greater than or equal to the given element, or-1
if there is no such element.static <E> boolean
contains(List<E> list, Comparator<? super E> comparator, E data)
Replies if the given element is inside the list, using a dichotomic algorithm.static <T> int
floorIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the greatest element in this list less than or equal to the given element, or-1
if there is no such element.static <T> int
getInsertionIndex(List<T> list, Comparator<? super T> comparator, T elt)
Replies the index at which the given element may be added in a sorted list.static <T> int
getInsertionIndex(List<T> list, Comparator<? super T> comparator, T elt, boolean allowMultiple)
Replies the index at which the given element may be added in a sorted list.static <T> int
higherIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the least element in this list strictly greater than the given element, or-1
if there is no such element.static <T> int
indexOf(List<T> list, Comparator<? super T> comparator, T elt)
Replies the index of the given data in the given list according to a dichotomic search algorithm.static <T> int
lastIndexOf(List<T> list, Comparator<? super T> comparator, T elt)
Replies the last index of the given data in the given list according to a dichotomic search algorithm.static <T> int
lowerIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the greatest element in this list strictly lower than the given element, or-1
if there is no such element.static <E> int
remove(List<E> list, Comparator<? super E> comparator, E data)
Remove the given element from the list using a dichotomic algorithm.static <T> Iterator<T>
reverseIterator(List<T> list)
Replies an iterator that goes from end to start of the given list.
-
-
-
Method Detail
-
remove
public static <E> int remove(List<E> list, Comparator<? super E> comparator, E data)
Remove the given element from the list using a dichotomic algorithm.This function ensure that the comparator is invoked as:
comparator(data, dataAlreadyInList)
.- Type Parameters:
E
- is the type of the elements in the list.- Parameters:
list
- is the list to change.comparator
- is the comparator of elements.data
- is the data to remove.- Returns:
- the index at which the element was removed in the list; or
-1
if the element was not removed.
-
addIfAbsent
public static <E> int addIfAbsent(List<E> list, Comparator<? super E> comparator, E data)
Add the given element in the main list using a dichotomic algorithm if the element is not already present.This function ensure that the comparator is invoked as:
comparator(data, dataAlreadyInList)
.- Type Parameters:
E
- is the type of the elements in the list.- Parameters:
list
- is the list to change.comparator
- is the comparator of elements.data
- is the data to insert.- Returns:
- the index where the element was inserted, or
-1
if the element was not inserted. - Since:
- 14.0
-
add
public static <E> int add(List<E> list, Comparator<? super E> comparator, E data, boolean allowMultipleOccurencesOfSameValue, boolean allowReplacement)
Add the given element in the main list using a dichotomic algorithm.This function ensure that the comparator is invoked as:
comparator(data, dataAlreadyInList)
.- Type Parameters:
E
- is the type of the elements in the list.- Parameters:
list
- is the list to change.comparator
- is the comparator of elements.data
- is the data to insert.allowMultipleOccurencesOfSameValue
- indicates if multiple occurrences of the same value are allowed in the list.allowReplacement
- indicates if the givenelt
may replace the found element.- Returns:
- the index where the element was inserted, or
-1
if the element was not inserted.
-
contains
@Pure public static <E> boolean contains(List<E> list, Comparator<? super E> comparator, E data)
Replies if the given element is inside the list, using a dichotomic algorithm.This function ensure that the comparator is invoked as:
comparator(data, dataAlreadyInList)
.- Type Parameters:
E
- is the type of the elements in the list.- Parameters:
list
- is the list to explore.comparator
- is the comparator of elements.data
- is the data to search for.- Returns:
true
if the data is inside the list, otherwisefalse
-
indexOf
@Pure public static <T> int indexOf(List<T> list, Comparator<? super T> comparator, T elt)
Replies the index of the given data in the given list according to a dichotomic search algorithm. Order between objects is given bycomparator
.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
- Type Parameters:
T
- is the type of the data to search for.- Parameters:
list
- is the list inside which the element should be searched.comparator
- is the comparator used to sort the list.elt
- is the element to search for.- Returns:
- the index at which the element is, or
-1
if the element was not found.
-
lastIndexOf
@Pure public static <T> int lastIndexOf(List<T> list, Comparator<? super T> comparator, T elt)
Replies the last index of the given data in the given list according to a dichotomic search algorithm. Order between objects is given bycomparator
.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
- Type Parameters:
T
- is the type of the data to search for.- Parameters:
list
- is the list inside which the element should be searched.comparator
- is the comparator used to sort the list.elt
- is the element to search for.- Returns:
- the last index at which the element is, or
-1
if the element was not found.
-
getInsertionIndex
@Pure public static <T> int getInsertionIndex(List<T> list, Comparator<? super T> comparator, T elt)
Replies the index at which the given element may be added in a sorted list.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
This function assumes that the given
elt
may appear many times in the list.- Type Parameters:
T
- is the type of the elements.- Parameters:
comparator
- is the comparator used to sort the list.elt
- is the element to add in.list
- is the list inside which the element should be added.- Returns:
- the index at which the element may be added.
-
getInsertionIndex
@Pure public static <T> int getInsertionIndex(List<T> list, Comparator<? super T> comparator, T elt, boolean allowMultiple)
Replies the index at which the given element may be added in a sorted list.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
This function assumes that the given
elt
may appear many times in the list.- Type Parameters:
T
- is the type of the elements.- Parameters:
comparator
- is the comparator used to sort the list.elt
- is the element to add in.list
- is the list inside which the element should be added.allowMultiple
- indicates if the givenelt
may appear many times in the list, or not.- Returns:
- the index at which the element may be added.
-
ceilingIndex
@Pure public static <T> int ceilingIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the leastest element in this list greater than or equal to the given element, or-1
if there is no such element.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
- Type Parameters:
T
- is the type of the data to search for.- Parameters:
list
- is the list inside which the element should be searched.comparator
- is the comparator used to sort the list.elt
- is the value to match.- Returns:
- the index of leastest element greater than or equal to
elt
, or-1
if there is no such element. - See Also:
NavigableSet.ceiling(Object)
-
floorIndex
@Pure public static <T> int floorIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the greatest element in this list less than or equal to the given element, or-1
if there is no such element.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
- Type Parameters:
T
- is the type of the data to search for.- Parameters:
list
- is the list inside which the element should be searched.comparator
- is the comparator used to sort the list.elt
- is the value to match.- Returns:
- the index of greatest element less than or equal to
elt
, or-1
if there is no such element. - See Also:
NavigableSet.floor(Object)
-
higherIndex
@Pure public static <T> int higherIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the least element in this list strictly greater than the given element, or-1
if there is no such element.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
- Type Parameters:
T
- is the type of the data to search for.- Parameters:
list
- is the list inside which the element should be searched.comparator
- is the comparator used to sort the list.elt
- is the value to match.- Returns:
- the index of least element strictly greater than to
elt
, or-1
if there is no such element. - See Also:
NavigableSet.higher(Object)
-
lowerIndex
@Pure public static <T> int lowerIndex(List<T> list, Comparator<? super T> comparator, T elt)
Returns the index of the greatest element in this list strictly lower than the given element, or-1
if there is no such element.This function assumes that the given list is sorted according to the given comparator. A dichotomic algorithm is used.
- Type Parameters:
T
- is the type of the data to search for.- Parameters:
list
- is the list inside which the element should be searched.comparator
- is the comparator used to sort the list.elt
- is the value to match.- Returns:
- the index of greater element strictly lower than to
elt
, or-1
if there is no such element. - See Also:
NavigableSet.lower(Object)
-
reverseIterator
public static <T> Iterator<T> reverseIterator(List<T> list)
Replies an iterator that goes from end to start of the given list.The replied iterator dos not support removals.
- Type Parameters:
T
- the type of the list elements.- Parameters:
list
- the list.- Returns:
- the reverse iterator.
- Since:
- 14.0
-
-