Stream3个阶段
创建Stream Stream中间处理 终止Steam
创建Stream方式
List < String > list = new ArrayList < > ( ) ;
list. stream ( ) ;
Stream . of ( list) ;
list. parallelStream ( ) ;
Stream . builder ( ) ;
Stream . generate ( ) ;
函数式基础接口
接口名 入参 返回 描述 Function T R 接受一个泛型 T 对象,返回一个泛型 R 对象,即参数类型和返回类型可以不同 Consumer T 无 接受一个泛型 类型参数,进行处理后无任何返回值,常用于消费数据 Predicate T boolean 接受一个泛型 参数,返回值为布尔类型**。Predicate 常用于数据过滤,如过滤出集合中符合某个条件的元素 Supplier 无 T 由于没有参数输入,所以多用于对象创建,类似于一个对象创建工 厂。可以使用 Lambda 方式创建任意对象,也可以使用对象构造方法 的方法引用创对象 Operator T T Function 函数接口的扩展
Stream中间处理
函数名 入参 返回 描述 filter Predicate Stream 按照条件过滤符合要求的元素, 返回新的stream流 map Function super T, ? extends R> Stream 将已有元素转换为另一个对象类型,一对一逻辑,返回新的stream流 mapToInt ToIntFunction super T, ? extends R> IntStream 将已有元素转换为另一个int对象类型,一对一逻辑,返回新的stream流 mapToLong ToLongFunction super T, ? extends R> LongStream 将已有元素转换为另一个Long对象类型,一对一逻辑,返回新的stream流 mapToDouble ToDoubleFunction super T, ? extends R> DoubleStream 将已有元素转换为另一个Double对象类型,一对一逻辑,返回新的stream流 flatMap Function super T, ? extends Stream extends R>> Stream 将已有元素转换为另一个int对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流 flatMapToInt Function super T, ? extends IntStream> IntStream 将已有元素转换为另一个对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流 flatMapToLong Function super T, ? extends LongStream> LongStream 将已有元素转换为另一个Long对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流 flatMapToDouble Function super T, ? extends DoubleStream> DoubleStream 将已有元素转换为另一个Double对象类型,一对多逻辑,即原来一个元素对象可能会转换为1个或者多个新类型的元素,返回新的stream流 distinct Stream 对Stream中所有元素进行去重,返回新的stream流(并行流效率低) sorted Stream 根据自然顺序进行排序 sorted Comparator super T> comparator Stream 对stream中所有的元素按照指定规则进行排序,返回新的stream流 peek Consumer super T> Stream 此方法主要用于支持调试,您希望在元素流过管道中的某个点时看到这些元素,返回新的stream流 limit long Stream 仅保留集合前面指定个数的元素,返回新的stream流(并行流效率低) skip long Stream 跳过集合前面指定个数的元素,返回新的stream流(并行流效率低)
终止Steam
函数名 入参 返回 描述 forEach Consumer super T> 无返回值,对元素进行逐个遍历,然后执行给定的处理逻辑 forEachOrdered 无返回值,始终遵循顺序,对元素进行逐个遍历,然后执行给定的处理逻辑 toArray Object[] 将流转换为数组 toArray IntFunction A[] 将流转换为指定类型数组 reduce T,BinaryOperator T 归约,有默认值或初始值,返回单一结果 reduce BinaryOperator Optional 归约, 返回单一结果Optional reduce U,BiFunction,BinaryOperator U 归约,有默认值或初始值,返回单一结果(并行流时第三参数生效) collect Collector super T, A, R> Collector 将流转换为指定的类型,通过Collectors进行指定 collect Supplier,BiConsumer,BiConsumer Supplier 将流转换为指定的类型,自定义Collector参数 min Comparator super T> T 返回stream处理后的元素最小值 max Comparator super T> T 返回stream处理后的元素最大值 count long 返回stream处理后最终的元素个数 anyMatch Predicate super T> boolean 返回一个boolean值,类似于isContains(),用于判断是否有符合条件的元素 allMatch Predicate super T> boolean 回一个boolean值,用于判断是否所有元素都符合条件 noneMatch Predicate super T> boolean 返回一个boolean值, 用于判断是否所有元素都不符合条件 findFirst T 找到第一个符合条件的元素时则终止流处理 findAny T 找到任何一个符合条件的元素时则退出流处理,这个对于串行流时与findFirst相同,对于并行流时比较高效 ,任何分片中找到都会终止后续计算逻辑
案例
public class XStream {
public static void main ( String [ ] args) {
List < String > a = new ArrayList < String > ( ) ;
a. add ( "1" ) ;
a. add ( "2" ) ;
a. add ( "2" ) ;
a. add ( "4" ) ;
a. add ( "3" ) ;
List < String > b = new ArrayList < String > ( ) ;
b. add ( "a" ) ;
b. add ( "b" ) ;
b. add ( "c" ) ;
b. add ( "d" ) ;
List < User > users = new ArrayList < > ( ) ;
User user1= new User ( "马超" , 18 ) ;
User user2= new User ( "小乔" , 16 ) ;
User user3= new User ( "典韦" , 27 ) ;
User user4= new User ( "诸葛亮" , 24 ) ;
User user5= new User ( "刘备" , 23 ) ;
users. add ( user1) ;
users. add ( user2) ;
users. add ( user3) ;
users. add ( user4) ;
users. add ( user5) ;
new XStream ( ) . maxUser ( users) ;
}
public void create ( ) {
List < String > list = new ArrayList < String > ( ) ;
list. add ( "1" ) ;
list. add ( "2" ) ;
list. add ( "3" ) ;
list. add ( "4" ) ;
Stream < String > stream = list. stream ( ) ;
Stream < List < String > > list1 = Stream . of ( list) ;
Stream < String > stringStream = list. parallelStream ( ) ;
Stream. Builder < String > builder = Stream . builder ( ) ;
builder. add ( "1" ) ;
builder. add ( "2" ) ;
builder. add ( "3" ) ;
builder. add ( "4" ) ;
Stream < String > build = builder. build ( ) ;
Supplier < String > supplier= ( ) -> "aaaa" ;
Stream < String > generate = Stream . generate ( supplier) . limit ( 3 ) ;
generate. forEach ( System . out:: println ) ;
}
public void filter ( List < String > list) {
Predicate < String > predicate= a-> ! a. equals ( 2 ) ;
Stream < String > stream = list. stream ( ) . filter ( predicate) ;
}
public void map ( List < String > list) {
Function < String , Integer > function= x-> Integer . valueOf ( x) ;
Stream < Integer > stream = list. stream ( ) . map ( function) ;
}
public void mapToInt ( List < String > list) {
ToIntFunction < String > function= x-> Integer . valueOf ( x) ;
IntStream intStream = list. stream ( ) . mapToInt ( function) ;
}
public void flatMap ( List < String > a, List < String > b) {
List < List < String > > ab= new ArrayList < > ( ) ;
ab. add ( a) ;
ab. add ( b) ;
List < String > collect = ab. stream ( ) . flatMap ( Collection :: stream ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
public void flatMapToInt ( List < String > a, List < String > b) {
List < List < String > > ab= new ArrayList < > ( ) ;
ab. add ( a) ;
ab. add ( b) ;
ArrayList < Integer > collect = ab. stream ( ) . flatMapToInt ( i -> i. stream ( ) . mapToInt ( Integer :: valueOf ) ) . collect ( ArrayList :: new , List :: add , List :: addAll ) ;
collect. forEach ( System . out:: println ) ;
}
public void distinct ( List < String > a) {
List < String > collect = a. stream ( ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
public void sorted ( List < String > a) {
List < String > collect = a. stream ( ) . sorted ( ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
public void sorted2 ( List < String > a) {
List < String > collect = a. stream ( ) . sorted ( Comparator . comparing ( x-> ! x. equals ( "2" ) ) ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
public void peek ( List < String > a) {
List < String > collect = a. stream ( ) . peek ( System . out:: println ) . sorted ( Comparator . comparing ( x-> ! x. equals ( "2" ) ) ) . peek ( System . out:: println ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
public void limit ( List < String > a) {
List < String > collect = a. stream ( ) . limit ( 2 ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
public void skip ( List < String > a) {
List < String > collect = a. stream ( ) . skip ( 2 ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
public void forEach ( List < String > a) {
a. stream ( ) . forEach ( System . out:: println ) ;
}
public void forEachOrdered ( List < String > a) {
a. parallelStream ( ) . forEachOrdered ( System . out:: println ) ;
System . out. println ( "-------------" ) ;
a. parallelStream ( ) . forEach ( System . out:: println ) ;
}
public void toArray ( List < String > a) {
Object [ ] objects = a. stream ( ) . toArray ( ) ;
System . out. println ( objects) ;
}
public void toArray2 ( List < String > a) {
String [ ] oarrs = a. stream ( ) . toArray ( String [ ] :: new ) ;
System . out. println ( oarrs) ;
}
public void reduce ( List < String > a) {
Optional < String > reduce = a. stream ( ) . reduce ( ( x, y) -> x. concat ( y) ) ;
System . out. println ( reduce. get ( ) ) ;
}
public void reduce2 ( List < String > a) {
String reduce = a. stream ( ) . reduce ( "a" , ( x, y) -> x. concat ( y) ) ;
System . out. println ( reduce) ;
}
public void reduce3 ( List < String > a) {
String reduce1 = a. stream ( ) . reduce ( "a" , ( x, y) -> x. concat ( y) , ( x, y) -> x. concat ( y) ) ;
System . out. println ( reduce1) ;
String reduce2 = a. parallelStream ( ) . reduce ( "a" , ( x, y) -> x. concat ( y) , ( x, y) -> x. concat ( y) ) ;
System . out. println ( reduce2) ;
}
public void collect ( List < String > a) {
List < String > collect = a. stream ( ) . collect ( Collectors . toList ( ) ) ;
}
public void collect2 ( List < String > a) {
BiConsumer < List < String > , String > accumulator= ( x, y) -> {
if ( y. equals ( "2" ) ) {
x. add ( y) ;
}
} ;
BiConsumer < List < String > , List < String > > combiner= ( x, y) -> {
List < String > collect = y. stream ( ) . map ( m -> "a" + m) . collect ( Collectors . toList ( ) ) ;
x. addAll ( collect) ;
} ;
List < String > collect = a. parallelStream ( ) . collect ( ArrayList :: new , accumulator, combiner) ;
collect. stream ( ) . forEach ( System . out:: println ) ;
}
public void min ( List < String > a) {
List < Integer > collect = a. stream ( ) . mapToInt ( Integer :: valueOf ) . collect ( ArrayList :: new , ArrayList :: add , ArrayList :: addAll ) ;
Optional < Integer > min = collect. stream ( ) . min ( Comparator . comparing ( x -> x) ) ;
System . out. println ( min. get ( ) ) ;
}
public void max ( List < String > a) {
List < Integer > collect = a. stream ( ) . mapToInt ( Integer :: valueOf ) . collect ( ArrayList :: new , ArrayList :: add , ArrayList :: addAll ) ;
Optional < Integer > max = collect. stream ( ) . max ( Comparator . naturalOrder ( ) ) ;
System . out. println ( max. get ( ) ) ;
}
public void maxUser ( List < User > a) {
Optional < User > max = a. stream ( ) . max ( Comparator . comparing ( x -> x. getAge ( ) ) ) ;
System . out. println ( max. get ( ) ) ;
}
public void count ( List < String > a) {
List < Integer > collect = a. stream ( ) . mapToInt ( Integer :: valueOf ) . collect ( ArrayList :: new , ArrayList :: add , ArrayList :: addAll ) ;
long count = collect. stream ( ) . count ( ) ;
System . out. println ( count) ;
}
public void anyMatch ( List < String > a) {
boolean b = a. stream ( ) . anyMatch ( m -> m. equals ( "2" ) ) ;
System . out. println ( b) ;
}
public void allMatch ( List < String > a) {
boolean b = a. stream ( ) . allMatch ( m -> ! m. equals ( "3" ) ) ;
System . out. println ( b) ;
}
public void noneMatch ( List < String > a) {
boolean b = a. stream ( ) . noneMatch ( m -> m. equals ( "7" ) ) ;
System . out. println ( b) ;
}
public void findFirst ( List < String > a) {
Optional < String > first = a. stream ( ) . findFirst ( ) ;
System . out. println ( first. get ( ) ) ;
}
public void findAny ( List < String > a) {
Optional < String > first = a. stream ( ) . findAny ( ) ;
System . out. println ( first. get ( ) ) ;
}
}
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218