Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills of using Stream in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "what are the skills for the use of Stream in Java", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what skills for the use of Stream in Java" can help you solve your doubts.

Stream

Use this method to create a Stream object.

New ArrayList (). Stream () Filter

Filter, which passes a function that preserves the element if the return result of this function is true, otherwise discards it.

StringCollection .stream () .filter ((s)-> s.startsWith ("a")) .forEach (System.out::println); Foreach

Traversing, consumption.

StringCollection .stream () .filter ((s)-> s.startsWith ("a")) .forEach (System.out::println); Map

This function is also traversed, but it has a return value, while the above Foreach has no return value, just pure consumption. And Foreach cannot be chained because there is no return value, but Map is fine.

StringCollection .stream () .map (String::toUpperCase) .sorted (Comparator.reverseOrder ()) .forEach (System.out::println); Sorted

This method is used for sorting, in which the function passed is a comparator, or it can not pass parameters, just use the default.

StringCollection .stream () .sorted ((x, y)-> y.length ()-x.length ()) .filter ((s)-> s.startsWith ("a")) .forEach (System.out::println); Match

Returns true or false depending on whether the given stream object contains the specified content.

The details are as follows:

AllMatch

AnyMatch

NoneMatch

Boolean anyStartsWithA = stringCollection .stream () .anyMatch ((s)-> s.startsWith ("a")); boolean allStartsWithA = stringCollection .stream () .allMatch ((s)-> s.startsWith ("a")) Boolean noneStartsWithZ = stringCollection .stream () .noneMatch ((s)-> s.startsWith ("z")); count

Counts the number of elements in the collection.

Long startsWithB = stringCollection .stream () .filter ((s)-> s.startsWith ("b")) .count (); reduce

This function is similar to the Fibonacci sequence, each passing parameters are the result of the previous one and the new elements taken from the collection. The first and second elements are taken out by default for the first time.

A simple example is that for the first time, 0 reduce 1 takes the result of the first time as the first parameter, 2 as the second parameter, and so on.

Optional reduced = stringCollection .stream () .sorted () .reduce ((S1, S2)-> S1 + "#" + S2); parallelStream

Parallel steam streams can be processed in parallel, which makes it more efficient. There is no thread safety problem with this traversal when using stream.foreach, but there will be thread safety problems when using parallelStream. All external variables used in parallelStream, such as collections, must use thread safe collections, otherwise it will cause multi-thread safety problems. If you want to ensure security, you need to use reduce and collect, but this is super troublesome to use!

Long count = values.parallelStream (). Sorted (). Count (); IntStream.range (aforme b)

Integers from a to b can be generated directly, and this still follows most of the conventions of the programming language, that is, the beginning without the end.

IntStream.range (0,10) .forEach (System.out::println)

The result of the output is

0

one

two

three

four

five

six

seven

eight

nine

New Random () .ints ()

Get a series of random values, and the data coming out of this interface is continuous, so you need to limit it with limit.

New Random (). Ints (). Limit (10) .forEach (System.out::println); SupplierSupplier stringSupplier=String::new;stringSupplier.get ()

This interface is an abstract method get method, which returns an instance of generic T without passing in any parameters. Just like the no-parameter structure.

Consumer1. Accept method

The only abstract method of this functional interface receives a parameter and does not return a value.

2. AndThen method

The method passed in parameters is executed after the caller method is executed.

Public class ConsumerTest {public static void main (String [] args) {Consumer consumer = (x)-> {int num = x * 2; System.out.println (num);}; Consumer consumer1 = (x)-> {int num = x * 3; System.out.println (num);}; consumer.andThen (consumer1) .accept (10) }

First consumer.accept (10) and then consumer1.accept (10)

IfPresent

Execute for an optional if it has a value, otherwise it will not be executed.

IntStream .builder () .add (1) .add (3) .add (5) .add (7) .add (11) .build () .average () .ifPresent (System.out::println)

The result of average execution is an optional.

Collect

He has two ways of calling it.

R collect (Supplier supplier, BiConsumer accumulator, BiConsumer combiner); R collect (Collector)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report