Differences between Collection and Collections in Java?

JavaObject Oriented Programming

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
raja
Published on 28-Aug-2019 12:43:07
Advertisements