package test;
import java.util.ArrayList;
import java.util.List;
public class StreamTest {
static List < Integer > myList = new ArrayList < > ();
public static void main(String[] args) {
for (int i = 0; i < 5000000; i++)
myList.add(i);
int result = 0;
long loopStartTime = System.currentTimeMillis();
for (int i: myList) {
if (i % 2 == 0)
result += i;
}
long loopEndTime = System.currentTimeMillis();
System.out.println(result);
System.out.println("Loop total Time = " + (loopEndTime - loopStartTime));
long streamStartTime = System.currentTimeMillis();
System.out.println(myList.stream().filter(value -> value % 2 == 0).mapToInt(Integer::intValue).sum());
long streamEndTime = System.currentTimeMillis();
System.out.println("Stream total Time = " + (streamEndTime - streamStartTime));
long parallelStreamStartTime = System.currentTimeMillis();
System.out.println(myList.parallelStream().filter(value -> value % 2 == 0).mapToInt(Integer::intValue).sum());
long parallelStreamEndTime = System.currentTimeMillis();
System.out.println("Parallel Stream total Time = " + (parallelStreamEndTime - parallelStreamStartTime));
}
}
820084320 Loop total Time = 17 820084320 Stream total Time = 81 820084320 Parallel Stream total Time = 30
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamLazinessTest {
/** Employee class **/
static class Employee {
int id;
String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return this.name;
}
}
public static void main(String[] args) throws InterruptedException {
List < Employee > employees = new ArrayList < > ();
/** Creating the employee list **/
for (int i = 1; i < 10000000; i++) {
employees.add(new StreamLazinessTest.Employee(i, "name_" + i));
}
/** Only Intermediate Operations; it will just create another streams and
* will not perform any operations **/
Stream < String > employeeNameStreams = employees.parallelStream().filter(employee -> employee.id % 2 == 0)
.map(employee -> {
System.out.println("In Map - " + employee.getName());
return employee.getName();
});
/** Adding some delay to make sure nothing has happen till now **/
Thread.sleep(2000);
System.out.println("2 sec");
/** Terminal operation on the stream and it will invoke the Intermediate Operations
* filter and map **/
employeeNameStreams.collect(Collectors.toList());
}
}
StreamShortCircuitTest.java:
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamShortCircuitTest {
/** Employee class **/
static class Employee {
int id;
String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
}
public static void main(String[] args) throws InterruptedException {
List < Employee > employees = new ArrayList < > ();
for (int i = 1; i < 10000000; i++) {
employees.add(new StreamShortCircuitTest.Employee(i, "name_" + i));
}
/** Only Intermediate Operations; it will just create another streams and
* will not perform any operations **/
Stream < String > employeeNameStreams = employees.stream().filter(e -> e.getId() % 2 == 0)
.map(employee -> {
System.out.println("In Map - " + employee.getName());
return employee.getName();
});
long streamStartTime = System.currentTimeMillis();
/** Terminal operation with short-circuit operation: limit **/
employeeNameStreams.limit(100).collect(Collectors.toList());
System.out.println(System.currentTimeMillis() - streamStartTime);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有