Java learning --- Collection Interface

Source: Internet
Author: User

Java learning --- Collection Interface

Collection Interface

 

Collection is the most basic Collection interface. A Collection represents a group of objects, namely, Elements of the Collection ). Some collections allow the same elements while others do not. Some can be sorted, while others cannot. Java SDK does not provide classes that directly inherit from collections. Classes provided by Java SDK are "sub interfaces" that inherit from collections, such as List and Set.

All classes that implement the Collection interface must provide two standard constructor: A non-parameter constructor is used to create an empty Collection, A constructor with the Collection parameter is used to create a new Collection, which has the same elements as the imported Collection. The next constructor allows you to copy a Collection.

How to traverse every element in the Collection? Regardless of the actual type of Collection, it supports an iterator () method. This method returns an iterator, and each element in the Collection can be accessed one by one using this iterator. The typical usage is as follows:

Iterator it = collection. iterator (); // get an iteration sub-while (it. hasNext () {Object obj = it. next (); // get the next element}
The two interfaces derived from the Collection interface are List and Set.

 

Collection  ├List  │├LinkedList  │├ArrayList  │└Vector  │ └Stack  └Set 

 

Obfuscation:

1. java. util. Collection isSet Interface. It provides common interface methods for basic operations on collection objects. The Collection interface has many specific implementations in the Java class library. The Collection interface provides a unified operation mode to maximize various specific sets.
2. java. util. Collections is a packaging class. It contains various Set OperationsStatic polymorphism Method. SuchCannot be instantiated, LikeToolsTo serve 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 that List is the List list that implements the Collection interface = new ArrayList (); int array [] = {6, 8, 23, 1, 7}; for (int I = 0; I <array. length; I ++) {list. add (array [I]);} Collections. sort (list); for (int I = 0; I <array. length; I ++) {System. out. println (list. result: 167823



 

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.