Summary of the Java Collection framework

Source: Internet
Author: User

This article introduces the interfaces and classes contained in the Java Collection framework, then summarizes some basic knowledge and key points in the collection framework, and makes a simple analysis with examples. When we put an object into the collection, the system treats all the collection elements as instances of the object class. Since JDK1.5, this state has improved: You can use generics to constrain the type of elements in a collection and let the collection remember the types of all collection elements.

First, the summary

All collection classes are located under the Java.util package. Only objects can be saved in the collection (a reference variable that holds the object). (Arrays can hold both basic types of data and objects).

When we put an object into the collection, the system treats all the collection elements as instances of the object class. Since JDK1.5, this state has improved: You can use generics to constrain the type of elements in a collection and let the collection remember the type of all the elements of the collection (see specific generic content).

The Java Collection class is primarily derived from two interfaces: Collection and map,collection and map are the root interfaces of the Java Collection framework, which in turn contains some interfaces or implementation classes. The set and list interfaces are two sub-interfaces derived from the collection interface, and queue is a Java-provided queuing implementation, similar to list.

The map implementation class is used to hold data that has a mapping relationship (Key-value). Set, list, and map can be seen as the three main classes of collections. The list collection is an ordered collection, and the elements in the collection can be repeated, and accessing the elements in the collection can be accessed based on the index of the element. The set collection is an unordered collection, and the elements in the collection cannot be duplicated, and access to the elements in the collection can only be accessed based on the element itself (and the reason that the elements in the collection are not allowed to be duplicated).

The map collection holds elements of the key-value pair form, which can only be accessed by the key of each element when accessing its value.

For set, list, and map three collections, the most common implementation classes are HashSet, ArrayList, and HashMap, respectively, of three implementation classes. (The collection class for concurrency control, which is available later in the study).

Second, collection interface

The collection interface is the parent interface of the list, set, and queue interfaces, and can operate on these three interfaces. The collection interface defines a specific way to manipulate a collection element. You can refer to the API documentation, which shows an example of collection adding elements, deleting elements, returning the number of elements in a collection, and emptying the elements of a collection.

Three, two methods of traversing a set iterator interface and Foreach Loop

1. Iterator interface

Iterator is also a member of the Java Collection framework, which is used primarily to iterate (i.e. iterate through) the elements in the collection collection, also known as iterators.

Available in three ways:

Boolean hasnext (): Returns the next element in the collection.

Object Next (): Returns the next element in the collection.

void Remove (); Deletes the element returned by the previous next method in the collection.

(1) Through the statement "book =" test string "; "When assigning a value to an iterative variable book, when we output the books collection again, the elements in the collection do not change." That is, when iterating over a collection element using iterator, iterator does not pass the collection element itself to the iteration variable, but instead passes the value of the collection element to the iteration variable.

(2) When using iterator to access the collection collection element, only the iterator Remove method is removed (It.remove ();) The collection element returned by the previous next method can add elements to the collection (book = "Test String" ; )。 Otherwise, a Java.util.ConcurrentModificationExcption exception is thrown.

2. Use the Foreach loop to iterate through the collection elements.

Format: for (element type T element variable x: Traverse object A) {//program block}

Description

(1) foreach simplifies the traversal of arrays and collections, and uses a traditional for loop if you do not want to traverse the entire collection, or if you need to manipulate subscript values inside the loop.

(2) Simplified programming, improved Code readability and security (do not fear the array out of bounds).

(3) foreach is generally combined with generics

Summary of the Java Collection framework

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.