首页 > 其他分享 >Stream【数据流】的基本使用

Stream【数据流】的基本使用

时间:2022-12-10 10:55:25浏览次数:45  
标签:基本 val Stream System println String 数据流 stream out

Stream:数据流
中间方法: filter【过滤】 limit【获取前几个数据】 skip【跳过前几个数据】
distinct【去重】 concat【合并】 map【转换流中的数据类型】

终结方法: foreach 【遍历】 count【统计】 toArray【收集数据到数组中】
collect【收集数据到集合中】

注意点:中间方法返回一个新的Stream流,原本的Stream流只能使用一次,建议使用链式编程
修改Stream流中的数据,不会影响原来的数据

 

  1 package com.Lucky;
  2 
  3 import java.util.*;
  4 import java.util.function.Function;
  5 import java.util.function.IntFunction;
  6 import java.util.stream.Collectors;
  7 import java.util.stream.Stream;
  8 
  9 /*
 10    Stream:数据流
 11      中间方法:  filter【过滤】     limit【获取前几个数据】      skip【跳过前几个数据】
 12                distinct【去重】   concat【合并】             map【转换流中的数据类型】
 13 
 14      终结方法:  foreach 【遍历】   count【统计】    toArray【收集数据到数组中】
 15                collect【收集数据到集合中】
 16 
 17      注意点:中间方法返回一个新的Stream流,原本的Stream流只能使用一次,建议使用链式编程
 18             修改Stream流中的数据,不会影响原来的数据
 19  */
 20 public class StreamDemo1 {
 21     /**
 22      * 要求:  1.将姓张的开头的元素添加到新集合中
 23      *        2.将姓张的开头并且长度为3的元素添加到新集合中
 24      *        3.遍历打印最终结果
 25      */
 26 
 27 
 28     public static void main(String[] args) {
 29         //1.原始做法
 30         System.out.println("-------- //1.原始做法---------");
 31         ArrayList<String> arr1=new ArrayList<>();
 32         Collections.addAll(arr1,"张三","张无忌","张断三","唯易","王八蛋","赛亚人","张天");
 33 
 34 
 35         ArrayList<String> arr2=new ArrayList<>();
 36         for (String s : arr1) {
 37             if(s.startsWith("张")){  //以张开头的数据
 38                 arr2.add(s);
 39             }
 40         }
 41         ArrayList<String> arr3=new ArrayList<>();
 42         for (String m : arr2) {
 43             if(m.length()==3){  //以张开头的数据并且长度为3
 44                 arr3.add(m);
 45             }
 46         }
 47 
 48         System.out.println(arr1);
 49         System.out.println(arr2);
 50         System.out.println(arr3);
 51 
 52 
 53         System.out.println("-------- //2.Stream数据流做法---------");
 54         System.out.println("-------  filter【过滤】-----------");
 55         arr1.stream().filter(name->name.startsWith("张")).filter(name->name.length()==3)
 56                 .forEach(strName-> System.out.println(strName));
 57 
 58 
 59         System.out.println("-------  limit【获取前几个数据】-----------");
 60         arr1.stream().limit(5).limit(3)
 61                 .forEach(val-> System.out.println(val));
 62 
 63         System.out.println("-------  skip【跳过前几个数据】-----------");
 64          arr1.stream().skip(3).skip(3)
 65                         .forEach(val-> System.out.println(val));
 66 
 67         System.out.println("-------  distinct【去重】-----------");
 68         ArrayList<String> arr=new ArrayList<>();
 69         Collections.addAll(arr,"张三","王八蛋","赛亚人","王八蛋","赛亚人","张三","精英");
 70         arr.stream().distinct().forEach(val-> System.out.println(val));
 71 
 72         System.out.println("-------  concat【合并】-----------");
 73         Stream.concat(arr.stream(),arr1.stream()).forEach(val-> System.out.println(val));
 74 
 75 
 76         System.out.println("-------  map 【转换流中的数据类型】-----------");
 77         ArrayList<String> arrStr=new ArrayList<>();
 78         Collections.addAll(arrStr,"BUG=999+","王八蛋=666+","赛亚人=555-");
 79 /*
 80      String:原本流的数据类型
 81      Integer:要转换成的数据类型
 82  */
 83         arrStr.stream().map(new Function<String, Integer>() {
 84             @Override
 85             public Integer apply(String s) {
 86                 //将字符串转换成字符串数组
 87                 String[] strArr=s.split("=");
 88                 String tempVar=strArr[1]; //获取=之后的数据  例如:999+
 89                 //截取字符串
 90                 tempVar  = tempVar.substring(0,tempVar.length()-1);
 91                 //转换成int类型
 92                 int age=Integer.parseInt(tempVar);
 93 
 94                 return age;
 95             }
 96         }).forEach(val-> System.out.println(val));
 97 
 98 
 99         System.out.println("---//lamda表达式写法   转换成int类型----");
100         arrStr.stream().map(s->Integer.parseInt(s.split("=")[1].substring(0,3)))
101                 .forEach(val-> System.out.println(val));
102 
103 
104 
105 
106 
107  /////////////////////////////////////////////////////////////////////////////////////////////
108         System.out.println("===============终结方法=======================");
109         /**
110          *    终结方法:  foreach 【遍历】   count【统计】    toArray【收集数据到数组中】
111          *                collect【收集数据到集合中】
112          */
113         arr1.stream().forEach(strName-> System.out.println(strName));
114 
115         System.out.println(arr.stream().count());
116 
117         System.out.println("-------toArray------");
118         Object[] objects = arrStr.stream().toArray();
119         System.out.println(Arrays.toString(objects));
120 
121         /*
122            new IntFunction<具体类型的数组>
123            value :流中数据的个数
124            返回值 :具体类型的数组
125          */
126       String[] atr=  arr1.stream().toArray(new IntFunction<String[]>() {
127             @Override
128             public String[] apply(int value) {
129                 return new String[value];
130             }
131         });
132         System.out.println(Arrays.toString(atr));  //转换成String字符串打印出来
133 
134         //使用lamda表达式
135         String[] v=  arr1.stream().toArray(value-> new String[value]);
136         System.out.println(Arrays.toString(v));  //转换成String字符串打印出来
137 
138 
139         System.out.println("-------collect:------");
140 
141         ArrayList<String> OK=new ArrayList<>();
142         Collections.addAll(OK,"张三-男-500","王八蛋-男-60","唯易-男-22","小微-女-21","李四-男-300");
143 
144         //要求:将男的收集到list集合中
145         List<String> collectList = OK.stream().filter(val -> "男".equals(val.split("-")[1]))
146                                    .collect(Collectors.toList());
147         System.out.println(collectList);
148 
149         //要求:将男的收集到Set集合中【跟list集合不同的地方:自动去重】
150         Set<String> collectSet = OK.stream().filter(val -> "男".equals(val.split("-")[1]))
151                 .collect(Collectors.toSet());
152         System.out.println(collectSet);
153 
154 
155         System.out.println("-------注意点:要储存在Map集合中,键不能重复,不然就报错----------");
156         System.out.println("---初级:匿名内部类写法----");
157 
158         //要求:将男的收集到Map集合中【要确定哪个作为键,哪个作为值】
159         //     用姓名作为键,将年龄作为值        张三-男-500
160         Map<String, Integer> collectMap = OK.stream().filter(val -> "男".equals(val.split("-")[1]))
161                 .collect(Collectors.toMap(
162                         //参数一:定义键的生成规则
163                         // new Function<原本流中的数据类型, 键的数据类型>
164                         // apply(原本流中的数据类型 s)
165                         // return: 返回生成的键
166                         new Function<String, String>() {
167                             @Override
168                             public String apply(String s) {
169                                 String name = s.split("-")[0];
170                                 return name;
171                             }
172                         },
173                         //参数二:定义值的生成规则
174                         // new Function<原本流中的数据类型, 值的数据类型>
175                         // apply(原本流中的数据类型  流里面的每一个数据)
176                         // return: 返回生成的值
177                         new Function<String, Integer>() {
178                             @Override
179                             public Integer apply(String s) {
180                                 String age = s.split("-")[2];
181                                 return Integer.parseInt(age);
182                             }
183                         }));
184         System.out.println(collectMap);
185 
186 
187         System.out.println("---高级:lamda表达式写法----");
188         Map<String, Integer> collectLam = OK.stream().filter(val -> "男".equals(val.split("-")[1]))
189                 .collect(Collectors.toMap(ki ->ki.split("-")[0],
190                                           vi ->Integer.parseInt(vi.split("-")[2])));
191         System.out.println(collectLam);
192     }
193 }

 

标签:基本,val,Stream,System,println,String,数据流,stream,out
From: https://www.cnblogs.com/Lucky-only/p/16970932.html

相关文章