Abstract Collection in Java with Examples
public abstract class AbstractCollection<E> extends Object implements Collection<E> where E is the type of elements maintained by this collection.
Constructors in Java AbstractCollection:
- protected AbstractCollection(): The default constructor, but being protected, it doesn’t allow to create an AbstractCollection object.
Below is the sample program to illustrate AbstractCollection in Java:
filter_none
edit
play_arrow
brightness_4
|
Output:
AbstractCollection: [Welcome, To, Geeks, 4, Geeks]
Methods in Java Abstract Collection:
- add(E e): This method ensures that this collection contains the specified element (optional operation).
- addAll(Collection c): This method Adds all of the elements in the specified collection to this collection (optional operation).
- clear(): This method removes all of the elements from this collection (optional operation).
- contains(Object o): This method returns true if this collection contains the specified element.
- contains(Collection c): This method returns true if this collection contains all of the elements in the specified collection.
- isEmpty(): This method returns true if this collection contains no elements.
- iterator(): This method returns an iterator over the elements contained in this collection.
- remove(Object o): This method removes a single instance of the specified element from this collection if it is present (optional operation).
- size(): This method returns the number of elements in this collection.
- toArray(): This method returns an array containing all of the elements in this collection.
- toArray(T[] a): This method returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
- toString?(): This method returns a string representation of this collection.
Example:
filter_none
edit
play_arrow
brightness_4
|
Output:
AbstractCollection 1: [4, Geeks, To, TreeSet, Welcome] AbstractCollection 2: [] AbstractCollection 2: [4, Geeks, To, TreeSet, Welcome] Is the collection empty? true
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html
post a comment