Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Stream in java8

Shulou Source: shulou.com Published: 2022-06-02 08:31:19 10月12日 Update

How to use Stream in java8? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

1. Use Stream

Java8 uses stream to facilitate a variety of processing of data sets, which makes the program less redundant. The use of Stream generally includes these three steps.

Define a data source

Define intermediate operations to form a pipeline

Define terminal operation, execute pipeline, and generate calculation results.

Stream.of ("tony", "9527", "952") .forEach (System.out::println); int [] nums = {1,2,3,4,100,6}; Arrays.stream (nums). Sorted (). ForEach (System.out::println); Files.lines (Paths.get ("/ Users/1120291/Desktop/test.txt")) .forEach (System.out::println); 1.2 Intermediate operation 1.2.1 filter

This operation accepts a function that returns boolean, and the element that returns false will be excluded.

Class Person {private String name; private Integer age; @ Override public String toString () {return "Person {" + "name='" + name +'\'+ ", age=" + age +'}';} public void setName (String name) {this.name = name;} public void setAge (Integer age) {this.age = age } public String getName () {return name;} public Integer getAge () {return age;}} public static List getPersonList () {List personList=new ArrayList (); for (int ionometrics I person.getAge () > 25) .person.getAge (Collectors.toList ()); personList1.stream () .forEach (person-> {System.out.println (person);}); 1.2.2 distinct

De-repetition operation

List data = Stream.of (1, 7, 3, 8, 2, 4, 9, 7, 9). Collectors.toList (); 1.2.3 limit

This method restricts the flow to return only a specified number of elements, similar to limit in sql

List data = Stream.of (1, 7, 3, 8, 2, 4, 9, 7, 9) .limit (2) .limits (Collectors.toList ()); 1.2.4 skip

Throw away the specified number of elements before, and use them with limit to achieve the effect of turning pages.

List data = Stream.of (1, 7, 3, 8, 2, 4, 9, 7, 9) .skip (3) .limit (2) .limits (Collectors.toList ()); 1.2.5 map

Each element in the flow will be applied to this function, and the returned result will form a new type of flow to continue with subsequent operations, similar to scala's map.

GetPersonList () .stream () .filter (customer-> customer.getAge () > 20) .map (person-> {return person.getName ();}) .forEach (System.out::println)

The type of stream before calling map is Stream, and the type after executing map is Stream

1.2.6 flatMap

FlatMap is similar to map, except that it is one-to-many, and one entry returns multiple.

GetPersonList () .stream () .flatMap (person-> {return Stream.of (person.getName (). Split (",");}) .forEach (System.out::println); / / Note that the return type of flatMap is the flatMap of Stream (Function)

Tags: Element grouping age maximum user minimum number data type result method terminal problem collector two function parameter customer that is more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information MySQL Linux Apple