Collection interface
Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). Some collection allow the same elements while others do not. Some can sort and others can't. JAVASDK does not provide classes that inherit directly from the collection, and the Java SDK provides classes that inherit from collection "subinterfaces" such as list and set.
All classes that implement the collection interface must provide two standard constructors: a parameterless constructor is used to create an empty collection, and a constructor with a collection parameter is used to create a new collection. This new collection has the same elements as the incoming collection. The latter constructor allows the user to copy a collection.
How do I traverse every element in the collection? Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that uses the iteration to access each element of the collection one at a time. Typical usage is as follows:
Iterator it = Collection.iterator (); Get an Iteration sub while (It.hasnext ()) { Object obj = It.next ();//Get Next element }
The two interfaces that are derived from the collection interface are list and set.
Collection ├list │├linkedlist │├arraylist │└vector │└stack
Easy to confuse:
1, Java.util.Collection is a collection interface . It provides a common interface method for basic manipulation of collection objects. The collection interface has many specific implementations in the Java class Library. The meaning of the collection interface is to provide a maximum unified operation for a variety of specific collections.
2, Java.util.Collections is a packaging class. It contains a variety of static polymorphic methods related to set operations. This class cannot be instantiated , just like a tool class that serves the Java collection framework.
Import java.util.ArrayList; Import java.util.Collections; Import java.util.List; public class Testcollections {public static void main (String args[]) { //Note list is the implementation of the collection interface List List = new Arraylis T (); int array[] = {6, 8, 23, 1, 7}; for (int i = 0; i < Array.Length; i++) { & nbsp; List.add (Array[i]); } COLLECTIONS.S ORT (list); for (int i = 0; i < Array.Length; i++) { & nbsp; System.out.println (List.get (i)); } } results: 167823
---collection interface of Java learning article