006 Java Set Analysis 1

Source: Internet
Author: User
Tags iterable

In the previous article in this series of tutorials, the underlying architecture of the collection framework in the JDK was demonstrated using paint. For the collection group, its top-level interface is the Iterable interface, and it is estimated that the reader familiar with the design pattern understands that the interface is introduced here to implement the iterator pattern.

about what is the design pattern or specific to what is the iterator pattern, this article will not be expanded, do not understand the friends, suggested to find additional information. About the design mode of learning, I think we must recommend a book, called "Headfirst design Mode", the book in a humorous manner, very detailed and in-depth explanation of the 23 design patterns in the common design patterns, while the book's example code used in the Java language, makes people feel very cordial.

If due to a variety of reasons for a temporary lack of time or energy to learn and master the iterator pattern, for learning and absorbing the knowledge of this article will not have too much impact, we can first grasp the use of methods and logic, the rationale behind can be further tuition later. First, let's look at the source code for the Iterable interface in the JDK:

Public interface Iterable<t> {

Iterator<t> Iterator ();

}

You can see the content of the entire interface is very simple, there is only one method, and the method returns a iterator (iterator). See here we know that all of the classes that implement the Iterable interface have a iterator () method that returns an iterator.

We continue to follow up and discover that iterator is also an interface, regardless of whether or not you know the design pattern of iterators, the underlying principle is the three main features of object-oriented programming (encapsulation, inheritance, polymorphism). Iterator is a very simple interface, the source code is as follows:

Public interface Iterator<e> {

Boolean hasnext ();

E next ();

void Remove ();

}

There is no secret in front of the source code, you can see the iterator interface is also only three methods: the Hasnext () method is used to determine if there is another element; the next () method is used to get the next element; the Remove () method is used to remove an element at the current iteration position.

Here, I must lament the power of the object-oriented programming paradigm and the flexibility of the interface functions. First, a set of operations that iterate through the collection and access or remove the elements of the collection are wrapped using an iterator iterator interface. Secondly, the Iterable interface is unified to identify the type of collection that can produce iterator interfaces. Finally, we simply implement the Iterable interface by the collection type and generate the concrete iterator interface implementation class according to its own characteristics. The whole idea and way of implementation is the standard iterator pattern, very subtle.

After reading the design of the Iterable interface, we went down. The direct sub-class of the Iterable interface is the collection interface, the specific source code is as follows:

Public interface Collection<e> extends iterable<e> {

int size ();

Boolean isEmpty ();

Boolean contains (Object O);

Iterator<e> Iterator ();

Object[] ToArray ();

<T> t[] ToArray (t[] a);

Boolean Add (e e);

Boolean remove (Object O);

Boolean containsall (collection<?> c);

Boolean AddAll (collection<? extends e> c);

Boolean RemoveAll (collection<?> c);

Boolean retainall (collection<?> c);

void Clear ();

Boolean equals (Object O);

int hashcode ();

}

From the methods contained in the collection interface, this interface simulates the concept of a collection well. This interface contains a number of methods, are some very intuitive addition, removal, judgment and other methods. Note: Since collection does not inherit object to obtain the Equals (object O) method and Hashcode (), and actually the collection interface corresponding to the collection concept should have these two methods, so here you introduce these two methods.

We know that the collection interface has a number of subtypes, such as:

1) List

2) Set

3) Queue

Although they are very different in characterizing the types of data structures and how they are actually stored, they all have all the methods of collection interfaces that can be used by upward transformation if necessary.

? Finally, let's summarize: The collection group has more interfaces and branches, and the map group has fewer interfaces and is a straight-line branch; The top-level interface of the collection group is the top-level interface of the Iterable,map group, which is the map. In fact, the complete architecture of the entire collection framework is far more complex than this, but as long as we have these two diagrams in mind, then both the post-tutorial of absorbing the collection framework and the choice of data structure in actual work can be extremely easy.

This series of documents will be published in my public number, welcome to sweep code attention.

                

006 Java Set Analysis 1

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.