Skip to main content

java.util.Collection

The java.util.Collection interface is a fundamental part of the Java Collections Framework. It defines the common behavior that all collection classes should have, such as adding, removing, and accessing elements.

The Collection interface extends the java.lang.Iterable interface, which means that all collection classes can be iterated over using a for-each loop or an iterator. The methods of the Collection interface include:

  • size(): Returns the number of elements in the collection.
  • isEmpty(): Returns true if the collection is empty.
  • contains(Object o): Returns true if the collection contains the specified element.
  • add(E e): Adds the specified element to the collection.
  • remove(Object o): Removes the specified element from the collection.
  • addAll(Collection<? extends E> c): Adds all the elements in the specified collection to this collection.
  • removeAll(Collection<?> c): Removes all the elements in the specified collection from this collection.
  • clear(): Removes all elements from the collection.

The Collection interface also provides default implementations for the stream() and parallelStream() methods, allowing collections to be processed using Java 8 Stream API.

The Collection interface is implemented by several other collection classes in Java, including List, Set, and Queue, each with their own unique properties and behaviors.

Code Example

import java.util.ArrayList;
import java.util.Collection;

public class CollectionExample {
    public static void main(String[] args) {
        // create a new collection
        Collection<String> fruits = new ArrayList<>();

        // add elements to the collection
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("cherry");
        System.out.println("Fruits: " + fruits);

        // check if collection contains an element
        boolean containsBanana = fruits.contains("banana");
        System.out.println("Contains banana? " + containsBanana);

        // remove an element from the collection
        boolean removedCherry = fruits.remove("cherry");
        System.out.println("Removed cherry? " + removedCherry);
        System.out.println("Fruits: " + fruits);

        // get the size of the collection
        int size = fruits.size();
        System.out.println("Size: " + size);

        // clear the collection
        fruits.clear();
        System.out.println("Fruits: " + fruits);
    }
}

In this example, we create a new ArrayList to represent a collection of fruits. We add some fruits to the collection using the add() method, then check if it contains a specific fruit using the contains() method. We then remove an element using the remove() method and get the size of the collection using the size() method. Finally, we clear the collection using the clear() method.

Output

Fruits: [apple, banana, cherry]
Contains banana? true
Removed cherry? true
Fruits: [apple, banana]
Size: 2
Fruits: []




Comment

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