Java8's Stream API introductory notes

Stream API is a new feature added in the java8 version. It provides a SQL-like operation process for data processing, and plays a very good role in optimizing data processing.

1. Introduction to Stream
1. Streams and collections are different in the following ways

  1. No storage. A stream is not a data structure that stores elements; instead, it transfers elements such as a data structure, array, generator function, or source of I/O channels through a pipeline of computational operations.
  2. Functional nature. The operation on the stream produces a result, but does not modify its source. For example, filtering the Stream obtained from the Stream will generate a new Stream without elements that need to be filtered, instead of removing elements from the source collection.
  3. Laziness seeks. Many stream operations (such as filtering, mapping, or deduplication) can be implemented lazily, exposing optimization opportunities. For example, "find the first String with three consecutive vowels" does not need to check all input strings. Stream operations are divided into intermediate (Stream production) operations and terminal (value or side effects generation) operations. Intermediate operations are always lazy.
  4. Possibly unlimited. Although the size of the collection is limited, the stream is not required. Limit(n) findFirst() such as limit(n) or findFirst() can allow calculations on an infinite stream to be completed in a limited time.
    The elements of the consumable stream can only be accessed once in the lifetime of the stream. Like Iterator, a new stream must be generated to revisit the same elements of the source.
    2. Streams can be obtained in a variety of ways .
  5. From Collection through stream() and parallelStream() methods;
  6. Pass Arrays.stream(Object[]) from the array;
  7. From the upstream class static factory method, such as Stream.of(Object[]), IntStream.range(int, int) or Stream.iterate(Object, UnaryOperator);
  8. The lines of the file can be obtained from BufferedReader.lines();
  9. The stream of the file path can be obtained from the method in Files;
  10. The random number stream can be obtained from Random.ints();
    2. Summary of actual combat
package com.cn.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import com.cn.bean.UserInfo;

/**
 * java8 新API stream 流
 * 
 * 流的使用
 * 1. 创建流(List、Arrays)  2. 中间操作(filter、map、distinct(去重)、sorted(排序))  3.结束操作(forEach)
 * 特性:
 * 1. stream 有‘惰性求值’机制,在没有结束操作时中间操作是不会执行的
 * 2. 使用stream流的时候,对操作的源数据是没有影响的
 * 3. 集合List生成流 List对象.stream  数组生成流 Arrays.stream(数组对象)
 */

public class StreamAPITest {
    
    
	/**
	 * 创建用户数据列表
	 * @return
	 */
	private static List<UserInfo> createUser() {
    
    
//		List<UserInfo> userInfos = new ArrayList<UserInfo>();
//		userInfos.add(new UserInfo("1001","wang",10));
//		userInfos.add(new UserInfo("1002","wang2",11));
//		userInfos.add(new UserInfo("1003","wang3",12));
//		userInfos.add(new UserInfo("1004","wang4",13));
//		userInfos.add(new UserInfo("1005","wang5",14));
		List<UserInfo> userInfos = Arrays.asList(
				new UserInfo("1001","wang",10),
				new UserInfo("1001","wang",10),
				new UserInfo("1004","wang4",13),
				new UserInfo("1004","wang4",13),
				new UserInfo("1004","wang4",13),
				new UserInfo("1004","wang4",13));
		return userInfos;
	}
	
	public static void main(String[] args) {
    
    
		/**
		 * 1. 创建 2. 过滤  3.结束操作 
		 */
		List<UserInfo> userInfos = createUser();
//		userInfos.stream()
//				.filter((e) -> e.getUserAge() >= 12)
//				.forEach(System.out::println);
//	
	/**
	 * 1. 创建 2. 映射  3.结束操作 
	 */
 		Integer[] integers = new Integer[] {
    
    1,2,3,4};
//		Arrays.stream(integers)
//				.map((x) -> x*x)
//				.forEach(System.out:: println);	
	/**
	 * reduce 求值
	 */
//		Optional<Integer> countOptional =  userInfos.stream()
//				.map((e) -> 1)
//				.reduce(Integer :: sum);
//		System.out.println(countOptional.get());
	/**
	 * 去重  (年龄去重)
	 */
 		userInfos.stream()
 					.map((t) -> t.getUserAge())
 					.distinct()
 					.forEach(System.out::println);
		
		
	}
}

Guess you like

Origin blog.csdn.net/Wangdiankun/article/details/109582438