Package org.djutils.immutablecollections
Class ImmutableCollections
java.lang.Object
org.djutils.immutablecollections.ImmutableCollections
Static methods operating on immutable collections, or a mix of an immutable collection and a mutable collection.
Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.
- Author:
- Alexander Verbraeck, Peter Knoppers
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> int
binarySearch
(ImmutableList<? extends Comparable<? super T>> il, T key) Search the immutable list for the specified object using the binary search algorithm.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.static boolean
disjoint
(Collection<?> c1, ImmutableCollection<?> ic2) Determine if an immutable collection and a (non immutable) collection have no common members.static boolean
disjoint
(ImmutableCollection<?> ic1, Collection<?> c2) Determine if an immutable collection and a (non immutable) collection have no common members.static boolean
disjoint
(ImmutableCollection<?> ic1, ImmutableCollection<?> ic2) Determine if two immutable collections have no common members.static <T> ImmutableList<T>
Return an immutable empty list.static <T,
V> ImmutableMap<T, V> Return an immutable empty map.static <T> ImmutableSet<T>
Return an immutable empty set.static int
frequency
(ImmutableCollection<?> ic, Object o) Return the number of occurrences of an object in an immutable collection.static int
indexOfSubList
(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 int
indexOfSubList
(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 int
indexOfSubList
(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 int
lastIndexOfSubList
(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 int
lastIndexOfSubList
(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 int
lastIndexOfSubList
(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> T
max
(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> T
min
(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<? extends Comparable<? super T>>; the list (must be ordered according to the natural ordering of <code>T</code>key
- T; the element to search for.- Returns:
- int; if
key
is present in the list, the index ofkey
, or, whenkey
is not present in the list(-(insertion point) - 1)
whereinsertion point
is the index ofkey
if 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<? extends T>; the list (must be ordered according to the <code>comparator</code>key
- T; the element to search for.comparator
- Comparator<? super T>; a comparator forT
- Returns:
- int; if
key
is present in the list, the index ofkey
, or, whenkey
is not present in the list(-(insertion point) - 1)
whereinsertion point
is the index ofkey
if it were contained in the list.
-
disjoint
Determine if two immutable collections have no common members.- Parameters:
ic1
- ImmutableCollection<?>; ImmutableCollection<?> one immutable collectionic2
- ImmutableCollection<?>; 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<?>; ImmutableCollection<?> an immutable collectionc2
- Collection<?>; 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<?>; Collection<?> a (mutable) collectionic2
- ImmutableCollection<?>; 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 oftarget
target
- ImmutableList<?>; the pattern to find insource
- Returns:
- int; the index in
source
of the first occurrence oftarget
or -1 iftarget
does 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 oftarget
target
- List<?>; the pattern to find insource
- Returns:
- int; the index in
source
of the first occurrence oftarget
or -1 iftarget
does 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 oftarget
target
- ImmutableList<?>; the pattern to find insource
- Returns:
- int; the index in
source
of the first occurrence oftarget
or -1 iftarget
does 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 oftarget
target
- ImmutableList<?>; the pattern to find insource
- Returns:
- int; the index in
source
of the last occurrence oftarget
or -1 iftarget
does 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 oftarget
target
- List<?>; the pattern to find insource
- Returns:
- int; the index in
source
of the last occurrence oftarget
or -1 iftarget
does 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 oftarget
target
- ImmutableList<?>; the pattern to find insource
- Returns:
- int; the index in
source
of the last occurrence oftarget
or -1 iftarget
does not occur anywhere insource
-
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 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
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
o
inic
-