记得复制到你的编译器里面瞅瞅,好多方法的原始写法我注释掉了
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class Stream_List {
public static void main(String[] args) {
// method_Stream_List();
// method_Stream_of();
// method_Stream_01();
// method_Stream_map();
// method_Stream_limit();
// method_Stream_skip();
method_Stream_concat();
}
public static void method_Stream_List() {
List<String> arrayList = new ArrayList<>();
arrayList.add("张无忌");
arrayList.add("周芷若");
arrayList.add("赵敏");
arrayList.add("张强");
arrayList.add("张三丰");
// 将list集合转换为stream,然后链式编程进行过滤,最后进行输出
arrayList.stream()
.filter(n -> n.startsWith("张"))
.filter(n -> n.length() == 3)
.forEach(n -> System.out.println(n));
}
public static void method_Stream_of() {
// stream arr 将字符串传入流中
Stream<String> stringStream = Stream.of("张三", "李四", "王五", "赵六", "田七");
// ergodic stream 遍历流中的数据
/* stringStream.forEach((String name) -> {
System.out.println(name);
});*/
// optimization 1 after 优化第一次
/* stringStream.forEach((name -> System.out.println(name)));*/
// optimization 2 after 优化第二次
stringStream.forEach(System.out::println);
}
public static void method_Stream_01() {
Stream<String> stringStream = Stream.of("张三", "李四", "王五", "赵六", "田七");
// 筛选流中的数据并进行遍历
/*stringStream.filter(s -> s.startsWith("张")).forEach(name -> System.out.println(name));*/
stringStream.filter(s -> s.startsWith("张")).forEach(System.out::println);
}
// dome stream map method使用map方法
public static void method_Stream_map() {
Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5");
// get stream 'String number' to 'Integer number'
// 字符串数字转换为Integer类型
// Stream<Integer> integerStream = stringStream.map(s -> Integer.parseInt(s));
// 优化写法
Stream<Integer> integerStream = stringStream.map(Integer::parseInt);
// ergodic stream
// 遍历流内的数据
// integerStream.forEach(i -> System.out.println(i + "\t This is ->\t" + i.getClass().getName()));
integerStream.forEach(System.out::println);
}
// dome stream limit method 测试limit方法
public static void method_Stream_limit() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("abc1");
arrayList.add("abc2");
arrayList.add("abc3");
arrayList.add("abc4");
arrayList.add("abc5");
arrayList.add("abc6");
Stream<String> stream = arrayList.stream();
// 读取前4个元素
stream.limit(4).forEach(System.out::println);
}
public static void method_Stream_skip() {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("abc01");
arrayList.add("abc02");
arrayList.add("abc03");
arrayList.add("abc04");
arrayList.add("abc05");
arrayList.add("abc06");
Stream<String> stream = arrayList.stream();
// 跳过前4个元素,遍历后几个元素
stream.skip(4).forEach(System.out::println);
}
public static void method_Stream_concat() {
Stream<String> str1 = Stream.of("wo shi ");
Stream<String> str2 = Stream.of("sh u ai ge !!");
// 连接str1和str2
Stream<String> concat_str = Stream.concat(str1, str2);
// print stream 打印流 方法1
// concat_str.forEach(System.out::print);
// 将流中的字符串转换为char数组 对数组进行遍历
Stream<char[]> stream = concat_str.map(String::toCharArray);
// chars is a array -> chars是一个数组
stream.forEach(chars -> {
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
System.out.print(aChar);
}
});
// 对char数组stream中的进行遍历
// stream.forEach(System.out::println);
}
}