<java8 What's new > About collection operations (Collection/iterator/stream)

Source: Internet
Author: User
Tags new set

Because lambda expressions are frequently used later in this article, see the contents of lambda expressions in Java:

Http://www.cnblogs.com/guguli/p/4394676.html

I. Traversing a collection element with an enhanced iterator

The iterator interface is also a member of the Java Collection framework, but it is not the same as the collection of the collection series, the map series: The Collection Series collection, which is used primarily to load other objects, The iterator is used primarily to traverse (that is, iterate) the elements in the collection collection, and iterator objects are also known as iterators.

The iterator interface hides the underlying details of various collection implementation classes, providing the application with a unified programming interface that iterates through collection collection elements, and the iterator interface generally defines the following 4 methods.

boolean Hasnext (): Returns a Boolean value that indicates whether the collection element has not been accessed.

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

void Remove (): deletes the element returned by the previous next method in the collection.

void foreachremaining (Consumer action): This is the default method that Java8 adds to iterator, which uses a lambda expression to iterate through the collection elements.

 Public classiteratortest{ Public Static voidMain (string[] args) {....//C is a collection<string>Iterator iter=C.iterator ();  while(Iter.hasnext ()) {String s=(String) iter.next ();            System.out.println (s); if(S.equals ("Content") ) {iter.remove (); } s= "New Content";    } System.out.println (s); }}


# #Iterator仅用于遍历集合, iterator itself does not provide the ability to load objects.

# #Iterator必须依附于Collection对象, if there is a iterator object, there must be a collection object associated with it.

# #Iterator迭代器采用的是快速失败 (fail-fast) mechanism, once the collection has been detected during the iteration (usually modified by other threads in the program), the program throws the Concurrentmodificationexception exception immediately, Instead of displaying the modified results, this avoids sharing resources and raises potential problems.

Two. New set of predicate operations using JAVA8

Java8 adds a new Removeif (predicate filter) method to the collection collection, which will bulk delete all elements that conform to the filter condition, which requires a predicate object as a parameter. predicate is also a functional interface, so you can use a lambda expression as a parameter.

... Collection C=new  HashSet (), C.add (new String ("Content 1"); C.add (New string ("Content 2"); C.add (new string ("Content 3")); // /lambda expressionC.removeif (S-> ((String) s). Length () <10); System.out.println (c);

Using predicate can simplify the operation of a collection and use lambda expressions to make the entire program brief. You can also use the Callall method to filter all with a lambda expression of type predicate, similar to select* in the SQL language.

Three. New stream operation collection using Java8

JAVA8 has added APIs for data flow processing such as Stream,intstream,longstream,doublestream, which represent multiple elements that support serial and parallel aggregation operations, where stream is a common interface in the above 4 interfaces, while others The **stream interface represents a stream with an element type of Int/double/long.

The JAVA8 provides the corresponding Builder for each of the above stream interfaces, such as Stream.builder. Developers can use these builder to create a corresponding stream. The steps are as follows:

# #使用Stream或者 **stream's builder () class method creates the corresponding builder for the stream.

# #重复调用Builder的add () method adds an element to the stream.

# #调用Builder的build () method to get the corresponding stream

# #调用Stream的聚集方法

For the aggregation of streams, the main list is explained as follows (explain the two concepts first # #):

# #intermediate (Intermediate method): Intermediate operations allow flow to remain open and allow direct invocation of subsequent methods

# #terminal (End method): The end operation of the end operation when the end method is executed on a stream and the stream cannot continue to be used

Intermediate

(1) filter (predicate p): Returns the element that all causes predicate to return true.

(2) maptoxxx (toxxxfunction Mapper): Each of the elements in a stream is converted one-to-another by a method of replication toxxxfunction.

(3) Peek (Consumer C): Similar to the foreach operation. The return stream contains the same elements as the original stream. This method is mainly used for debugging.

(4) Distinct (): This method is used to sort all the duplicated elements in the stream.

(5) sorted (): This method is used to ensure that the elements in the stream are in an orderly state in subsequent accesses.

(6) Limit (long MaxSize): This method is used to guarantee the maximum number of elements allowed to access in subsequent elements of a stream.

Terminal

(1) ForEach (Consumer C): Traverses an element to perform a C operation on each element.

(2) ToArray (): Converts all the elements in a stream into an array.

(3) Reduce (): Merges the elements in the stream.

(4) min (): Returns the smallest element in the stream.

(5) Max (): Returns the largest element in the stream.

(6) Count (): Returns the number of elements in the stream.

(7) AnyMatch (predicate p): Determines whether a stream contains at least one element that conforms to p.

(8) Nonematch (predicate p): determine if the stream does not contain an element that conforms to p.

(9) Allmatch (predicate p): Determines whether all elements in the stream conform to p.

(Ten) Findany (): Returns any element in the stream.

<java8 What's new > About Collections (collection/iterator/stream)

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.