Learning comprehension of some new features of JAVA8

Source: Internet
Author: User

Recently in the learning queue related to some knowledge, in the learning process found the Iterable<t> interface added two new methods, out of curiosity, want to know what this is, what to use. As the saying goes: Practice the truth, so there are the following repeated tests.

Let's look at these two new methods:

1 default void Super T> action) {2    objects.requirenonnull (action); 3      for  This ) {4        action.accept (t); 5     }6 }
1 default Spliterator<t> Spliterator () {2      return spliterators.spliteratorunknownsize ( Iterator (), 0); 3 }


All know that the interface is not write the implementation method of the HA, but it has the realization method, suddenly feel that the world view has been overturned, in fact, this is also java8 new feature, in the interface method to add the default keyword can be directly in the interface write implementation (this is not the focus of this article, Interested children's shoes can be self-learning java8).

The following formally begins testing < using Blockingqueue test;

1 New Arrayblockingqueue<integer> (1024x768); 2  while (Q.size () <10) {3     q.put (q.size () +1); 4 }5 spliterator<integer> split = q.spliterator (); 6 split.foreachremaining (line->{system.out.println (line);});  ①
7 System.out.println ("-------------------------------------");
Split.foreachremaining (Line->{system.out.println (line);}); Ii

The above output results are:

1
2
3
4
5
6
7
8
9
10
-------------------------------------

According to the above results, we can see that the ② does not output any data at all. Look at the iteration class foreachremaining provided by Spliterator, which is a new iteration class for parallel processing of streams,

Source:

1 default void Super T> action) {2do         while (Tryadvance (action)); 3 }

The tryAdvance method then executes the action on the next operation for processing and returns true if there is no next element, which returns false.

1  Public BooleanTryadvance (consumer<?SuperT>action) {2       if(Action = =NULL)Throw Newnullpointerexception ();3        if(It = =NULL) {4it =collection.iterator ();5EST = (Long) collection.size ();6        }7       if(It.hasnext ()) {8 action.accept (It.next ());9           return true;Ten       } One       return false; A}

According to the above source code can be seen, action if empty directly throws an exception. When it is empty, it is the iterator object that traverses the incoming collection.

So in ①: Call the iteration call Tryadvance, after executing ①, it has pointed to the last item of Conllection, and for ②, the incoming collection is the same set, for it also has pointed to the last item, so returned false, there is no data to output.

Trysplit:arraylistspliterator's Trysplit uses dichotomy, returns the first half of the data, and returns NULL if the data is too small to be divided. and Concurrentlinkedqueue and concurrentlinkeddeque the corresponding spliterator processing a little more complex, first take one, the second take two, not more than Max_batch.

More features also need to be learned slowly.

Learning comprehension of some new features of JAVA8

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.