并发编程:并行反应式流:遍历流(forEach,forEachOrdered)

tech2026-08-17  0

目录

 

方法说明

一、主程序

二、辅助类

三、执行结果


方法说明

 forEach():无法保证动作是有序的应用到流上的。 forEachOrdered():需要先使用sorted方法排序,再使用该方法,该方法可以保证动作是有序应用到流上的 peek(): 这是个中间操作,是懒执行的,只有调用结尾操作时,动作才会真正应用到流的消费元素上。

一、主程序

package xyz.jangle.thread.test.n6_5.foreach; import java.util.List; import xyz.jangle.thread.test.n6_2.create.Person; import xyz.jangle.thread.test.n6_2.create.PersonGenerator; import xyz.jangle.thread.test.n6_3.mapreduce.DoubleGenerator; /** * 6.5、遍历流(forEach) * forEach无法保证动作是有序的应用到流上的。 * forEachOrdered(): 需要先使用sorted方法排序,再使用该方法,该方法可以保证动作是有序应用到流上的 * peek(): 这是个中间操作,是懒执行的,只有调用结尾操作时,动作才会真正应用到流的消费元素上。 * @author jangle * @email jangle@jangle.xyz * @time 2020年9月4日 下午4:04:05 * */ public class M { public static void main(String[] args) { List<Double> doubleList = DoubleGenerator.generateDoubleList(10, 100); System.out.println("**使用排序后的流来遍历:"); doubleList.parallelStream().sorted().forEachOrdered(d -> System.out.println(d)); System.out.println("**不使用排序遍历:"); doubleList.parallelStream().sorted().forEach(d -> System.out.println(d)); List<Person> persons = PersonGenerator.generatePersonList(10); System.out.println("**不使用排序遍历:"); persons.parallelStream().forEach(p -> System.out.println(p.getLastName() + "," + p.getFirstName())); System.out.println("**使用排序后的流来遍历:"); persons.parallelStream().sorted() .forEachOrdered(p -> System.out.println(p.getLastName() + "," + p.getFirstName())); doubleList.parallelStream().peek(d -> System.out.println("1 Step :" + d)) .peek(d -> System.out.println("2 Step :" + d)).forEach(d -> System.out.println("Last Step N:" + d)); } }

二、辅助类

DoubleGenerator:

https://blog.csdn.net/Bof_jangle/article/details/108372556

PersonGenerator:

https://blog.csdn.net/Bof_jangle/article/details/108350870

Person:

package xyz.jangle.thread.test.n6_2.create; import java.util.Comparator; import java.util.Date; /** * 普通类,雇员信息 * * @author jangle * @email jangle@jangle.xyz * @time 2020年9月1日 下午6:03:57 * */ public class Person implements Comparable<Person> { private int id; private String firstName, lastName; private Date birthDate; private int salary; private double coeficient; private static Comparator<Person> comparator = Comparator.comparing(Person::getLastName) .thenComparing(Person::getFirstName); @Override public int compareTo(Person o) { /* * int compareLastNames = this.getLastName().compareTo(o.getLastName()); if * (compareLastNames != 0) { return compareLastNames; } else { return * this.getFirstName().compareTo(o.getFirstName()); } */ return comparator.compare(this, o); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public double getCoeficient() { return coeficient; } public void setCoeficient(double coeficient) { this.coeficient = coeficient; } @Override public String toString() { return firstName + " " + lastName; } @Override public boolean equals(Object obj) { if (obj instanceof Person) { return this.compareTo((Person) obj) == 0; } else { return false; } } @Override public int hashCode() { String sequence = this.getLastName() + this.getFirstName(); return sequence.hashCode(); } }

三、执行结果

**使用排序后的流来遍历: 12.126256419681935 27.258965652231247 59.894337814749136 62.324808431739996 63.79242930792434 69.70840302146416 69.89499952458232 72.60540924733482 76.06770984905098 82.96765068330782 **不使用排序遍历: 69.89499952458232 82.96765068330782 63.79242930792434 59.894337814749136 72.60540924733482 76.06770984905098 27.258965652231247 69.70840302146416 12.126256419681935 62.324808431739996 **不使用排序遍历: V,B V,D Z,G X,C T,B W,A X,E Y,B U,F Y,D **使用排序后的流来遍历: T,B U,F V,B V,D W,A X,C X,E Y,B Y,D Z,G 1 Step :69.89499952458232 1 Step :82.96765068330782 1 Step :72.60540924733482 1 Step :69.70840302146416 1 Step :76.06770984905098 1 Step :12.126256419681935 1 Step :59.894337814749136 1 Step :62.324808431739996 2 Step :59.894337814749136 2 Step :12.126256419681935 2 Step :76.06770984905098 2 Step :69.70840302146416 2 Step :72.60540924733482 2 Step :82.96765068330782 2 Step :69.89499952458232 Last Step N:82.96765068330782 Last Step N:72.60540924733482 Last Step N:69.70840302146416 Last Step N:76.06770984905098 Last Step N:12.126256419681935 Last Step N:59.894337814749136 2 Step :62.324808431739996 1 Step :27.258965652231247 1 Step :63.79242930792434 Last Step N:69.89499952458232 2 Step :63.79242930792434 2 Step :27.258965652231247 Last Step N:62.324808431739996 Last Step N:27.258965652231247 Last Step N:63.79242930792434

 

最新回复(0)