package Stream;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* stream 学习
*/
public class App {
/**
* 数组创建of
*/
static void gen1(){
String[] arr={"a","b","c","d"};
Stream<String> stream = Stream.of(arr);
}
/**
* 集合创建asList
*/
static void gen2(){
String[] arr={"a","b","c","d"};
List<String> list = Arrays.asList(arr);
Stream<String> stream = list.stream();
}
/**
* generate创建 无限制的流
*/
static void gen3(){
Stream<Integer> stream = Stream.generate(() -> 1);
}
/**
* iterate 创建 无限制的流
*/
static void gen4(){
Stream<Integer> stream = Stream.iterate(1, x -> x + 1);
stream.limit(10).forEach(System.out::println);
}
/**
* 通过其他方式创建
*/
static void gen5(){
String str="1234645";
IntStream stream = str.chars();
stream.forEach(System.out::println);
}
public static void main(String[] args) {
/**
* 中间操作演示
*/
//去重博客演示
// Arrays.asList(1.23,4,5,6,7,7,7,7).stream().distinct().forEach(System.out::println);
//排序博客演示
// Arrays.asList(1,23,4,5,6).stream().sorted((a,b)->b-a).forEach(System.out::println);
//截取博客演示
// Arrays.asList(1,2,34,56,1).stream().limit(1).forEach(System.out::println);
//跳跃博客演示
// Arrays.asList(1,23,34,5).stream().skip(1).forEach(System.out::println);
// 转换博客演示
// Arrays.asList("1","2","3","5").stream().mapToInt(x->Integer.valueOf(x)).forEach(System.out::println);
// Arrays.asList("1","2","3","5").stream().map(x->Integer.valueOf(x)).forEach(System.out::println);
// String[] strs = {"java8", "is", "easy", "to", "use"};
// Arrays.stream(strs).map(s -> s.split("")).distinct().collect(Collectors.toList()).forEach(System.out::println);
// Arrays.stream(strs).map(s -> s.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()).forEach(System.out::println);
//peek博客演示
// Arrays.asList("1","2","3","5").stream().peek(System.out::println).forEach(System.out::println);
/**
* 终止操作演示
* anyMatch表示,判断的条件里,任意一个元素成功,返回true
*
* allMatch表示,判断条件里的元素,所有的都是,返回true
*
* noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
*
* findFirst用于返回满足条件的第一个元素
*
* findAny相对于findFirst的区别在于,findAny不一定返回第一个,而是返回任意一个,比如我们希望返回任意一个专业为土木工程的学生,可以实现如下:
*
* 实际上对于顺序流式处理而言,findFirst和findAny返回的结果是一样的,至于为什么会这样设计,是因为在下一篇我们介绍的并行流式处理,当我们启用并行流式处理的时候,
* 查找第一个元素往往会有很多限制,如果不是特别需求,在并行流式处理中使用findAny的性能要比findFirst好。
*
* reduce 根据一定的规则将Stream中的元素进行计算后返回一个唯一的值。
*/
//forEach
// Arrays.asList("1","2","3","5").stream().forEach(System.out::println);
//计算 min
// Integer min = Arrays.asList(1, 3, 4, 61, 1).stream().min((a, b) -> a - b).get();
// System.out.println(min);
//计算 max
// Integer max = Arrays.asList(1, 3, 4, 61, 1).stream().max((a, b) -> a - b).get();
// System.out.println(max);
//计算 count
// long count = Arrays.asList(1, 3, 4, 61, 1).stream().count();
// System.out.println(count);
//计算 average
// double avg = Arrays.asList(1, 3, 4, 61, 1).stream().mapToLong(x -> Long.valueOf(x + "")).average().getAsDouble();
// System.out.println(avg);
//匹配 anyMatch anyMatch表示,判断的条件里,任意一个元素成功,返回true
// boolean b = Arrays.asList(1, 3, 4, 61, 1).stream().anyMatch(x -> x % 2 == 0);
// System.out.println(b);
//匹配 allMatch allMatch表示,判断条件里的元素,所有的都是,返回true
// boolean b = Arrays.asList(2, 4, 4, 8, 12).stream().allMatch(x -> x % 2 == 0);
// System.out.println(b);
//匹配 noneMatch noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
// boolean b = Arrays.asList(2, 4, 4, 8, 12).stream().noneMatch(x -> x % 2 != 0);
// System.out.println(b);
//匹配 findFirst
// Optional<Integer> first = Arrays.asList(1, 2, 3, 4, 5).stream().filter(x -> x % 2 == 0).findFirst();
// System.out.println(first.get());
//匹配 findAny
// Optional<Integer> any = Arrays.asList(1, 2, 3, 4, 5).stream().filter(x -> x % 2 == 0).findAny();
// System.out.println(any.get());
//汇聚 reduce
// Integer reduce = Arrays.asList(1, 2, 3, 4, 5).stream().reduce((a, b) -> a + b).get();
// System.out.println(reduce);
//收集器 toArray
// Integer[] integers = Arrays.asList(1, 2, 3, 4, 5).stream().filter(x -> x % 2 == 0).toArray(Integer[]::new);
// Stream.of(integers).forEach(System.out::println);
// System.out.println(integers);
//收集器 collect
// List<Integer> collect = Arrays.asList(1, 2, 3, 4, 5).stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
// System.out.println(collect);
/**
* 自我测试
*/
// boolean b1 = Arrays.asList(1, 2, 3, 4, 5, 6).stream().filter(x -> x % 2 == 0).sorted((a, b) -> b - a).mapToInt(x->x).anyMatch(x -> x % 2 == 0);
// System.out.println(b1);
// 过滤
// Arrays.asList(1, 2, 3, 4, 5).stream().filter(x->x%2==0).forEach(System.out::println);
// 求和
// int sum = Arrays.asList(1, 2, 3, 4, 5).stream().filter(x -> x % 2 == 0).mapToInt(x -> x).sum();
// System.out.println(sum);
// 最大
// Integer max = Arrays.asList(1, 2, 3, 4, 5).stream().max((a, b) -> a - b).get();
// System.out.println(max);
// 最小
// Integer min = Arrays.asList(1, 2, 3, 4, 5).stream().min((a, b) -> a - b).get();
//// System.out.println(min);
// 查找第一个
// Optional<Integer> any = Arrays.asList(1, 2, 3, 4, 5).stream().filter(x -> x % 2 == 0).findFirst();
// System.out.println(any.get());
// 排序完毕之后查找第一个
// Optional<Integer> any2 = Arrays.asList(1, 2, 3, 4, 5,6).stream().filter(x -> x % 2 == 0).sorted((a,b)->b-a).findFirst();
// System.out.println(any2.get());
//从1到50里面的所有偶数找出来,放到一个list里面 收集
// List<Integer> collect = Stream.iterate(1, x -> x + 1).limit(50).filter(x -> x % 2 == 0).collect(Collectors.toList());
// System.out.println(collect);
// 去重
// Arrays.asList(1, 2, 2, 4, 2).stream().distinct().forEach(System.out::println);
// Set<Integer> collect = Arrays.asList(1, 2, 2, 4, 2).stream().collect(Collectors.toSet());
// System.out.println(collect);
// 截取
// List<Integer> collect = Stream.iterate(1, x -> x + 1).limit(50).sorted((a, b) -> b - a).skip(10).limit(10).collect(Collectors.toList());
// System.out.println(collect);
// String string = "11,22,33,44,55";
// int sum = Stream.of(string.split(",")).map(x -> Integer.valueOf(x)).mapToInt(x -> x).sum();
// System.out.println(sum);
// String str="tom,dada,test";
// Stream.of(str.split(",")).map(x->new User(x)).forEach(System.out::println);
// String string = "11,22,33,44,55";
// int sum = Stream.of(string.split(",")).peek(System.out::println).map(x -> Integer.valueOf(x)).mapToInt(x -> x).sum();
// System.out.println(sum);
}
}
/**
* 用于自我测试
*/
class User{
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
}
package Stream;
import java.util.Optional;
import java.util.stream.Stream;
/**
* 演示并行流和顺序流
*/
public class TaskApp {
public static void main(String[] args) {
/**
* parallel() 开启并行流
*/
Optional<Integer> max= Stream.iterate(1,x->x+1).limit(200).parallel().peek(x->{
System.out.println(Thread.currentThread().getName());
}).max(Integer::compare);
/**
* sequential() 顺序流
*/
Optional<Integer> min= Stream.iterate(1,x->x+1).limit(200).sequential().peek(x->{
System.out.println(Thread.currentThread().getName());
}).max(Integer::compare);
}
}