Stream is a new feature in Java8. It is not an InputStream or OutputStream as we know it. In Java, Stream is an enhancement to collection objects, which provides a very convenient and efficient operation on collection objects. StreamAPI can greatly improve the efficiency and readability of programs when processing large volumes of data.

A Stream is not a collection element. It is not a data structure and does not hold data. It is more like an advanced version of Iterator. In the original version of Iterator, users could only explicitly traverse elements one by one and perform certain operations on them. In the advanced version of Stream, the user only needs to specify what operations to perform on the elements it contains, such as “filter out strings with length greater than 10” or “get the first letter of each string”, etc., and the Stream implicitly traverses internally and performs the corresponding data conversion. Stream Stream = stream.of (“a”, “b”, “c”); / / 2. Founded by an array of flow String [] strArray = new String [] {” a “, “b”, “c”}; stream = Stream.of(strArray); stream = Arrays.stream(strArray); ArrayList

list = new ArrayList<>(); stream = list.stream(); Intermediate operations multiple Intermediate operations can be connected to form a pipeline. Intermediate operations do not perform any processing unless termination is triggered on the pipeline. Map, filter, Distinct, sorted, peek, Limit, Skip, Parallel, sequential, unordered Terminal Stream M terminal operations generate results from the pipeline of the stream, The result can be any value that is not a stream forEach, forEachOrdered, toArray, Reduce, Collect, min, Max, count, anyMatch, allMatch, noneMatch, FindFirst, findAny, iterator 5, using the specific case /* map(Function f) takes a Function as an argument that will be applied to each element, */ ArrayList

list = new ArrayList<>(); list.add(“abc”); list.add(“test”); List

output = list.stream().map(String::toUpperCase).collect(Collectors.toList());


List

nums = array. asList(1, 2, 3, 4); List

squareNums = nums.stream().map(n -> n * n).collect(Collectors.toList());

// Filter (Predicate p) Filters elements from the stream Integer[] sixNums = {1, 2, 3, 4, 5, 6}; // Divisible by 2, leaving an even number. Integer[] evens =Stream.of(sixNums).filter(n -> n%2 == 0).toArray(Integer[]::new);

//forEach internal iteration (using the Collection interface requires the user to do iteration, called external iteration. The Stream API uses internal iteration) // iterate over a collection of people, finding the male and printing the name. ArrayList nameList = new ArrayList<>(); Namelist. add(new Person(” 三”,18)); Namelist. add(new Person(” lisi “,18)); nameList.stream().forEach(p -> System.out.println(p.getName()));

//limit returns the first n elements of Stream; List persons = new ArrayList(); for (int i = 1; i <= 10000; i++) { Person person = new Person(i, “name” + i); persons.add(person); } List

personList2 = persons.stream(). map(Person::getName).limit(10).skip(3).collect(Collectors.toList()); System.out.println(personList2);

Sorted streams are sorted by sorted streams. It is more powerful than sorted arrays in that you can reduce the number of elements in streams by performing all types of map, filter, limit, skip, or even distinct. This can help the program significantly reduce execution time. List persons = new ArrayList(); for (int i = 1; i <= 5; i++) { Person person = new Person(i, “name” + i); persons.add(person); } List personList2 = persons.stream().limit(2).sorted((p1, p2) -> p1.getName().compareTo(p2.getName())).collect(Collectors.toList()); /* MatchStream has three match methods, semantically speaking: allMatch: all elements of the Stream match the predicate passed in, which returns true anyMatch: Returns true as long as one element in the Stream matches the called predicate: Returns true */ if no element in the Stream matches the called predicate

List<Person> persons = new ArrayList();

persons.add(new Person(1, “name” + 1, 10));

persons.add(new Person(2, “name” + 2, 21));

persons.add(new Person(3, “name” + 3, 34));

persons.add(new Person(4, “name” + 4, 6));

persons.add(new Person(5, “name” + 5, 55));

boolean isAllAdult = persons.stream().allMatch(p -> p.getAge() > 18);

System.out.println(“All are adult? ” + isAllAdult);

boolean isThereAnyChild = persons.stream().anyMatch(p -> p.getAge() < 12);

System.out.println(“Any child? ” + isThereAnyChild);

Internal iteration reduces the need to write loop code, provides a variety of data processing methods and interfaces that combine lambda expressions to further optimize the code