目录
Stream流的终结方法
forEach
public class StreamDemo12 {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1,"张无忌","周芷若","赵敏","张强");
list1.stream().forEach(s-> System.out.println(s));
}
}
打印结果:
count
public class StreamDemo13 {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1,"张无忌","周芷若","赵敏","张强");
System.out.println(list1.stream().count());
}
}
打印结果:
toArray
public class StreamDemo14 {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1,"张无忌","周芷若","赵敏","张强");
//这种方式创建的数组的元素类型为Object
Object[] arr1 = list1.stream().toArray();
System.out.println(Arrays.toString(arr1));
//这种方式创建的数组的元素类型为String
String[] arr2 = list1.stream().toArray(value -> new String[value]);
System.out.println(Arrays.toString(arr2));
}
}
打印结果:
collect
由于collect的讲解内容过多,所以我单独写了一篇文章,如果想了解collect的相关知识,请阅读这篇文章:JavaSE-Stream流的终结方法_collect-CSDN博客