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
//limit returns the first n elements of Stream; 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
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