创建stream的几种方式:
5种方式:数组方式、集合方式、Stream.of方式、Stream.iterate方式、Stream.generate方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Test public void test(){ String[] str = {"abc","hql","etl","","hl_le","exit","abc"}; Stream<String> arrStream = Arrays.stream(str); Stream<String> listStream = Arrays.asList(str).stream(); Stream<String> stream = Stream.of(str); Stream<Integer> iterateStream = Stream.iterate(0, x-> x+2).limit(10); AtomicInteger i= new AtomicInteger(0); Stream<String> generateStream = Stream.generate(()->{ return str[i.getAndIncrement()]; }).limit(str.length); generateStream.forEach(System.out::println); }
|
stream的API方式说明:
| 中间操作 |
无状态 |
unordered、filter、map、mapToInt、mapToDouble、mapToLong、flatMap、flatMapToInt、flatMapToLong、flatMapToDouble、peek |
|
有状态 |
distinct、sorted、limit、skip |
| 结束操作 |
非短路操作 |
forEach、forEachOrdered、toArray、reduce、collect、max、min、count |
|
短路操作 |
anyMatch、allMatch、noneMatch、findFirst、findAny |
flat:处理多层List遍历方式,如下面示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Test public void test7(){ List<List<String>> param = new ArrayList<>(); List<String> a1 = Arrays.asList("1","2","3"); List<String> a2 = Arrays.asList("1","2","3"); List<String> a3 = Arrays.asList("1","2","3"); List<String> a4 = Arrays.asList("1","2","3"); param.add(a1); param.add(a2); param.add(a3); param.add(a4); List<String> collect = param.stream().flatMap(x -> x.stream()).collect(Collectors.toList()); System.out.println(Arrays.toString(collect.toArray())); }
|
peek:为数据遍历的中间操作
skip:为跳过前面多少数据的操作
reduce:为合并操作需要传原始参数,如下面示例代码
1 2 3 4 5 6
| @Test public void test8(){ Stream<String> stream1 = Stream.of("a", "b", "c", "d"); String concatStr = stream1.reduce("",String::concat); System.out.println(concatStr); }
|
collect:为将流中的元素转换为对应集合的操作,如下面示例代码
1 2 3 4 5 6 7 8 9 10 11 12
| @Test public void test9(){ Map<String, String> map1 = new HashMap() { { put("id", "templateId"); put("job_id", "jobId"); put("rule_id", "ruleId"); } }; List<String> keyList = map1.entrySet().stream().map(x->x.getKey()).collect(Collectors.toList()); System.out.println(Arrays.toString(keyList.toArray())); }
|
anyMatch(任一匹配)、allMatch(全部匹配)、noneMatch(全部不匹配)、findFirst(找第一个)、findAny(找任意一个)
函数式接口说明:
Supplier:表示只有返回没有输入
Consumer:表示只有输入没有返回
Function:表示既有输入又有输出,[[输入和输出类型可以不一样
UnaryOperator:表示既有输入又有输出,且输入和输出类型必须一致
BiFunction:表示2元输入参数又有输出返回,输入和输出类型可以不一样
Predicate:表示既有输入又有输出返回,且输出类型必须为布尔类型
示例如下:
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
| @Test public void applyTest(){ Supplier<Integer> supplier = ()->1; System.out.println("supplier demo: "+ supplier.get()); }
@Test
public void consumerTest(){ Consumer<Integer> consumer = i-> System.out.println("consumer demo:"+i); consumer.accept(1); }
@Test
public void functionTest(){ Function<Integer, String> function = i-> "function demo:"+i; System.out.println(function.apply(33)); }
@Test
public void unaryOperatorTest(){ UnaryOperator<String> unaryOperator = i->"unaryOperator demo:"+i; System.out.println(unaryOperator.apply("33")); }
@Test public void biFunctionTest(){ BiFunction<Integer, Integer, String> biFunction = (i,j)->"biFunction demo: i*j="+i*j; System.out.println(biFunction.apply(8,9)); }
|