Filter for duplicate elements
The stream interface supports the distinct method, which returns the flow of an element (as implemented by the hashcode and equals methods of the elements generated by the stream).
For example, the following code filters out all the even numbers in the list and ensures that there are no duplicates.
List<dish> dishes = Dish.menu.stream (). Filter (Dish::isvegetarian). Collect (Collectors.tolist ()); Skips a specified number of elements
Stream supports the Skip (n) method, returning a stream that throws up the first n elements. If there are less than n elements in the stream, an empty stream is returned. The limit (n) and Skip (n) are complementary
list<dish> Dishskip = Dish.menu.stream (). Filter (D-d.getcalories () >). Skip (2)// Returns the first 2 elements of a collection that meet the requirements. Collect (Collectors.tolist ());d Ishskip.foreach (system.out::p rintln); Map operations
Stream supports the map method, which takes a function as a parameter. This function is applied to each element and is mapped to a new element.
list<string> list = St.skip (0). Limit (2). Map (S->s.touppercase ()). Collect (Collectors.tolist ()); element sum list <Integer> numbers = arrays.aslist (3,4,5,1,2); int sum1 = Numbers.stream (). Reduce (0, (A, B), A + B); System.out.println (SUM1); int sum2 = Numbers.stream (). reduce (0,integer::sum); System.out.println (sum2); max int max = Numbers.stream (). reduce (0,integer::max); SYSTEM.OUT.PRINTLN (max);//reduce does not accept the initial value, returns a Optional object (considering the absence of any element in the stream) optional<integer> min = Numbers.stream (). reduce (integer::min); Min.ifpresent (System.out::p rintln);
Stream stream data in Java8