What is a Stream

java.util.stream.Stream

A Stream and a traditional IO Stream, both of which are called streams, are completely different things.

Streams can simply be said to work with collections of data, and declarative streaming apis can work with collections rather than writing a logical implementation.

Flow classification

Flow is divided into sequential flow and parallel flow. Sequential flow means that each instruction is executed sequentially, while parallel flow means that operations in a set are executed in parallel.

List<Integer> numbers = Arrays.asList(1, 2, 3); Stream ().foreach (n -> system.out.print (n)); Thread.parallelstream ().foreach (n -> system.out.print (n));Copy the code

In the above example, a sequential stream always outputs 123, whereas a parallel stream does not always output 123. A parallel stream uses ForkJoinPool to divide and conquer, so those of you who understand the principle of ForkJoinPool know what a parallel stream looks like.

Create a flow

1. Call the stream() or parallelStream() methods of the collection.

2, stream.of (), IntStream, LongStream for int,long

Use the stream

Here are some common uses of streams.

Public class StreamTest {public static void main(String[] args) {system.out.println (" filter - find older than 18 "); List<User> list = initList(); list.stream().filter((User user) -> user.getAge() > 18).collect(Collectors.toList()) .forEach(System.out::println); System.out.println(); System.out.println(" Max - find the oldest person "); list = initList(); Optional<User> max = list.stream().max((u1, u2) -> u1.getAge() - u2.getAge()); System.out.println(max.get()); System.out.println(); System.out.println(" map - gauge - sum of all ages "); list = initList(); Optional<Integer> reduce = list.stream().map(User::getAge).reduce(Integer::sum); System.out.println(reduce.get()); System.out.println(); System.out.println(" group by age "); list = initList(); Map<Integer, List<User>> userMap = list.stream() .collect(Collectors.groupingBy(User::getAge)); MapUtils.verbosePrint(System.out, null, userMap); System.out.println(); System.out.println(" create/delete/count "); Stream<User> userStream = Stream .of(new User("u1", 1), new User("u2", 21), new User("u2", 21)); System.out.println(userStream.distinct().count()); System.out.println(); } public static List<User> initList() { List<User> list = new ArrayList<>(); list.add(new User("oaby", 23)); list.add(new User("tom", 11)); list.add(new User("john", 16)); list.add(new User("jennis", 26)); list.add(new User("tin", 26)); list.add(new User("army", 26)); list.add(new User("mack", 19)); list.add(new User("jobs", 65)); list.add(new User("jordan", 23)); return list; }}Copy the code

Output result:

User [username=oaby, age=23] User [username=jennis, age=26] User [username=tin, age=26] User [username=army, age=26] User [username=mack, age=19] User [username=jobs, age=65] User [username=jordan, User [username=jobs, age=65] User [username=jobs, age=65] User [username=jobs, age=65] User [username=jobs, age=65] age=16]] 65 = [User [username=jobs, age=65]] 19 = [User [username=mack, age=19]] 23 = [User [username=oaby, age=23], User [username=jordan, age=23]] 26 = [User [username=jennis, age=26], User [username=tin, age=26], User [username=army, Age =26]] 11 = [User [username= Tom, age=11]]} Create - delete - statistics 2Copy the code

As you can see, the stream manipulation data set is very powerful, but it is important to note that the stream can only be executed once, and it needs to be turned on again.

More gameplay can be studied by yourself.

Recommended reading

Dry goods: Free 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.