目录
forEach、count、toArray and collection
Stream概述
JDK 1.8 版本及其以上支持!!!
利用流水线思想对于集合,数组数据进行处理和操作。涉及到数据筛选,排序,转换类型,限制个数,最终处理....并且在处理数据的过程中,对于数据的原始空间没有任何的修改,不影响原始数据。
初爽Stream流
public static void main(String[] args) {
// 单列集合 default Stream<E> Stream() Collection中的默认方法
//1.单列集合获取Stream流
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"a", "b", "c", "d", "e");
//Stream
//获取到一条流水线,并把集合中的数据放到流水线上
Stream<String> stream1 = list.stream();
//使用终结方法打印一下流水线上的数据
stream1.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
//使用Stream流
list.stream().forEach(s -> System.out.println(s));
}
Stream流处理的对象
集合对象
Stream<T> stream();
集合对象调用可以直接获取对应当前集合存储元素的 Stream 流对象。数组
Stream<T> Arrays.Stream(T[] t);
利用 Arrays 工具类对当前需要按照 Stream 流方式操作的数据进行转换操作,根据当前数组的数据类型和数据存储情况,返回一个对应的 Stream 流对象
获取Stream流
单列集合 default Stream<E> Stream() Collection中的默认方法
双列集合 无 无法直接使用需要用setKey()转换成单列数组
数组 public static<T>Stream<T>Stream(T[] array) Arrays工具类中的静态方法
一堆零散数据 public static<T>Stream<T> of(T……va;ues) Stream接口中的静态方法
双列集合
//创建双列集合
HashMap<String, Integer> hm = new HashMap<>();
//添加数据
hm.put("aaa", 111);
hm.put("bbb", 222);
hm.put("ccc", 333);
hm.put("ddd", 444);
//第一种获取Stream流
hm.keySet().stream().forEach(s-> System.out.println(s));
//第二种获取Stream流
hm.entrySet().stream().forEach(s-> System.out.println(s));
数组
/*
数组 public static<T>Stream<T>Stream(T[] array) Arrays工具类中的静态方法
*/
//创建数组
int[] arr = {1,2,3,4,5,6,7};
String[] arr1 = {"a", "b", "c"};
//获取Stream流
Arrays.stream(arr).forEach(s-> System.out.println(s));
Arrays.stream(arr1).forEach(s-> System.out.println(s));
一堆零散数据
/*
一堆零散数据 public static<T>Stream<T> of(T……va;ues) Stream接口中的静态方法
*/
Stream.of(1,2,3,4,5).forEach(s-> System.out.println(s));
Stream.of("a","b","c","d","e").forEach(s-> System.out.println(s));
Stream流中间方法
注意1:中间方法,返回新的Stream流,原来的Stream流只能使用一次,建议使用链式编程
注意2:修改Stream流中的数据,不会影响原来集合或数组中的数据
Stream<T> filter(predicate <?super T>predicate) 过滤
Stream<T>limit(long maxSize) 提取前几个元素
Stream<T>skip(long n) 跳过前几个元素
Stream<T>distinct 元素去重依赖(hashCode和equals方法)
static<T> Stream<T>concat(Stream a,Stream b) 合并a和b两个流为一个流
Stream<T> map(Funcition<T , R > mapper) 转换流中的数据类型
filter、limit and skip
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌","张三丰","张翠山","张良","张怀民","王二麻子","谢广坤");
//filter 过滤 把张开头的留下其余的过滤不要
list.stream().filter(new Predicate<String>() {
@Override
public boolean test(String s) {
//如果返回值为True,表示当前数据留下
//如果返回值为False,表示当前数据舍弃不要
return s.startsWith("张");
}
}).forEach(s-> System.out.println(s));
list.stream().filter(s->s.startsWith("张")).forEach(s-> System.out.println(s));
//张无忌
//张三丰
//张翠山
//张良
//张怀民
System.out.println("-----------------------");
Stream<T>limit(long maxSize)// 提取前几个元素
Stream<T>skip(long n) // 跳过前几个元素
//"张无忌","张三丰","张翠山","张良","张怀民","王二麻子","谢广坤"
list.stream().limit(3).forEach(s -> System.out.println(s));
//张无忌
//张三丰
//张翠山
System.out.println("-----------------------");
list.stream().skip(3).forEach(s -> System.out.println(s));
//张怀民
//王二麻子
//谢广坤
distinct and concat
/*
Stream<T>distinct 元素去重依赖(hashCode和equals方法)
static<T> Stream<T>concat(Stream a,Stream b) 合并a和b两个流为一个流
*/
ArrayList<String> list1 = new ArrayList<>();
Collections.addAll(list1,"张无忌","张无忌","张无忌","张三丰","张翠山","张良","张怀民","王二麻子","谢广坤");
ArrayList<String> list2 = new ArrayList<>();
Collections.addAll(list2, "周诗若","赵梅");
list1.stream().distinct().forEach(s -> System.out.println(s));
//张无忌 张三丰 张翠山 张良 张怀民 王二麻子 谢广坤
Stream.concat(list1.stream(),list2.stream()).forEach(s -> System.out.println(s));
//张无忌 张无忌 张无忌 张三丰 张翠山 张良 张怀民 王二麻子 谢广坤 周诗若 赵梅
Stream.concat(list1.stream(),list2.stream()).distinct().forEach(s -> System.out.println(s));
//张无忌 张三丰 张翠山 张良 张怀民 王二麻子 谢广坤 周诗若 赵梅
Map
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张三丰-100","张翠山-29","张良-23","张怀民-16","王二麻子-90","谢广坤-56");
//匿名内部类方式
//String->int
//当map方法执行完毕后,流上的数据变成了整数
//所以在forEach当中,s依次表示六里面的每一个数据,这个数据现在是整数了
list.stream().map(new Function<String, Integer>() {//第一个类型,流中原本的数据类型
@Override //第二个类型,要转变之后的类型
public Integer apply(String s) { //apply的形参s:依次表示流里面的每一个数据
String[] arr = s.split("-");
String ageString = arr[1];
int age = Integer.parseInt(ageString);
return age; //返回值:表示转换之后的数据
}
}).forEach(s-> System.out.println(s));
//Stream流
list.stream()
.map(s -> Integer.parseInt(s.split("-")[1]))
.forEach(s-> System.out.println(s));
Stream流终结方法
void forEach(Consumer action) 遍历
long count() 统计
toArray() 收集流中数据放到数组中
collect(Collector collector) 收集流中数据放到集合中
forEach、count、toArray and collection
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list,"张无忌","张三丰","张翠山","张良","张怀民","王二麻子","谢广坤");
list.stream().forEach(s -> System.out.println(s));
//统计元素个数
long count = list.stream().count();
System.out.println(count);
//toArray
Object[] arr = list.stream().toArray();
System.out.println(Arrays.toString(arr));
//IntFunction的泛型,具体类型的数据
//apply的形参:流中数据个数,要跟数组的成都保持一致
//apply的返回值:具体类型的数组
//方法体:就是创建数组
//toArray方法的参数的作用:负责创建一个指定类型的数组
//toArray方法的底层:会依次得到流里的每一个数据,并把他放到数组中
//toArray方法的返回值:是一个装着流里面所有数据的数组
//匿名内部类形式
String[] arr = list.stream().toArray(new IntFunction<String[]>() {
@Override
public String[] apply(int value) {
return new String[value];
}
});
System.out.println(Arrays.toString(arr));
//Stream流形式
String[] arr = list.stream().toArray(value -> new String[value]);
System.out.println(Arrays.toString(arr));
以上就是stream流中常用的一些api,至于stream流的高级用法及源码分析将放在后续的文章中讲解。