Home > Java > javaTutorial > body text

What is the difference between Collection and Collections in Java?

WBOY
Release: 2023-09-01 21:57:05
forward
1032 people have browsed it

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>
Copy after login

Syntax of collection class

public class Collections extends Object
Copy after login

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));
   }
}
Copy after login

Output

Minimum value: 5
Maximum value: 65
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template