Class SubclassFilteredCollection<S,​T extends S>

  • Type Parameters:
    S - element type of the underlying collection
    T - element type of filtered collection (and subclass of S). The predicate must accept only objects of type T.
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>

    public class SubclassFilteredCollection<S,​T extends S>
    extends java.util.AbstractCollection<T>
    Filtered view of a collection. (read-only collection, but elements can be changed, of course) Lets you iterate through those elements of a given collection that satisfy a certain condition (imposed by a predicate).

    The behaviour of this class is undefined if the underlying collection is changed.

    Since:
    3147
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Collection<? extends S> collection  
      private java.util.function.Predicate<? super S> predicate  
      private int size  
    • Constructor Summary

      Constructors 
      Constructor Description
      SubclassFilteredCollection​(java.util.Collection<? extends S> collection, java.util.function.Predicate<? super S> predicate)
      Constructs a new SubclassFilteredCollection.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> SubclassFilteredCollection<T,​T> filter​(java.util.Collection<? extends T> collection, java.util.function.Predicate<T> predicate)
      Create a new filtered collection without any constraints on the predicate type.
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      int size()  
      java.util.Spliterator<T> spliterator()  
      • Methods inherited from class java.util.AbstractCollection

        add, addAll, clear, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        equals, hashCode, parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • collection

        private final java.util.Collection<? extends S> collection
      • predicate

        private final java.util.function.Predicate<? super S> predicate
      • size

        private int size
    • Constructor Detail

      • SubclassFilteredCollection

        public SubclassFilteredCollection​(java.util.Collection<? extends S> collection,
                                          java.util.function.Predicate<? super S> predicate)
        Constructs a new SubclassFilteredCollection.
        Parameters:
        collection - The base collection to filter
        predicate - The predicate to use as filter
        See Also:
        for an alternative way to construct this.
    • Method Detail

      • iterator

        public java.util.Iterator<Titerator()
        Specified by:
        iterator in interface java.util.Collection<S>
        Specified by:
        iterator in interface java.lang.Iterable<S>
        Specified by:
        iterator in class java.util.AbstractCollection<T extends S>
      • spliterator

        public java.util.Spliterator<Tspliterator()
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<S>
        Specified by:
        size in class java.util.AbstractCollection<T extends S>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<S>
        Overrides:
        isEmpty in class java.util.AbstractCollection<T extends S>
      • filter

        public static <T> SubclassFilteredCollection<T,​T> filter​(java.util.Collection<? extends T> collection,
                                                                       java.util.function.Predicate<T> predicate)
        Create a new filtered collection without any constraints on the predicate type.
        Type Parameters:
        T - The collection type.
        Parameters:
        collection - The collection to filter.
        predicate - The predicate to filter for.
        Returns:
        The filtered collection. It is a Collection<T>.