The java Collection framework is divided into two parts, one is Map, in the form of key and value; the other is the Collection interface, which is the final parent interface of set and list.
Let's take a look at his definition
public interface Collection<E> extends Iterable<E>
First, it has generics, and second, it inherits from the Iterable interface, indicating that its implementation class can use iterators to traverse elements.
We should note that Collection is only an interface, so he has defined some methods. Let's take a look at them
-
/** * Returns the number of elements in this collection. If this collection * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns * <tt>Integer.MAX_VALUE</tt>. * * @return the number of elements in this collection */ int size();
The main function of the size method is to return the number of elements stored in the current collection object. However, if the number of elements in the current collection exceeds the maximum Integer that Integer can represent, only the maximum value of Integer can be returned
-
/** * Returns <tt>true</tt> if this collection contains no elements. * * @return <tt>true</tt> if this collection contains no elements */ boolean isEmpty();
The isEmpty method is used to determine whether the elements are stored in the current collection object. If the elements are stored, it returns true; otherwise, it returns false.
-
/** * Returns <tt>true</tt> if this collection contains the specified element. * More formally, returns <tt>true</tt> if and only if this collection * contains at least one element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this collection is to be tested * @return <tt>true</tt> if this collection contains the specified * element * @throws ClassCastException if the type of the specified element * is incompatible with this collection * (<a href="#optional-restrictions">optional</a>) * @throws NullPointerException if the specified element is null and this * collection does not permit null elements * (<a href="#optional-restrictions">optional</a>) */ boolean contains(Object o);
The contains method is used to determine whether the collection Object contains the specified element (parameter o), which is of Object type. If it contains o, it returns true, otherwise it returns false. It is worth noting that if O is null and an element in the collection Object is also null, then true is also returned. When the parameter o is not an element supported by collection generics, you can choose to crawl out the classcastexpectation exception. When the parameter o is null and the collection Object does not support storing null, you can choose to throw an NPE exception
-