Java Collection Class source code learning One

Source: Internet
Author: User
Tags iterable

For the Java Collection class, look at the two graphs first

These two graphs roughly depict the Java Collection class overview, two systems, a collection set system, a map set system. Before talking about the collection class, say iterable this interface, this interface before jdk1.8, there is only one method iterator<t> iterator (), that is, the return of a T-type iterator, in 1.8 added two default decoration method

default void ForEach (CONSUMER<? Super T> action) {        objects.requirenonnull (action);        for (t t:this) {            action.accept (t);        }    } Default Spliterator<t> spliterator () {        return spliterators.spliteratorunknownsize (iterator (), 0);    }

This is for the implementation of Java functional programming and added to the method, the specific use in this article is not to say, and later specifically to get an article to say this. There is also an interface Iterator<e>, which contains two methods Boolean Hasnext () and E next (), both of which are used to determine whether the set has the next element and one to take out the next element. In the same iterator there are two default methods, which are skipped first. The role of the Iterable interface is that if a class implements this interface, or if it implements an interface that inherits the interface, it can use the enhanced for loop to loop through the elements in the collection (although it is also used as an iterator); The iterator interface is If a class or its parent implements this interface, then it is possible to use the iterator's Hasnext () and Next () to iterate through the values explicitly. Let's take a look at the collection interface:

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); Default Boolean Removeif (PREDICATE&LT;? Super e> Filter) {objects.requirenonnull (filter); Boolean removed = false; Final iterator<e> each = Iterator (); while (Each.hasnext ()) {if (Filter.test (Each.next ())) {each.remove (); removed = true; }} return removed; } Boolean Retainall (collection<?> c); void Clear (); Boolean equals (Object O); int hashcode (); @Override default Spliterator<e> spliterator () {return spliterators.spliterator (this, 0); } default Stream<e> Stream () {return Streamsupport.stream (Spliterator (), false); } Default Stream<e> ParallelstReam () {return Streamsupport.stream (Spliterator (), true); }}

Aside from the default method, the remaining 15 methods are the most basic method of collection collection system, will be implemented in its implementation class, the names of these methods are obvious, it is not detailed, it is worth noting that this interface inherits the Iterable<e> interface, which has a iterator<e> iterator () method, which means that the implementation class of the collection collection system can use the enhanced for loop to take the value. In Eclipse, the collection interface presses F4 to view references, such as

We can see that the following three interfaces inherit the collection interface (Beancontext is not under the Java.util package, ignore), List,queue and set, and some implementation classes. The three interfaces correspond to those on the graph, and we hit to see them. Look at the list interface first, it has all the methods in the collection interface (not including the default method of collection), and then there are a few of their own methods, it should be noted that there is a listiterator method, this thing is an iterator, But compared to iterator, this thing a few more functions, in fact, the equivalent of iterator an extension:

Public interface Listiterator<e> extends Iterator<e> {boolean hasnext (); E next (); Boolean hasprevious (); e previous (); int nextindex (); int previousindex (); void remove (); void set (E e); void Add (E e);}

The meaning of the method is also quite clear, but the set method is to set the last value and the Add method is to precede the current index with a value. List with this iterator, you can be self-willed to take the value. Similarly, look at the set interface and the queue interface, the method of discovering the set interface is basically the same as in collection, and the queue interface is almost completely different, except for the add and remove methods, Then it should be in the subclass or inherit its interface to implement the collection remaining method. OK, continue to study tomorrow.

Java Collection Class source code learning One

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.