Package org.djutils.immutablecollections
Class ImmutableCollections
java.lang.Object
org.djutils.immutablecollections.ImmutableCollections
public class ImmutableCollections extends Object
Static methods operating on immutable collections, or a mix of an immutable collection and a mutable collection.
Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
- Version:
- $Revision$, $LastChangedDate$, by $Author$, initial version Feb 26, 2019
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Method Summary
Modifier and Type Method Description static <T> intbinarySearch(ImmutableList<? extends Comparable<? super T>> il, T key)Search the immutable list for the specified object using the binary search algorithm.static <T> intbinarySearch(ImmutableList<? extends T> il, T key, Comparator<? super T> comparator)Search the immutable list for the specified object using the binary search algorithm.static booleandisjoint(Collection<?> c1, ImmutableCollection<?> ic2)Determine if an immutable collection and a (non immutable) collection have no common membersstatic booleandisjoint(ImmutableCollection<?> ic1, Collection<?> c2)Determine if an immutable collection and a (non immutable) collection have no common membersstatic booleandisjoint(ImmutableCollection<?> ic1, ImmutableCollection<?> ic2)Determine if two immutable collections have no common members.static <T> ImmutableList<T>emptyImmutableList()Return an immutable empty list.static <T, V> ImmutableMap<T,V>emptyImmutableMap()Return an immutable empty map.static <T> ImmutableSet<T>emptyImmutableSet()Return an immutable empty set.static intfrequency(ImmutableCollection<?> ic, Object o)Return the number of occurrences of an object in an immutable collection.static intindexOfSubList(List<?> source, ImmutableList<?> target)Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.static intindexOfSubList(ImmutableList<?> source, List<?> target)Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.static intindexOfSubList(ImmutableList<?> source, ImmutableList<?> target)Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.static intlastIndexOfSubList(List<?> source, ImmutableList<?> target)Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.static intlastIndexOfSubList(ImmutableList<?> source, List<?> target)Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.static intlastIndexOfSubList(ImmutableList<?> source, ImmutableList<?> target)Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.static <T extends Object & Comparable<? super T>>
Tmax(ImmutableCollection<? extends T> ic)Returns the maximum element of an immutable collection according to the natural ordering of its elements.static <T> Tmax(ImmutableCollection<? extends T> ic, Comparator<? super T> comparator)Returns the maximum element of an immutable collection according to the natural ordering of its elements.static <T extends Object & Comparable<? super T>>
Tmin(ImmutableCollection<? extends T> ic)Returns the minimum element of an immutable collection according to the natural ordering of its elements.static <T> Tmin(ImmutableCollection<? extends T> ic, Comparator<? super T> comparator)Returns the minimum element of an immutable collection according to the natural ordering of its elements.
-
Method Details
-
emptyImmutableSet
Return an immutable empty set.- Type Parameters:
T- the data type of the ImmutableSet- Returns:
- ImmutableSet<T>; an immutable empty set
-
emptyImmutableMap
Return an immutable empty map.- Type Parameters:
T- the key type of the ImmutableMapV- the value type of the ImmutableMap- Returns:
- ImmutableMap<T, V>; an immutable empty map
-
emptyImmutableList
Return an immutable empty list.- Type Parameters:
T- the data type of the ImmutableList- Returns:
- ImmutableList<T>; an immutable empty list
-
binarySearch
Search the immutable list for the specified object using the binary search algorithm. The list must be ordered according to the natural ordering of its elements.- Type Parameters:
T- the data type of the ImmutableList- Parameters:
il- ImmutableList<?>; the list (must be ordered according to the natural ordering ofTkey- T; the element to search for.- Returns:
- int; if
keyis present in the list, the index ofkey, or, whenkeyis not present in the list(-(insertion point) - 1)whereinsertion pointis the index ofkeyif it were contained in the list.
-
binarySearch
public static <T> int binarySearch(ImmutableList<? extends T> il, T key, Comparator<? super T> comparator)Search the immutable list for the specified object using the binary search algorithm. The list must be ordered according to the comparator.- Type Parameters:
T- the object type in the list- Parameters:
il- ImmutableList<?>; the list (must be ordered according to thecomparatorkey- T; the element to search for.comparator- Comparator<? super T>; a comparator forT- Returns:
- int; if
keyis present in the list, the index ofkey, or, whenkeyis not present in the list(-(insertion point) - 1)whereinsertion pointis the index ofkeyif it were contained in the list.
-
disjoint
Determine if two immutable collections have no common members.- Parameters:
ic1- ImmutableCollection<?> one immutable collectionic2- ImmutableCollection<?> another immutable collection- Returns:
- boolean; true if the collections have no common members; false if the collections have at least one member in common
-
disjoint
Determine if an immutable collection and a (non immutable) collection have no common members- Parameters:
ic1- ImmutableCollection<?> an immutable collectionc2- Collection<?> a (mutable) collection- Returns:
- boolean; true if the collections have no common members; false if the collections have at least one member in common
-
disjoint
Determine if an immutable collection and a (non immutable) collection have no common members- Parameters:
c1- Collection<?> a (mutable) collectionic2- ImmutableCollection<?> an immutable collection- Returns:
- boolean; true if the collections have no common members; false if the collections have at least one member in common
-
indexOfSubList
Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.- Parameters:
source- ImmutableList<?>; the list in which to find the first occurrence oftargettarget- ImmutableList<?>; the pattern to find insource- Returns:
- int; the index in
sourceof the first occurrence oftargetor -1 iftargetdoes not occur anywhere insource
-
indexOfSubList
Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.- Parameters:
source- ImmutableList<?>; the list in which to find the first occurrence oftargettarget- List<?>; the pattern to find insource- Returns:
- int; the index in
sourceof the first occurrence oftargetor -1 iftargetdoes not occur anywhere insource
-
indexOfSubList
Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.- Parameters:
source- List<?>; the list in which to find the first occurrence oftargettarget- ImmutableList<?>; the pattern to find insource- Returns:
- int; the index in
sourceof the first occurrence oftargetor -1 iftargetdoes not occur anywhere insource
-
lastIndexOfSubList
Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.- Parameters:
source- ImmutableList<?>; the list in which to find the last occurrence oftargettarget- ImmutableList<?>; the pattern to find insource- Returns:
- int; the index in
sourceof the last occurrence oftargetor -1 iftargetdoes not occur anywhere insource
-
lastIndexOfSubList
Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.- Parameters:
source- ImmutableList<?>; the list in which to find the last occurrence oftargettarget- List<?>; the pattern to find insource- Returns:
- int; the index in
sourceof the last occurrence oftargetor -1 iftargetdoes not occur anywhere insource
-
lastIndexOfSubList
Returns the starting position of the last occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence.- Parameters:
source- List<?>; the list in which to find the last occurrence oftargettarget- ImmutableList<?>; the pattern to find insource- Returns:
- int; the index in
sourceof the last occurrence oftargetor -1 iftargetdoes not occur anywhere insource
-
max
public static <T extends Object & Comparable<? super T>> T max(ImmutableCollection<? extends T> ic)Returns the maximum element of an immutable collection according to the natural ordering of its elements.- Type Parameters:
T- the object type in the collection- Parameters:
ic- ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collection- Returns:
- T <T extends Object & Comparable<? super T>>; the maximum element in the immutable collection
- Throws:
NoSuchElementException- if the immutable collection is empty.
-
max
Returns the maximum element of an immutable collection according to the natural ordering of its elements.- Type Parameters:
T- the object type in the collection- Parameters:
ic- ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collectioncomparator- Comparator<? super T>; a comparator forT- Returns:
- T <T extends Object & Comparable<? super T>>; the maximum element in the immutable collection
- Throws:
NoSuchElementException- if the immutable collection is empty.
-
min
public static <T extends Object & Comparable<? super T>> T min(ImmutableCollection<? extends T> ic)Returns the minimum element of an immutable collection according to the natural ordering of its elements.- Type Parameters:
T- the object type in the collection- Parameters:
ic- ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collection- Returns:
- T <T extends Object & Comparable<? super T>>; the minimum element in the immutable collection
- Throws:
NoSuchElementException- if the immutable collection is empty.
-
min
Returns the minimum element of an immutable collection according to the natural ordering of its elements.- Type Parameters:
T- the object type in the collection- Parameters:
ic- ImmutableCollection<T extends Object & Comparable<? super T>>; the immutable collectioncomparator- Comparator<? super T>; a comparator forT- Returns:
- T <T extends Object & Comparable<? super T>>; the minimum element in the immutable collection
- Throws:
NoSuchElementException- if the immutable collection is empty.
-
frequency
Return the number of occurrences of an object in an immutable collection.- Parameters:
ic- ImmutableCollection; the immutable collectiono- Object; the object to count the number occurrences of- Returns:
- int; the number of occurrences of
oinic
-