一 遍历输出forEach
import java.util.stream.Stream; public class demo_forEach { public static void main(String[] args) { Stream<String> steam = Stream.of("a", "c", "b"); steam.forEach((name)-> System.out.println(name)); } }
二 字符过滤filter
Stream<String> stream = Stream.of("张三丰", "张无忌", "张翠山", "赵敏"); stream.filter((name)->name.startsWith("张")).forEach(name-> System.out.println(name));
三 类型转换map
Stream<String> stream1 = Stream.of("1", "2", "3"); stream1.map((String s)->Integer.parseInt(s)).forEach((num)-> System.out.println(num));
四 数量统计 count
Stream<String> stream3 = Stream.of("张三丰", "张无忌", "张翠山", "赵敏"); System.out.println(stream3.filter(name -> name.startsWith("张")).count());
五 截取前n个limit
Stream<String> stream4 = Stream.of("张三丰", "张无忌", "张翠山", "赵敏"); stream4.limit(2).forEach(name-> System.out.println(name));
六 跳过前n个skip
Stream<String> stream5 = Stream.of("张三丰", "张无忌", "张翠山", "赵敏"); stream5.skip(1).forEach(name-> System.out.println(name));
七 流的合并concat
Stream<String> stream6 = Stream.of("张三丰", "张无忌", "张翠山", "赵敏"); Stream<String> stream7 = Stream.of("1", "2", "3"); Stream.concat(stream6,stream7).forEach((String str)-> System.out.println(str));