Sobes.tech
Junior — Middle

What is the main interface implemented by the Collection class in Java?

sobes.tech AI

Answer from AI

In Java, the Collection class is an interface that defines the core methods for working with groups of objects. It implements the Iterable interface, which allows using a for-each loop to iterate over elements.

The main methods of the Collection interface include:

  • add(E e) — add an element to the collection
  • remove(Object o) — remove an element
  • size() — get the number of elements
  • isEmpty() — check if the collection is empty
  • contains(Object o) — check for the presence of an element
  • iterator() — get an iterator to traverse the elements
  • clear() — clear the collection

Thus, Collection serves as the base interface for all collections in Java, such as List, Set, and Queue, providing a unified contract for working with groups of objects.

What is the main interface implemented by the… - sobes.tech