Home >Java >javaTutorial >What is the difference between Collection and Collections in Java?

What is the difference between Collection and Collections in Java?

WBOY
WBOYforward
2023-09-01 21:57:051077browse

What is the difference between Collection and Collections in Java?

Collection is an interface and Collections is a utility class in Java. Set, List, and Queue are some sub-interfaces of the Collection interface, and the Map interface is also part of the Collections framework part, but it does not inherit the Collection interface. The important methods of the Collection interface are add(), remove(), size(), clear(), etc., and the Collections class only contains static Methods, such as sort(), min(), max(), fill(), copy(), reverse(), etc.

Syntax of collection interface

public interface Collection<E> extends Iterable<E>

Syntax of collection class

public class Collections extends Object

Example

import java.util.*;
public class CollectionTest {
   public static void main(String args[]) {
     <strong> </strong>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);
<strong>      </strong>// Collections.min() method to display minimum value<strong>
</strong>      System.out.println("Minimum value: " + Collections.min(list));
<strong>      </strong>// Collections.max() method to display maximum value<strong>
</strong>      System.out.println("Maximum value: " + Collections.max(list));
   }
}

Output

Minimum value: 5
Maximum value: 65

The above is the detailed content of What is the difference between Collection and Collections in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete