public final class CollectionHelpers
extends java.lang.Object
| Constructor and Description |
|---|
CollectionHelpers() |
| Modifier and Type | Method and Description |
|---|---|
static <KeyType extends java.lang.Comparable<KeyType>,ValueType> |
binarySearch(IndexedMap<KeyType,ValueType> source,
java.util.Collection<KeyType> keys,
java.util.Map<KeyType,ValueType> result)
Performs a binary search on the given IndexedMap.
|
static <T> int |
binarySearch(java.util.List<? extends T> list,
java.util.function.Function<? super T,java.lang.Integer> comparator)
Performs a binary search on the given sorted list using the given comparator.
|
static <T> java.util.Collection<T> |
filterOut(java.util.Collection<T> collection,
java.util.Collection<T> toExclude)
Returns a new collection which contains all the items in the given collection that are not to be excluded.
|
static <OutputType,Type1,Type2> |
joinCollections(java.util.Collection<Type1> c1,
java.util.function.Function<Type1,OutputType> converter1,
java.util.Collection<Type2> c2,
java.util.function.Function<Type2,OutputType> converter2)
Returns an unmodifiable Collection View made up of the given Collections while translating the items into a common type.
|
static <T> java.util.Set<T> |
joinSets(java.util.Set<T> set1,
java.util.Set<T> set2)
Returns an unmodifiable Set View made up of the given Sets.
|
static <OutputType,Type1,Type2> |
joinSets(java.util.Set<Type1> set1,
java.util.function.Function<Type1,OutputType> converter1,
java.util.Set<Type2> set2,
java.util.function.Function<Type2,OutputType> converter2)
Returns an unmodifiable Set View made up of the given Sets while translating the items into a common type.
|
public static <T> int binarySearch(java.util.List<? extends T> list,
java.util.function.Function<? super T,java.lang.Integer> comparator)
This method is different than that in java.util.Collections in the following ways: 1. This one searches by a simple comparator, vs the ones in the Collections class which search for a specific element. This one is useful if we don't have an instance of a search object or we want to implement a fuzzy comparison. 2. This one returns -1 if the element is not found. The ones in the Collections class return (-(start+1)), which is the index where the item should be inserted if it were to go in the list.
T - Type of the elements in the list.list - The list to search on.comparator - The comparator to use for comparison. Returns -1 if sought item is before the current item,
+1 if it is after or 0 if an exact match.public static <KeyType extends java.lang.Comparable<KeyType>,ValueType> boolean binarySearch(IndexedMap<KeyType,ValueType> source, java.util.Collection<KeyType> keys, java.util.Map<KeyType,ValueType> result)
KeyType - Type of the Keys.ValueType - Type of the values.source - The IndexedMap to search on. The Entries in this map must be sorted by Key, otherwise the outcome
of this method will be undefined.keys - A Collection containing Keys to search for. This collection need not be sorted.result - A Map that where results will be populated into. Only Keys that are present in the given source
will be added here, and only if they are found during the search (if they're already present in
the given Map, they will be overridden with the new values found here).public static <T> java.util.Collection<T> filterOut(java.util.Collection<T> collection,
java.util.Collection<T> toExclude)
T - Type of elements.collection - The collection to check.toExclude - The elements to exclude.public static <T> java.util.Set<T> joinSets(java.util.Set<T> set1,
java.util.Set<T> set2)
T - The type of the items in the Sets.set1 - The first Set.set2 - The second Set.public static <OutputType,Type1,Type2> java.util.Set<OutputType> joinSets(java.util.Set<Type1> set1,
java.util.function.Function<Type1,OutputType> converter1,
java.util.Set<Type2> set2,
java.util.function.Function<Type2,OutputType> converter2)
OutputType - The type of the items in the returned Set View.Type1 - The type of the items in Set 1.Type2 - The type of the items in Set 2.set1 - The first Set, which contains items of type Type1.converter1 - A Function that translates from Type1 to OutputType.set2 - The second Set, which contains items of type Type2.converter2 - A Function that translates from Type2 to OutputType.public static <OutputType,Type1,Type2> java.util.Collection<OutputType> joinCollections(java.util.Collection<Type1> c1,
java.util.function.Function<Type1,OutputType> converter1,
java.util.Collection<Type2> c2,
java.util.function.Function<Type2,OutputType> converter2)
OutputType - The type of the items in the returned Set View.Type1 - The type of the items in Set 1.Type2 - The type of the items in Set 2.c1 - The first Collection, which contains items of type Type1.converter1 - A Function that translates from Type1 to OutputType.c2 - The second Collection, which contains items of type Type2.converter2 - A Function that translates from Type2 to OutputType.