com.alee.utils
Class CollectionUtils

java.lang.Object
  extended by com.alee.utils.CollectionUtils

public final class CollectionUtils
extends java.lang.Object

This class provides a set of utilities to work with collections.

Author:
Mikle Garin

Constructor Summary
CollectionUtils()
           
 
Method Summary
static
<T> boolean
addAll(java.util.Collection<T> collection, java.util.Collection<T> objects)
          Adds all objects into the specified list.
static
<T> boolean
addAll(java.util.Collection<T> collection, T... objects)
          Adds all objects into the specified list.
static
<T> boolean
addAllNonNull(java.util.Collection<T> collection, java.util.Collection<T> objects)
          Adds all non-null objects into the specified list.
static
<T> boolean
addAllNonNull(java.util.Collection<T> collection, T... objects)
          Adds all non-null objects into the specified list.
static boolean areEqual(java.util.List list1, java.util.List list2)
          Returns whether lists are equal or not.
static
<T> java.util.ArrayList<T>
asList(java.util.Iterator<T> data)
          Returns data converted into list.
static
<T> java.util.ArrayList<T>
asList(T... data)
          Returns data converted into list.
static
<T extends java.lang.Cloneable>
java.util.ArrayList<T>
clone(java.util.Collection<T> collection)
          Returns clone of the specified collection.
static
<T> java.util.ArrayList<T>
cloneOrCopy(java.util.Collection<T> collection)
          Returns collection with clonable values being cloned and non-clonable values simply copied from source collection.
static
<T extends java.lang.Cloneable>
java.util.ArrayList<T>
cloneSubList(java.util.List<T> list, int fromIndex, int toIndex)
          Returns sub list with cloned values.
static
<T> java.util.ArrayList<T>
copy(java.util.Collection<T> collection)
          Returns copy of the specified list.
static
<T> java.util.ArrayList<T>
copy(T... data)
          Returns data converted into list.
static
<T> java.util.ArrayList<T>
copySubList(java.util.List<T> list, int fromIndex, int toIndex)
          Returns sub list with copied values.
static
<T> java.util.List<T>
fillList(int amount, IndexedSupplier<T> supplier)
          Fills and returns list with data provided by supplier interface implementation.
static
<T> java.util.ArrayList<T>
filter(java.util.Collection<T> collection, Filter<T> filter)
          Returns list of elements filtered from collection.
static boolean isEmpty(java.util.Collection collection)
          Returns whether specified collection is empty or not.
static
<K,V> java.util.ArrayList<K>
keysList(java.util.Map<K,V> map)
          Returns map keys list.
static
<T> java.util.List<T>
limit(java.util.List<T> list, int limit)
          Returns list with limited amount of objects from the initial list.
static
<T> boolean
removeAll(java.util.Collection<T> collection, T... objects)
          Removes all objects from the specified list.
static
<T> java.util.List<T>
removeNulls(java.util.List<T> list)
          Removes all null elements from list.
static int[] toArray(java.util.List<java.lang.Integer> list)
          Returns an int array created using Integer list.
static
<T> java.util.ArrayList<T>
toList(java.util.Deque<T> deque)
          Returns a list of objects converted from deque.
static
<T> java.util.ArrayList<T>
toList(T[] array)
          Returns a list of objects converted from array.
static
<T> java.util.ArrayList<java.lang.String>
toStringList(java.util.List<T> list, TextProvider<T> textProvider)
          Returns list of strings extracted from the specified elements list.
static
<K,V> java.util.ArrayList<V>
valuesList(java.util.Map<K,V> map)
          Returns map values list.
static
<K,V> java.util.ArrayList<V>
valuesSummaryList(java.util.Map<K,java.util.List<V>> map)
          Returns map values summary list with unique elements only.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtils

public CollectionUtils()
Method Detail

isEmpty

public static boolean isEmpty(java.util.Collection collection)
Returns whether specified collection is empty or not.

Parameters:
collection - collection to process
Returns:
true if specified collection is empty, false otherwise

limit

public static <T> java.util.List<T> limit(java.util.List<T> list,
                                          int limit)
Returns list with limited amount of objects from the initial list. Only specified amount of first objects in initial list will be transferred to new list.

Type Parameters:
T - object type
Parameters:
list - initial list
limit - objects amount limitation
Returns:
list with limited amount of objects from the initial list

copySubList

public static <T> java.util.ArrayList<T> copySubList(java.util.List<T> list,
                                                     int fromIndex,
                                                     int toIndex)
Returns sub list with copied values.

Type Parameters:
T - data type
Parameters:
list - source list
fromIndex - start index
toIndex - end index
Returns:
sub list with copied values

cloneSubList

public static <T extends java.lang.Cloneable> java.util.ArrayList<T> cloneSubList(java.util.List<T> list,
                                                                                  int fromIndex,
                                                                                  int toIndex)
Returns sub list with cloned values.

Type Parameters:
T - data type
Parameters:
list - source list
fromIndex - start index
toIndex - end index
Returns:
sub list with cloned values

asList

public static <T> java.util.ArrayList<T> asList(T... data)
Returns data converted into list.

Type Parameters:
T - data type
Parameters:
data - data
Returns:
data list

asList

public static <T> java.util.ArrayList<T> asList(java.util.Iterator<T> data)
Returns data converted into list.

Type Parameters:
T - data type
Parameters:
data - data
Returns:
data list

addAll

public static <T> boolean addAll(java.util.Collection<T> collection,
                                 T... objects)
Adds all objects into the specified list.

Type Parameters:
T - objects type
Parameters:
collection - list to fill
objects - objects
Returns:
true if list changed as the result of this operation, false otherwise

addAllNonNull

public static <T> boolean addAllNonNull(java.util.Collection<T> collection,
                                        T... objects)
Adds all non-null objects into the specified list.

Type Parameters:
T - objects type
Parameters:
collection - list to fill
objects - objects
Returns:
true if list changed as the result of this operation, false otherwise

addAll

public static <T> boolean addAll(java.util.Collection<T> collection,
                                 java.util.Collection<T> objects)
Adds all objects into the specified list.

Type Parameters:
T - objects type
Parameters:
collection - list to fill
objects - objects
Returns:
true if list changed as the result of this operation, false otherwise

addAllNonNull

public static <T> boolean addAllNonNull(java.util.Collection<T> collection,
                                        java.util.Collection<T> objects)
Adds all non-null objects into the specified list.

Type Parameters:
T - objects type
Parameters:
collection - list to fill
objects - objects
Returns:
true if list changed as the result of this operation, false otherwise

removeAll

public static <T> boolean removeAll(java.util.Collection<T> collection,
                                    T... objects)
Removes all objects from the specified list.

Type Parameters:
T - objects type
Parameters:
collection - list to fill
objects - objects
Returns:
true if list changed as the result of this operation, false otherwise

copy

public static <T> java.util.ArrayList<T> copy(java.util.Collection<T> collection)
Returns copy of the specified list. Note that this method will copy same list values into the new list.

Type Parameters:
T - list type
Parameters:
collection - list to copy
Returns:
copy of the specified list

clone

public static <T extends java.lang.Cloneable> java.util.ArrayList<T> clone(java.util.Collection<T> collection)
Returns clone of the specified collection. Note that this method will clone all values into new list.

Type Parameters:
T - collection objects type
Parameters:
collection - collection to clone
Returns:
clone of the specified list

copy

public static <T> java.util.ArrayList<T> copy(T... data)
Returns data converted into list.

Type Parameters:
T - data type
Parameters:
data - data
Returns:
data list

cloneOrCopy

public static <T> java.util.ArrayList<T> cloneOrCopy(java.util.Collection<T> collection)
Returns collection with clonable values being cloned and non-clonable values simply copied from source collection.

Type Parameters:
T - collection objects type
Parameters:
collection - collection to perform action for
Returns:
collection with clonable values being cloned and non-clonable values simply copied from source collection

removeNulls

public static <T> java.util.List<T> removeNulls(java.util.List<T> list)
Removes all null elements from list.

Type Parameters:
T - list type
Parameters:
list - list to refactor
Returns:
refactored list

areEqual

public static boolean areEqual(java.util.List list1,
                               java.util.List list2)
Returns whether lists are equal or not.

Parameters:
list1 - first list
list2 - second list
Returns:
true if lists are equal, false otherwise

toStringList

public static <T> java.util.ArrayList<java.lang.String> toStringList(java.util.List<T> list,
                                                                     TextProvider<T> textProvider)
Returns list of strings extracted from the specified elements list.

Type Parameters:
T - elements type
Parameters:
list - elements list
textProvider - text provider
Returns:
list of strings extracted from the specified elements list

toArray

public static int[] toArray(java.util.List<java.lang.Integer> list)
Returns an int array created using Integer list.

Parameters:
list - Integer list
Returns:
int array

toList

public static <T> java.util.ArrayList<T> toList(T[] array)
Returns a list of objects converted from array.

Type Parameters:
T - data type
Parameters:
array - data array
Returns:
data list

toList

public static <T> java.util.ArrayList<T> toList(java.util.Deque<T> deque)
Returns a list of objects converted from deque.

Type Parameters:
T - data type
Parameters:
deque - data deque
Returns:
data list

filter

public static <T> java.util.ArrayList<T> filter(java.util.Collection<T> collection,
                                                Filter<T> filter)
Returns list of elements filtered from collection.

Type Parameters:
T - elements type
Parameters:
collection - collecton to filter
filter - filter to process
Returns:
list of elements filtered from collection

keysList

public static <K,V> java.util.ArrayList<K> keysList(java.util.Map<K,V> map)
Returns map keys list.

Type Parameters:
K - key object type
V - value object type
Parameters:
map - map to process
Returns:
map keys list

valuesList

public static <K,V> java.util.ArrayList<V> valuesList(java.util.Map<K,V> map)
Returns map values list.

Type Parameters:
K - key object type
V - value object type
Parameters:
map - map to process
Returns:
map values list

valuesSummaryList

public static <K,V> java.util.ArrayList<V> valuesSummaryList(java.util.Map<K,java.util.List<V>> map)
Returns map values summary list with unique elements only.

Type Parameters:
K - key object type
V - value object type
Parameters:
map - map to process
Returns:
map values summary list with unique elements only

fillList

public static <T> java.util.List<T> fillList(int amount,
                                             IndexedSupplier<T> supplier)
Fills and returns list with data provided by supplier interface implementation.

Type Parameters:
T - data type
Parameters:
amount - amount of list elements
supplier - data provider
Returns:
list filled with data provided by supplier interface implementation