常见的Strean流模型的使用方式

1.forEach

void forEach(Consumer<? super T> action);

//打印所有元素
String[] arr = {"张翠山", "张三丰", "张无忌", "马蓉", "王宝强"};
//将数组转换成stream
Stream<String> arr1 = Stream.of(arr);
//对数组进行过滤并且打印
arr1.forEach(name->System.out.println(name));


2.filter

Stream<T> filter(Predicate<? super T> predicate);

String[] arr = {"张翠山", "张三丰", "张无忌", "马蓉", "王宝强"};
//将数组转换成stream
Stream<String> arr1 = Stream.of(arr);
//对数组进行过滤并且打印
arr1.filter((String name)->{return name.startsWith("张");})
.forEach(name->System.out.println(name));


3.map

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

String[] str = {"5", "8", "1", "3", "2"};
Stream<String> str1 = Stream.of(str);
//调用map,map中是Function接口，在判断大于2的个数
str1.map((String s) -> {return Integer.parseInt(s);})
.forEach(i->System.out.println(i));


4.count

//统计字符串中大于2的个数
String[] str = {"5", "8", "1", "3", "2"};
//转换成stream流
Stream<String> str1 = Stream.of(str);
//调用map,map中是Function接口，在判断大于2的个数
long count = str1.map((String s) -> {return Integer.parseInt(s);})
.filter((Integer i) -> {return i > 2;}).count();


5.limit

String[] arr = {"a","b","c","d","e"};
//取出前三个元素
Stream<String> limit = Stream.of(arr).limit(3);


6.skip

String[] arr2 = {"喜羊羊", "美羊羊", "慢羊羊", "灰太狼","红太狼"};
//跳过前三个元素
Stream<String> skip = Stream.of(arr2).skip(3);


7.concat

static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)


Stream<String> stream1 = Stream.of("Hello");;
Stream<String> stream2 = Stream.of("World");
//将两个stream拼接起来
Stream<String> concat1 = Stream.concat(stream1, stream2);
concat1.forEach(s->System.out.println(s));