1. CollectionAPI
A Collection is a Collection of objects ). Collections are an important data interface in programming. Java provides a collection class library called CollectionAPI.
A set uses an object to represent a group of objects. Each object in the set is called an element. The specific types of each element in the set can be different, but generally they are derived from the same class (and this is not difficult to do, because all classes in Java are subclasses of objects ). When retrieving each element from a set, it is often necessary to perform corresponding forced type conversion based on its specific type.
The interfaces and classes in the Collection API are mainly unique in the java. util package. The most basic interface is Collection, which organizes a group of objects in the form of Collection elements and implements different organization methods in its subinterfaces. There are two subinterfaces for Collection.
Set: the sequence in which elements are stored is not recorded, and repeated elements are not allowed;
List: Record elements are kept in order and repeated elements are allowed.
The reuse implementation class of the Set interface includes HashSet ). Important implementation classes of the List interface include ArrayList, List, and Vector.
Shows the relationship between them.
Collection and its sub-interfaces List and Set
Ii. Collection interface and Method
The Collection interface has the following important methods:
Public boolean add (Object o); // add an element
Public boolean remove (Object o); // remove an element
Public void clear (); // clear all elements
Public boolean contains (Object o); // determines whether an element is contained.
Public int size (); // number of elements
Public boolean isEmpty (); // determines whether it is null.
Public Iterator iterator (); // get the Iterator