---collection interface of Java learning article

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.