Sobes.tech
Junior — Middle

How does the Collection interface differ from the utility class Collections in Java?

sobes.tech AI

Answer from AI

In Java, Collection is an interface that defines basic operations for working with groups of objects, such as adding, removing, checking for the presence of elements, and iteration. It is the root of the collection hierarchy and is implemented by classes like ArrayList, HashSet, and others.

Collections is a utility class (a final class with a private constructor) that contains static methods for working with collections. For example, methods for sorting, searching, synchronization, creating unmodifiable collections, etc.

Example:

List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");

// Using the utility class Collections for sorting
Collections.sort(list);

In summary:

  • Collection — an interface describing the behavior of collections.
  • Collections — a class with useful static methods for working with collections.
How does the Collection interface differ from the… - sobes.tech