Stream流
Stream流的作用:
结合了Lambda表达式 简化集合、数组的操作
Stream流的使用步骤
- 先得到一条Stream流 并把数据放上去
- 利用Stream流中的API进行各种操作(如 过滤 转换 统计 打印等等)
- 中间方法: 方法调用完之后,还可以调用其他方法 如:过滤 转换
- 终结方法: 最后一步,调用完毕之后,不能调用其他方法 如:统计 打印
- 使用中间方法对流水线上的数据进行操作
- 使用终结方法对流水线上的数据进行操作
先得到一条Stream流 并把数据放上去
中间方法
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| ArrayList<String> list = new ArrayList<>(); Collections.addAll(list,"a","ab","bc");
list.stream().filter(new Predicate<String>() { @Override public boolean test(String s) { return s.startsWith("a"); } }).forEach(s -> System.out.println(s));
list.stream().filter(s -> s.startsWith("a")).forEach(s-> System.out.printl(s));
list.stream().limit(3) .forEach(s-> System.out.println(s)); list.stream().skip(4) .forEach(s-> System.out.println(s));
list.stream() .distinct().forEach(s-> System.out.println(s));
Stream.concat(list.stream(),list.stream()) .forEach(s-> System.out.println(s));
ArrayList<String> list1 = new ArrayList<>(); Collections.addAll(list1,"a-1","b-2","c-3");
list1.stream().map(new Function<String, Integer>() { @Override public Integer apply(String s) { String[] arr = s.split("-"); return Integer.parseInt(arr[1]); } }).forEach(s-> System.out.println(s));
list.stream().map(s-> Integer.parseInt(s.split("-")[1])) .forEach(s-> System.out.println(s));
|
终结方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| ArrayList<String> list = new ArrayList<>(); Collections.addAll(list,"a","ab","bc");
list.stream().filter(new Predicate<String>() { @Override public boolean test(String s) { return s.startsWith("a"); } }).forEach(s -> System.out.println(s));
list.stream().filter(s -> s.startsWith("a")).forEach(s-> System.out.printl(s));
list.stream().limit(3) .forEach(s-> System.out.println(s)); list.stream().skip(4) .forEach(s-> System.out.println(s));
list.stream() .distinct().forEach(s-> System.out.println(s));
Stream.concat(list.stream(),list.stream()) .forEach(s-> System.out.println(s));
ArrayList<String> list1 = new ArrayList<>(); Collections.addAll(list1,"a-1","b-2","c-3");
list1.stream().map(new Function<String, Integer>() { @Override public Integer apply(String s) { String[] arr = s.split("-"); return Integer.parseInt(arr[1]); } }).forEach(s-> System.out.println(s));
list.stream().map(s-> Integer.parseInt(s.split("-")[1])) .forEach(s-> System.out.println(s));
list.stream().forEach(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } });
list.stream().forEach(s-> System.out.println(s));
long count = list.stream().count(); System.out.println(count);
Object[] arr1 = list.stream().toArray(); System.out.println(arr1.toString());
String[] arr2 = list.stream().toArray(new IntFunction<String[]>() { @Override public String[] apply(int value) { return new String[value]; } }); System.out.println(Arrays.toString(arr2)); String[] arr3 = list.stream().toArray(value -> new String[value]); System.out.println(Arrays.toString(arr3));
ArrayList<String> list2 = new ArrayList<>(); Collections.addAll(list2,"a-男-1","b-女-2","c-男-3");
List<String> collect = list.stream().filter(s -> "男".equals(s.split("-")[1])) .collect(Collectors.toList()); System.out.println(collect);
Set<String> collect1 = list.stream().filter(s -> "男".equals(s.split("-")[1])) .collect(Collectors.toSet()); System.out.println(collect1);
Map<String, Integer> collect2 = list.stream() .filter(s -> "男".equals(s.split("-")[1]))
.collect(Collectors.toMap(new Function<String, String>() { @Override public String apply(String s) { return s.split("-")[0]; } }, new Function<String, Integer>() { @Override public Integer apply(String s) { return Integer.parseInt(s.split("-")[2]); } }));
Map<String, Integer> collect3 = list.stream() .filter(s -> "男".equals(s.split("-")[1])) .collect(Collectors.toMap( s -> s.split("-")[0], s -> Integer.parseInt(s.split("-")[2]))); System.out.println(collect3);
|