Package com.pnfsoftware.jeb.util.collect
Class CollectionUtil
java.lang.Object
com.pnfsoftware.jeb.util.collect.CollectionUtil
Convenience methods for Java
collections, in particular, lists.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Collection<T> addNonNulls(Collection<T> dst, Collection<? extends T> src) Add all non-null source elements to a destination collection.static <T> Collection<T> addNonNulls(Collection<T> dst, T... elts) Add all non-null elements to a destination collection.static <T,K extends Comparable<K>>
TbinarySearch(List<T> list, K key, IExtractor<T, K> extractor) Perform a binary search in an ordered list.static <T,K extends Comparable<K>>
intbinarySearchEx(List<? extends T> list, K key, IExtractor<T, ? extends K> extractor, Comparator<K> cc) Perform a binary search in an ordered list.static <T> booleancompare(Collection<? extends T> c1, Collection<? extends T> c2, boolean sorted) Compare two collections using element equality.static <T> booleancompareByReference(Collection<? extends T> c1, Collection<? extends T> c2, boolean sorted) Compare two collections using reference identity for elements.static booleancontains(Collection<?> c, Object v) Safely check if an object is present within a collection.static booleancontainsNonNull(Iterable<?> c) Determine if the given iterable contains one or more non-null elements.static booleancontainsNull(Iterable<?> c) Determine if the given iterable contains one or more null elements.static <T> booleancontainsReference(Collection<? extends T> c, T o) Determine if the collection contains the object reference (differs fromCollection.contains(Object)by checking reference equality==instead ofObject.equals(Object)equality)static <T> intcount(Collection<T> coll, T item) Count elements usingObject.equals(Object).static <T> Iterable<T> doubleCollectionIterable(Collection<T> coll1, Collection<T> coll2) Create a generator that will iterate over two collections, starting with the first one, then the second one.static <T> Tfirst(Collection<T> coll) Retrieve the first item of aCollection.static <T> booleanhasIntersection(Collection<? extends T> c1, Collection<? extends T> c2) Determine whether two collections share at least one element.static <T> intidentityCount(Collection<T> coll, T item) Count elements using==.static <T> Set<T> intersect(Collection<? extends T> c1, Collection<? extends T> c2) Return the intersection of 2 collections (order of collection parameters does not matter).static <T> Tlast(Collection<T> coll) Retrieve the last item of aCollection.
-
Method Details
-
containsNull
Determine if the given iterable contains one or more null elements.- Parameters:
c- iterable to inspect- Returns:
- true if at least one element is null
-
containsNonNull
Determine if the given iterable contains one or more non-null elements.- Parameters:
c- iterable to inspect- Returns:
- true if at least one element is not null
-
containsReference
Determine if the collection contains the object reference (differs fromCollection.contains(Object)by checking reference equality==instead ofObject.equals(Object)equality)- Type Parameters:
T- element type- Parameters:
c- collection to loop intoo- element whose presence in this collection is to be tested- Returns:
- true if the collection contains the specified element
-
contains
Safely check if an object is present within a collection.- Parameters:
c- collection, potentially nullv- object, potentially null- Returns:
- true IFF the object is contained in the collection
-
intersect
Return the intersection of 2 collections (order of collection parameters does not matter).- Type Parameters:
T- element type- Parameters:
c1- first collectionc2- second collection- Returns:
- a set of intersection
-
hasIntersection
Determine whether two collections share at least one element.- Type Parameters:
T- element type- Parameters:
c1- first collectionc2- second collection- Returns:
- true if both collections contain at least one equal element
-
binarySearch
public static <T,K extends Comparable<K>> T binarySearch(List<T> list, K key, IExtractor<T, K> extractor) Perform a binary search in an ordered list.- Type Parameters:
T- element typeK- key type- Parameters:
list- a list of ordered itemskey- the ordering key for the items.extractor- key extractor- Returns:
- the element, null if not found
-
binarySearchEx
public static <T,K extends Comparable<K>> int binarySearchEx(List<? extends T> list, K key, IExtractor<T, ? extends K> extractor, Comparator<K> cc) Perform a binary search in an ordered list.- Type Parameters:
T- element typeK- key type- Parameters:
list- a list of ordered itemskey- the ordering key for the items.extractor- key extractorcc- optional custom comparator for the keys- Returns:
- the zero+ index of the found element; else, the negative insertion index of the ghost element
-
compareByReference
public static <T> boolean compareByReference(Collection<? extends T> c1, Collection<? extends T> c2, boolean sorted) Compare two collections using reference identity for elements.- Type Parameters:
T- element type- Parameters:
c1- first collectionc2- second collectionsorted- true if iteration order must be considered- Returns:
- true if the collections contain the same references
-
compare
public static <T> boolean compare(Collection<? extends T> c1, Collection<? extends T> c2, boolean sorted) Compare two collections using element equality.- Type Parameters:
T- element type- Parameters:
c1- first collectionc2- second collectionsorted- true if iteration order must be considered- Returns:
- true if the collections contain the same elements
-
count
Count elements usingObject.equals(Object).- Type Parameters:
T- element type- Parameters:
coll- collection to inspectitem- item to count- Returns:
- number of equal elements
-
identityCount
Count elements using==.- Type Parameters:
T- element type- Parameters:
coll- collection to inspectitem- item to count- Returns:
- number of identical elements
-
addNonNulls
Add all non-null source elements to a destination collection.- Type Parameters:
T- element type- Parameters:
dst- destination collectionsrc- optional source collection- Returns:
- the destination collection
-
addNonNulls
Add all non-null elements to a destination collection.- Type Parameters:
T- element type- Parameters:
dst- destination collectionelts- optional elements- Returns:
- the destination collection
-
doubleCollectionIterable
Create a generator that will iterate over two collections, starting with the first one, then the second one.This method is convenient to iterate over two collections without having to create an intermediate list holding the elements.
- Type Parameters:
T- element type- Parameters:
coll1- first collectioncoll2- second collection- Returns:
- an iterable
-
first
Retrieve the first item of aCollection. This method is safe and returns null if the collection is null or empty.- Type Parameters:
T- element type- Parameters:
coll- a collection- Returns:
- the first item or null if collection is null or empty
-
last
Retrieve the last item of aCollection. This method is safe and returns null if the collection is null or empty.- Type Parameters:
T- element type- Parameters:
coll- a collection- Returns:
- the last item or null if collection is null or empty
-