AbstractCollection in java

java.util.AbstractCollection is a class in java programming language. Inside of this class exists 14 public methods. Declaration of this class as follows.

public abstract class java.util.AbstractCollection <E> 
implements java.util.Collection <E>

AbstractCollection public method

There are following useful methods which is define the inside of java.util.AbstractCollection class.

MethodDescription
boolean add(E e) It ensures that this collection contains the specified element (optional operation).
boolean addAll(Collection c) It adds all of the elements in the specified collection to this collection (optional operation).
void clear() It removes all of the elements from this collection (optional operation).
boolean contains(Object o) It returns true if this collection contains the specified element.
boolean containsAll(Collection c) It returns true if this collection contains all of the elements in the specified collection.
boolean isEmpty() It returns true if this collection contains no elements.
abstract Iterator iterator() It returns an iterator over the elements contained in this collection.
boolean remove(Object o) It removes a single instance of the specified element from this collection, if it is present (optional operation).
boolean removeAll(Collection c) It removes all of this collection's elements that are also contained in the specified collection (optional operation).
boolean retainAll(Collection c) It retains only the elements in this collection that are contained in the specified collection (optional operation).
abstract int size() It returns the number of elements in this collection.
Object[] toArray() It returns an array containing all of the elements in this collection.
 T[] toArray(T[] a) It returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
String toString() It returns a string representation of this collection.

This reference is belong to javadoc

This is an abstract class so it's not create any instance. But this class variable can hold the reference of subclass instance. Those class is AbstractList, ArrayDeque and ConcurrentLinkedDeque.

Example By using AbstractList

// Useful package
import java.util.AbstractCollection;
import java.util.ArrayList;
public class Example
{
	public static void main(String[] args)
	{
		// Empty collection
		AbstractCollection < Object > record = new ArrayList < Object > ();
		// Add element
		record.add("Is");
		record.add("Real");
		record.add(10);
		record.add("errors");
		// Display value
		System.out.println(record);
	}
}
AbstractCollection using ArrayList
[Is, Real, 10, errors]

Example By using ArrayDeque

// Useful package
import java.util.AbstractCollection;
import java.util.ArrayDeque;
public class Example
{
	public static void main(String[] args)
	{
		// Empty collection
		AbstractCollection < Object > record = new ArrayDeque < Object > ();
		// Add element
		record.add("Is");
		record.add("Real");
		record.add(10);
		record.add("errors");
		// Display value
		System.out.println(record);
	}
}
AbstractCollection using ArrayDeque
[Is, Real, 10, errors]

Example By using ConcurrentLinkedDeque

// Useful package
import java.util.AbstractCollection;
import java.util.concurrent.ConcurrentLinkedDeque;
public class Example
{
	public static void main(String[] args)
	{
		// Empty collection
		AbstractCollection < Object > record = 
          new ConcurrentLinkedDeque < Object > ();
		// Add element
		record.add("Is");
		record.add("Real");
		record.add(10);
		record.add("errors");
		// Display value
		System.out.println(record);
	}
}
AbstractCollection using ConcurrentLinkedDeque
[Is, Real, 10, errors]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved