A summary of the Java set Framework _java

Source: Internet
Author: User

This article first introduces the interface and class of Java set framework, then summarizes some basic knowledge and key points in the set frame, 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. This state has been improved since JDK1.5: You can use generics to limit the type of elements in a collection and let the collection remember the types of all collection elements.

First, summary

All collection classes are located under the Java.util package. Only objects can be saved in the collection (Save the object's reference variable). (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. This state has been improved since JDK1.5: You can use generics to limit the type of elements in a collection and let the collection remember the types of all the elements of the collection (see the contents of the specific generics).

The Java Collection class derives primarily from two interfaces: Collection and map,collection and map are the root interfaces of the Java Collection framework, and these two interfaces contain 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 three classes of collections. The list collection is an ordered collection in which elements in the collection can be repeated, and access to elements in the collection can be accessed based on the index of the element. Set collections are unordered collections in which elements in a collection cannot be duplicated, and access to elements in the collection can only be accessed based on the element itself (and cannot be the reason that the elements in the collection do not allow duplication).

In the map collection, the elements of the Key-value form are saved, and the value can only be accessed from the key of each element.

For set, list, and map three collections, the most common implementation classes are the HashSet, ArrayList, and HashMap three implementation classes. (Concurrency-controlled collection class, later available for research).

Second, collection interface

The collection interface is the parent interface of the list, set, and queue interfaces, and can operate on these three interfaces. Collection interface defines the method of manipulating a set of elements you can refer to the API documentation, and here is an example of how collection adds elements, deletes elements, returns the number of elements in the collection, and clears the collection elements.

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, primarily used to traverse (that is, iterative access) The elements in the collection collection, also known as iterators.

Three ways to provide:

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 you assign a book to an iteration variable, the elements in the collection do not change when we output the books collection again." That is, when using iterator to iterate over the collection elements, the 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 collection collection elements, only the Remove method from iterator (It.remove ();) The collection element returned by the last next method can add elements to the collection (book = "Test String" ; )。 Otherwise, a Java.util.ConcurrentModificationExcption exception is thrown.

2, using a 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 sets, and uses a traditional for loop if you do not want to traverse the entire collection, or if you need to manipulate subscript values within the loop.

(2) simplifies programming, improves code readability and security (do not fear array out of bounds).

(3) foreach general combination of generic use

The above summary of the Java set Framework is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.