Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Stream in java8

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report