Collection là một interface trong khi Collections là một lớp tiện ích (utility class). Các subinterface của Collection bao gồm Set, List, Queue. Interface Map cũng thuộc Collections Framework nhưng không kế thừa Collection. Các phương thức quan trọng của Collection: add(), remove(), size(), clear(). Lớp Collections chỉ chứa các phương thức static như sort(), min(), max(), fill(), copy(), reverse().
Cú pháp Interface Collection
public interface Collection<E> extends Iterable<E>
Cú pháp lớp Collections
public class Collections extends Object
Ví dụ
import java.util.*;
public class CollectionTest {
public static void main(String args[]) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(20);
list.add(35);
list.add(50);
list.add(65);
// Collections.min() để tìm giá trị nhỏ nhất
System.out.println("Minimum value: " + Collections.min(list));
// Collections.max() để tìm giá trị lớn nhất
System.out.println("Maximum value: " + Collections.max(list));
}
}
Kết quả
Minimum value: 5 Maximum value: 65