软件开发 网页设计网站,有链接的网站,wordpress增加文章目录,高要区住房和城乡建设局网站一、什么是Stream流 Stream是一种处理集合#xff08;Collection#xff09;数据的方式。Stream可以让我们以一种更简洁的方式对集合进行过滤、映射、排序等操作。 二、Stream流的使用步骤 先得到一条Stream流#xff0c;并把数据放上去利用Stream流中的API进行各种操作 中间…一、什么是Stream流 Stream是一种处理集合Collection数据的方式。Stream可以让我们以一种更简洁的方式对集合进行过滤、映射、排序等操作。 二、Stream流的使用步骤 先得到一条Stream流并把数据放上去利用Stream流中的API进行各种操作 中间操作 过滤filter去重distinct终止操作 统计打印 三、Stream流的特点 结构化处理Stream提供了一种结构化的方式来处理集合数据可以使用一系列的操作来处理数据而不是通过循环遍历集合。 链式操作Stream的操作可以进行链式调用形成一个操作流水线每个操作都会作用于前一个操作的结果上。 惰性求值Stream的操作是惰性求值的只有在终止操作时才会触发执行这样可以避免不必要的计算。 并行执行Stream可以以并行的方式执行操作可以充分利用多核处理器的优势提高程序的性能。 四、获取流的方式
获取方式方法名说明单列集合default StreamE stream()Collection中的默认方法双列集合无无法直接使用stream流数组public static T StreamT stream(T[] array)Arrays工具类中的静态方法一堆零散数据public staticT StreamT of(T...values)Stream接口中的静态方法
// 单列集合
ArrayListString list new ArrayList();
Collections.addAll(list, a, b, c, d, e, f);StreamString stream list.stream();
stream.forEach(s - System.out.println(s));
// 双列集合
HashMapString,Integer hm new HashMap();
hm.put(aaa, 111);
hm.put(bbb, 222);
hm.put(ccc, 333);
hm.put(ddd, 444);// 第一种
hm.keySet().stream().forEach(s - System.out.println(s));// 第二种
hm.entrySet().stream().forEach(s - System.out.println(s));
// 数组
int[] arr {1, 2, 3, 4, 5, 6, 7, 8, 9};Arrays.stream(arr).forEach(s- System.out.println(s));
// 一堆零散数据
// 必须是同一种数据
Stream.of(1,2,3,4,5,6,7).forEach(s- System.out.println(s));Stream.of(a,b,c,d,e).forEach(s- System.out.println(s));
五、Stream流的中间方法
StreamT filter(Predicate? super T predicate)过滤StreamT limit(long maxSize)获取前几个元素StreamT skip(long n)跳过前几个元素StreamT distinct()元素去重依赖hashcode和equals方法static T StreamT concat(Stream a,Stream b)合并a和b两个流为一个流StreamR map(FunctionT,R mapper)转换流中的数据类型 注意1中间方法返回新的Stream流原来的Stream流只能使用一次建议使用链式编程 注意2修改Stream流中的数据不会影响原来集合或者数组中的数据 ArrayListString list new ArrayList();
Collections.addAll(list, 张无忌, 周芷若, 赵敏, 张强, 张三丰, 张翠山, 张良, 王二麻子, 谢广坤);// filter 过滤
list.stream().filter(s - s.startsWith(张)).forEach(s - System.out.println(s));// limit 获取前几个元素
list.stream().limit(2).forEach(s - System.out.println(s));// skip 跳过前几个元素
list.stream().skip(2).forEach(s - System.out.println(s));
ArrayListString list1 new ArrayList();
Collections.addAll(list1, 张无忌, 张无忌, 张无忌, 张强, 张三丰, 张翠山, 张良, 王二麻子, 谢广坤);ArrayListString list2 new ArrayList();
Collections.addAll(list2, 周芷若, 赵敏);// distinct 元素去重依赖hashcode和equals方法
list1.stream().distinct().forEach(s - System.out.println(s));// concat 合并a和b两个流为一个流
Stream.concat(list1.stream(),list2.stream()).forEach(s - System.out.println(s));
ArrayListString list new ArrayList();
Collections.addAll(list, 张无忌-20, 周芷若-18, 赵敏-19, 张强-30, 张三丰-69, 张翠山-25, 张良-29, 王二麻子-54, 谢广坤-58);// StreamR map(FunctionT,R mapper) 转换流中的数据类型
// 第一个类型流中原本的数据类型
// 第二个类型要转成之后的类型
// apply的形参s依次表示流里面的每一个数据
// 返回值表示转换之后的数据//当map方法执行完毕后流上的数据就变成了整数
//随意在下面forEach当中s依次表示流里面的每一个数据这个数据现在是整数
list.stream().map(new FunctionString, Integer() {Overridepublic Integer apply(String s) {String[] arr s.split(-);return Integer.parseInt(arr[1]);}
}).forEach(s - System.out.println(s));System.out.println();// lambda表达式的形式
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)收集流中的数据放到集合中
ArrayListString list new ArrayList();
Collections.addAll(list, 张无忌, 周芷若, 赵敏, 张强, 张三丰, 张翠山, 张良, 王二麻子, 谢广坤);// void forEach(Consumer action) 遍历
list.stream().forEach(s - System.out.println(s));// long count() 统计
long count list.stream().count();
System.out.println(count);// toArray() 收集流中的数据放到数组中
Object[] obj list.stream().toArray();
System.out.println(Arrays.toString(obj));// IntFunction的泛型具体类型的数组
// apply的形参流中数据的个数要跟数组的长度保持一致
// apply的返回值具体类型的数组
// 方法体就是创建数组
String[] arr list.stream().toArray(new IntFunctionString[]() {Overridepublic String[] apply(int value) {return new String[value];}
});
System.out.println(Arrays.toString(arr));// lambda表达式的形式
String[] arr list.stream().toArray(value - new String[value]);
System.out.println(Arrays.toString(arr));
// collect(Collector collector) 收集流中的数据放到集合中
ArrayListString list new ArrayList();
Collections.addAll(list, 张无忌-男-32, 周芷若-女-18, 赵敏-女-19, 张强-男-26, 张三丰-男-45, 张翠山-男-24, 张良-男-23, 王二麻子-男-48, 谢广坤-男-56);// 收集到List集合
// 收集男性
ListString collect list.stream().filter(s - 男.equals(s.split(-)[1])).collect(Collectors.toList());
System.out.println(collect);// 收集到Set集合中
// 收集男性
// 会去重
SetString collect1 list.stream().filter(s - 男.equals(s.split(-)[1])).collect(Collectors.toSet());
System.out.println(collect1);// 收集到Map集合
// 收集男性,键姓名值年龄
// 键不能重复
/*
toMap:参数一表示键的生成规则参数二表示值的生成规则参数一Function泛型一表示流中每一个数据的类型泛型二表示map集合中键的数据类型apply形参依次表示流里面的每一个数据方法体生成键的代码返回值已经生成的键参数二Function泛型一表示流中每一个数据的类型泛型二表示map集合中值的数据类型apply形参依次表示流里面的每一个数据方法体生成键的代码返回值已经生成的键*/
MapString, Integer map list.stream().filter(s - 男.equals(s.split(-)[1])).collect(Collectors.toMap(new FunctionString, String() {Overridepublic String apply(String s) {return s.split(-)[0];}
}, new FunctionString, Integer() {Overridepublic Integer apply(String s) {return Integer.parseInt(s.split(-)[2]);}
}));
System.out.println(map);// lambda表达式的形式
MapString, Integer map1 list.stream().filter(s - 男.equals(s.split(-)[1])).collect(Collectors.toMap(s - s.split(-)[0],s - Integer.parseInt(s.split(-)[2])
));
System.out.println(map1); lambda表达式详解
文章转载自: http://www.morning.lgnrl.cn.gov.cn.lgnrl.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.rlbc.cn.gov.cn.rlbc.cn http://www.morning.fglxh.cn.gov.cn.fglxh.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.rxpp.cn.gov.cn.rxpp.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.rgrz.cn.gov.cn.rgrz.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.nrwr.cn.gov.cn.nrwr.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.pqkyx.cn.gov.cn.pqkyx.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn http://www.morning.cspwj.cn.gov.cn.cspwj.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.nwcgj.cn.gov.cn.nwcgj.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.qmzwl.cn.gov.cn.qmzwl.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.gtbjf.cn.gov.cn.gtbjf.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.xswrb.cn.gov.cn.xswrb.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.yhglt.cn.gov.cn.yhglt.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn