Package io.pravega.common.util
Class CollectionHelpers
- java.lang.Object
-
- io.pravega.common.util.CollectionHelpers
-
public final class CollectionHelpers extends java.lang.ObjectHelper methods for collections.
-
-
Constructor Summary
Constructors Constructor Description CollectionHelpers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> intbinarySearch(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 <T> intfindGreatestLowerBound(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 to find the greatest lower bound for the supplied element.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)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>
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)Returns an unmodifiable Set View made up of the given Sets while translating the items into a common type.
-
-
-
Method Detail
-
binarySearch
public 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. This method has undefined behavior if the list is not sorted.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.
- Type Parameters:
T- Type of the elements in the list.- Parameters:
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.- Returns:
- The index of the sought item, or -1 if not found.
-
findGreatestLowerBound
public static <T> int findGreatestLowerBound(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 to find the greatest lower bound for the supplied element.- Type Parameters:
T- Type of elements in the list- Parameters:
list- list to search incomparator- A bifunction comparator that compares elements in list of type T to value of type U- Returns:
- returns index of element in the list is greatest lower bound for the given value. If value is not found, this returns -1
-
filterOut
public 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.- Type Parameters:
T- Type of elements.- Parameters:
collection- The collection to check.toExclude- The elements to exclude.- Returns:
- A new collection containing all items in collection, except those in toExclude.
-
joinSets
public 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. The returned Set View does not copy any of the data from any of the given Sets, therefore any changes in the two Sets will be reflected in the View. NOTE: The iterator of this SetView will not de-duplicate items, so if the two Sets are not disjoint, the same item may appear multiple times.- Type Parameters:
T- The type of the items in the Sets.- Parameters:
set1- The first Set.set2- The second Set.- Returns:
- A new Set View made up of the two Sets.
-
joinSets
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)Returns an unmodifiable Set View made up of the given Sets while translating the items into a common type. The returned Set View does not copy any of the data from any of the given Sets, therefore any changes in the two Sets will be reflected in the View. NOTE: The iterator of this SetView will not de-duplicate items, so if the two Sets are not disjoint, of if the converter functions yield the same value for different inputs, the same item may appear multiple times.- Type Parameters:
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.- Parameters:
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.- Returns:
- A new Set View made up of the two Collections, with translation applied.
-
joinCollections
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)Returns an unmodifiable Collection View made up of the given Collections while translating the items into a common type. The returned Collection View does not copy any of the data from any of the given Collections, therefore any changes in the two Collections will be reflected in the View.- Type Parameters:
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.- Parameters:
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.- Returns:
- A new Collection View made up of the two Collections, with translation applied.
-
-