Class Collection<T>

java.lang.Object
me.sbasalaev.collection.Collection<T>
All Implemented Interfaces:
Cloneable, Iterable<T>, Traversable<T>
Direct Known Subclasses:
List, Opt, Set

public abstract class Collection<@Out T> extends Object implements Traversable<T>, Cloneable
Collection of elements. This is a base class for different types of collections such as lists or sets. Collections have known size at any time. Every collection has a notion of equality though that notion depends on the type of the collection. Collection may be cloned to obtain a shallow immutable copy of the collection of the same type.
  • Constructor Details

    • Collection

      public Collection()
      Constructor for subclasses.
  • Method Details

    • size

      public abstract int size()
      Number of elements in this collection. Unlike Traversable.count() this method is guaranteed to be fast.
    • count

      public int count()
      Number of elements in this collection. Returns the same value as size().
      Specified by:
      count in interface Traversable<T>
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Traversable
      Whether this collection has no elements.
      Specified by:
      isEmpty in interface Traversable<T>
    • nonEmpty

      public boolean nonEmpty()
      Description copied from interface: Traversable
      Whether this collection contains elements.
      Specified by:
      nonEmpty in interface Traversable<T>
    • mapped

      public abstract <R> Collection<R> mapped(Function<? super @Out T,? extends R> mapping)
      Returns collection of the same type with given mapping applied to all elements. The collection is immutable and is unaffected by changes to this collection.
    • filtered

      public abstract Collection<T> filtered(Predicate<? super @Out T> condition)
      Returns collection of the same type containing only elements matching given condition. The collection is immutable and is unaffected by changes to this collection.
    • toArray

      public Object[] toArray()
      Allocates and returns array with the elements of this collection.
    • toArray

      public T[] toArray(IntFunction<@Out T[]> arraySupplier)
      Allocates and returns array with the elements of this collection.
    • fillArray

      public void fillArray(@Nullable Object[] array, int fromIndex)
      Fills array with elements of this collection starting from given index in array.
      Throws:
      IndexOutOfBoundsException - if the collection does not fit into given array.
    • spliterator

      public abstract Spliterator<T> spliterator()
      Creates a Spliterator over elements of this collection. The spliterator reports Spliterator.NONNULL and Spliterator.SIZED.
      Specified by:
      spliterator in interface Iterable<T>
    • stream

      public Stream<T> stream()
      Returns a sequential Stream with this collection as its source.
      Since:
      4.1
    • clone

      public abstract Collection<T> clone()
      Returns a shallow copy of this collection. The returned collection is immutable, has the same type and contains the same elements. If the collection is ordered, the elements in a copy are in the same order. The collection is allowed to return itself if it is immutable.
      Overrides:
      clone in class Object
    • toList

      public List<T> toList()
      Description copied from interface: Traversable
      Collects elements of this traversable into an immutable list.
      Specified by:
      toList in interface Traversable<T>
    • toSet

      public Set<T> toSet()
      Description copied from interface: Traversable
      Collects elements of this traversable into an immutable set.
      Specified by:
      toSet in interface Traversable<T>
    • equals

      public abstract boolean equals(@Nullable Object obj)
      Whether given object is equal to this collection. Different collections have different notions of equality.
      Overrides:
      equals in class Object
    • hashCode

      public abstract int hashCode()
      Hash code of the collection. Different collections have different ways to calculate the hash code.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      String representation of this collection. Returns string of the form
      {e1, e2, ..., eN}
      where elements are in the order returned by iterator.
      Overrides:
      toString in class Object