Class CollectionFilters


  • public class CollectionFilters
    extends java.lang.Object
    A tool box of methods to filter collections by class.
    Author:
    stm
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.Collection<T> checkedCast​(java.util.Collection oldCollection, java.lang.Class<T> type)
      Casts any collection to a generic collection.
      static <T> java.util.Collection<T> filter​(java.lang.Object[] oldCollection, java.lang.Class<T> type)
      Creates a new set, adds to it all elements of oldCollection which are of the specified type, and returns this set.
      static <T> java.util.Collection<T> filter​(java.util.Collection<?> oldCollection, java.lang.Class<T> type)
      Creates a new set, adds to it all elements of oldCollection which are of the specified type, and returns this set.
      static <T> void filter​(java.util.Collection<?> oldCollection, java.util.Collection<T> newCollection, java.lang.Class<T> type)
      Adds to newCollection all elements of oldCollection which are of the specified type.
      static <T> java.util.Collection<T> getCollection​(java.lang.Class<T> type, java.util.Collection<?> values)
      Returns an (unmodifiable) collection that contains all elements that are instances of class1 in values.
      static <T> java.util.Iterator<T> getIterator​(java.lang.Class<T> type, java.util.Collection<?> coll)
      Returns an iterator that iterates only over elements of coll of type class1.
      static <T> java.util.Iterator<T> getKeyIterator​(java.lang.Class<T> type, java.util.Map<?,​?> map)
      Returns an iterator that iterates only over keys of map of type class1.
      static <T> java.util.Iterator<T> getValueIterator​(java.lang.Class<T> type, java.util.Map<?,​?> map)
      Returns an iterator that iterates only over values of map of type class1.
      static <T> java.util.Collection<T> uncheckedCast​(java.util.Collection oldCollection, java.lang.Class<T> type)
      Casts any collection to a generic collection.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CollectionFilters

        public CollectionFilters()
    • Method Detail

      • uncheckedCast

        public static <T> java.util.Collection<T> uncheckedCast​(java.util.Collection oldCollection,
                                                                java.lang.Class<T> type)
        Casts any collection to a generic collection. USE WITH EXTREME CARE! Use it only if you are absolutely sure that the oldCollection contains only elements of Type T. The result is backed by oldCollection. This means that any changes to oldCollection affect the collection returned and vice versa.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        oldCollection -
        Returns:
        Effectively returns oldCollection
      • checkedCast

        public static <T> java.util.Collection<T> checkedCast​(java.util.Collection oldCollection,
                                                              java.lang.Class<T> type)
                                                       throws java.lang.ClassCastException
        Casts any collection to a generic collection. If not every element of oldCollection can be cast to type, an Exception is thrown. The result is backed by oldCollection. This means that any changes to oldCollection affect the collection returned and vice versa.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        oldCollection -
        type - The values are filtered by this type; effectively the same as T
        Returns:
        Effectively returns oldCollection
        Throws:
        java.lang.ClassCastException - if any of the elements of oldCollection is not an instance of type
      • filter

        public static <T> java.util.Collection<T> filter​(java.util.Collection<?> oldCollection,
                                                         java.lang.Class<T> type)
        Creates a new set, adds to it all elements of oldCollection which are of the specified type, and returns this set.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        oldCollection -
        type - The values are filtered by this type; effectively the same as T
        Returns:
        A collection with the objects of the desired type
      • filter

        public static <T> java.util.Collection<T> filter​(java.lang.Object[] oldCollection,
                                                         java.lang.Class<T> type)
        Creates a new set, adds to it all elements of oldCollection which are of the specified type, and returns this set.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        oldCollection -
        type - The values are filtered by this type; effectively the same as T
        Returns:
        A collection with the objects of the desired type
      • filter

        public static <T> void filter​(java.util.Collection<?> oldCollection,
                                      java.util.Collection<T> newCollection,
                                      java.lang.Class<T> type)
        Adds to newCollection all elements of oldCollection which are of the specified type.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        oldCollection -
        newCollection -
        type - The values are filtered by this type; effectively the same as T
      • getValueIterator

        public static <T> java.util.Iterator<T> getValueIterator​(java.lang.Class<T> type,
                                                                 java.util.Map<?,​?> map)
        Returns an iterator that iterates only over values of map of type class1.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        type - The values are filtered by this type; effectively the same as T
        map -
        Returns:
        an iterator that iterates only over values of map of type class1.
      • getKeyIterator

        public static <T> java.util.Iterator<T> getKeyIterator​(java.lang.Class<T> type,
                                                               java.util.Map<?,​?> map)
        Returns an iterator that iterates only over keys of map of type class1.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        type - The values are filtered by this type; effectively the same as T
        map -
        Returns:
        an iterator that iterates only over keys of map of type class1.
      • getIterator

        public static <T> java.util.Iterator<T> getIterator​(java.lang.Class<T> type,
                                                            java.util.Collection<?> coll)
        Returns an iterator that iterates only over elements of coll of type class1.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        type - The values are filtered by this type; effectively the same as T
        coll -
        Returns:
        an iterator that iterates only over entries of the collection of type class1.
      • getCollection

        public static <T> java.util.Collection<T> getCollection​(java.lang.Class<T> type,
                                                                java.util.Collection<?> values)
        Returns an (unmodifiable) collection that contains all elements that are instances of class1 in values.
        Type Parameters:
        T - The target collection's element type
        Parameters:
        type - The values are filtered by this type; effectively the same as T
        values -
        Returns:
        A collection of type T elements
        See Also:
        Class.isInstance(Object)