java8之后 parallel()与sequential()切换并行流与并行流
package Stream; import java.time.Duration; import java.time.Instant; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.RecursiveTask; import java.util.stream.LongStream; /** * @autour zdc * @create 2020-03-24-23:53 */ public class ForkJoinCaculate extends RecursiveTask<Long> { private static final long serialVersionUID =1222222L; private Long start; private long end; private static long THESHOLD = 10000L; public ForkJoinCaculate(Long start, long end) { this.start = start; this.end = end; } @Override protected Long compute() { long length = end-start; if (length<THESHOLD){ long sum = 0; for (long i = start; i <=end ; i++) { sum+=i; } return sum; } else { Long mid = (start+end)/2; ForkJoinCaculate left = new ForkJoinCaculate(start, mid); left.fork(); ForkJoinCaculate right = new ForkJoinCaculate(mid + 1, end); right.fork(); return left.join()+right.join(); } } public static void main(String[] args) { Instant start = Instant.now(); ForkJoinPool pool = new ForkJoinPool(); ForkJoinTask<Long> task = new ForkJoinCaculate(0l,1000000000l); Long sum = pool.invoke(task); System.out.println(sum); Instant end = Instant.now(); System.out.println(Duration.between(start,end).toMillis()); Instant start1 = Instant.now(); sum=0l; for (Long i = 0l; i <=1000000000l; i++) { sum+=i; } System.out.println(sum); Instant end1 = Instant.now(); System.out.println(Duration.between(start1,end1).toMillis()); //java8之后 parallel()与sequential()切换并行流与并行流 long sum1 = LongStream.rangeClosed(0, 1000000000).reduce(0, Long::sum); long sum2 = LongStream.rangeClosed(0, 1000000000).parallel().reduce(0, Long::sum); long sum3 = LongStream.rangeClosed(0, 1000000000).sequential().reduce(0, Long::sum); System.out.println(sum2); } }
Optional
@Test public void test3() { //Optional是容器 能快速锁定空指针异常 避免空指针异常带来的麻烦 Optional<Person> op = Optional.of(new Person("zzz", 33, 445d)); //Optional<Person> op1 = Optional.of(null); //报异常 Optional<Person> op3 = Optional.empty();//创建空的实例 //System.out.println(op3.get());//报异常 //传过来null构建空实例 传过来对象构建 是of和empty的综合体 Optional<Person> op4 = Optional.ofNullable(null); Optional<Person> op5 = Optional.ofNullable(new Person("zz",22,11.3)); if(op3.isPresent()){ System.out.println("op3w不为空"); }else { System.out.println("nulll"); } //op3为空则返回给定的值 orElse(T t) Person zzz = op3.orElse(new Person("zzz", 22, 22.2)); //orElseGet(Supplier s) 返回s给的值 op3.orElseGet(Person::new);//返回用空参数构造器 因为是Supplier接口 //map 如果有值 对其处理 Optional<String> s = op5.map(p -> p.getName()); //flatMap返回Optional Optional<String> s1 = op5.flatMap(p -> Optional.of(p.getName())); }
解决空指针异常:
package Stream; /** * @autour zdc * @create 2020-03-25-01:00 */ public class Dog { private String ball; public Dog() { } public Dog(String ball) { this.ball = ball; } public String getBall() { return ball; } public static void main(String[] args) { Dog dog = new Dog(); dog.getBall(); //发生空指针异常 System.out.println(dog.getBall().length()); //解决方法 if(dog!=null){ String ball = dog.getBall(); if(ball!=null) System.out.println(ball.length()); } } }
换成Optional
package Stream; import javax.swing.text.html.Option; import java.util.Optional; /** * @autour zdc * @create 2020-03-25-01:00 */ public class Dog { //注意!!! private Optional<String> ball= Optional.empty(); public Dog() { } public Dog(Optional<String> ball) { this.ball = ball; } public Optional<String> getBall() { return ball; } public static void main(String[] args) { Optional<Dog> dog= Optional.ofNullable(null); String s = dog.orElse(new Dog()).getBall().orElse("默认的球"); System.out.println(s); } }