Commit e873e328 by 杨林龙

添加stream流测试用例

1 parent 5219ac09
package cn.com.glsx.cn.com.glsx;
import java.util.concurrent.*;
/**
* @author Yanglinlong
* @date 2024/2/21 17:34
*/
public class test01 {
public static void main(String[] args) {
// Create a ForkJoinPool with default configuration
ForkJoinPool pool = new ForkJoinPool();
// Create a task that generates Fibonacci numbers
FibonacciTask task = new FibonacciTask(30L);
// Submit the task to the pool
pool.submit(task);
// Wait for the task to complete
pool.shutdown();
try {
pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Print the result
System.out.println(task.compute());
}
static class FibonacciTask extends RecursiveTask<Long> {
private final Long n;
public FibonacciTask(Long n) {
this.n = n;
}
@Override
protected Long compute() {
if (n <= 1) {
// Base case: return the value directly
return n;
} else {
// Divide the task into two smaller tasks
FibonacciTask leftTask = new FibonacciTask(n - 1);
FibonacciTask rightTask = new FibonacciTask(n - 2);
// Fork the tasks to the pool
leftTask.fork();
rightTask.fork();
// Join the tasks and return the sum of their results
return leftTask.join() + rightTask.join();
}
}
}
}
package cn.com.glsx.cn.com.glsx;
import cn.hutool.json.JSONUtil;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Yanglinlong
* @date 2024/2/21 17:44
*/
public class test02 {
public static void main(String[] args) {
Stream<Integer> numbers = Stream.of(1, 2, 3, 4, 5);
Optional<Integer> first = numbers.findFirst();
System.out.println("first = " + first.get());
Stream<Integer> numbers01 = Stream.of(1, 2, 3, 4, 5);
Optional<Integer> any = numbers01.findAny();
System.out.println("any = " + any.get());
Stream<Integer> numbers02 = Stream.of(1, 2, 3, 4, 5);
// numbers02.forEach(System.out::println);
Integer integer = numbers02.map(i -> i * 2).collect(Collectors.toList()).stream().peek(i -> System.out.println("i = " + i)).reduce(1, Integer::sum);
System.out.println("integer = " + integer);
Stream<Integer> numbers03 = Stream.of(1, 2, 3, 4, 5);
List<Integer> collect = numbers03.map(i -> i * 2).collect(Collectors.toList()).stream().peek(i -> System.out.println("i = " + i)).flatMap(i -> Stream.of(6, 7, 8, 9, 10)).collect(Collectors.toList());
System.out.println("integer01 = " + JSONUtil.toJsonStr(collect));
Stream<Integer> numbers04 = Stream.of(1, 2, 3, 4, 5);
List<Integer> collect01 = numbers04.map(i -> i * 2).collect(Collectors.toList()).stream().peek(i -> System.out.println("i = " + i)).sorted(Integer::compareTo).flatMap(i -> Stream.of(6, 7, 8, 9, 10)).collect(Collectors.toList());
System.out.println("collect01 = " + JSONUtil.toJsonStr(collect01));
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!