Differences between Collection and Collections in Java?


The Collection is an interface whereas Collections is a utility class in Java. The Set, List, and Queue are some of the subinterfaces of Collection interface, a Map interface is also part of the Collections Framework, but it doesn't inherit Collection interface.

The important methods of Collection interface are add(), remove(), size(), clear() etc and Collections class contains only static methods like sort(), min(), max(), fill(), copy(), reverse() etc.

Syntax for Collection Interface

public interface Collection<E> extends Iterable<E>

Syntax for Collections Class

public class Collections extends Object

Example

import java.util.*;
public class CollectionTest {
   public static void main(String args[]) {
      ArrayList<Integer> list = new ArrayList<Integer>();
      // Adding elements to the ArrayList
      list.add(5);
      list.add(20);
      list.add(35);
      list.add(50);
      list.add(65);
      // Collections.min() method to display minimum value
      System.out.println("Minimum value: " + Collections.min(list));
      // Collections.max() method to display maximum value
      System.out.println("Maximum value: " + Collections.max(list));
   }
}

Output

Minimum value: 5
Maximum value: 65

Updated on: 27-Nov-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements