当前位置:网站首页>Stream learning record

Stream learning record

2022-06-26 13:00:00 Learning notes of net fish

One 、 summary

stream Flow is Java8 , which deals with key abstractions of collections , He can specify what you want to do with the collection , You can perform very complex lookups 、 Filtering and mapping operations . Use StreamAPI Operates on the collection data , It's kind of like using SQL Execute database query , You can also use StreamAPI To execute operations in parallel .

in short :StreamAPI Provides an efficient and easy-to-use way to process data .

characteristic :

  • It's not a data structure , It doesn't save data
  • The original data source will not be modified , It saves the data after the operation to another object
  • Lazy evaluation , The stream is in the reconstruction process , It's just a record of the operation , Not immediately executed , The timing calculation will not be performed until the termination operation is executed

Two 、 Operation classification  Insert picture description here

  • No state : The processing of an element is not affected by the previous element
  • A stateful : The operation cannot continue until all elements have been obtained
  • Non short circuit operation : All elements must be processed to get the final result
  • Short circuit operation : When you meet some elements that meet the conditions, you can get the final result , Such as A||B, as long as A by true, There is no need to judge B Result

3、 ... and 、 The specific use

  1. Common methods for creating streams
    1.1 Use Collection Under the stream() and parallelStream() Method
  List<AnalysisOrderCaseGroupDO> list = page.getList();
  List<Long> caseGroupIds = list.stream().map(AnalysisOrderCaseGroupDO::getId)
            .collect(Collectors.toList());
 dataList.parallelStream().forEach(d -> geometryBaseService.doSimplify(d, zoom));

1.2 Use Arrays Medium stream() Method , Convert array to stream

Integer[] nums = new Integer[10];
Stream<Integer> stream = Array.stream(nums);

Here is an example used in enumerations :

public enum ModeEnum{
    
	// File operation mode 
	READ_ONLY(1, " read-only ",
	EDITABLE(2, " Editable "),
	;

	Integer code;
	String desc;
	
	public static String descOf(Integer code){
    
	 	if(code == null ){
    
	 		return "";
	 	}
	 	return Arrays.Stream(ModeEnum.values())
	 		.filter(o -> Objects.equals(o.getCode(), code))
	 		.findAny().map(ModeEnum::getDesc).orElse("");
	}
}

1.3 Use Stream Static methods in :of()、iterator() 、generate()

A small case :

public class StreamOfTest {
    
    public static void main(String[] args) {
    
        String  idStr = "12, 22, 33,44";
        List<Long> ids = Stream.of(idStr.split(","))
                .map(String::trim)
                .map(Long::valueOf)
                .collect(Collectors.toList());
        System.out.println(ids);

		Stream<Integer> stream2 = Stream.iterate(0, (x) -> x + 2).limit(6);
        stream2.forEach(System.out::println);

        Stream<Double> stream3 = Stream.generate(Math::random).limit(2);
        stream3.forEach(System.out::println);
    }
}

1.4 Use BufferReader.lines() Method , Turn each line into a stream

原网站

版权声明
本文为[Learning notes of net fish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261155142041.html