| 
 | 1 | +package org.javacore.stream;  | 
 | 2 | + | 
 | 3 | +/*  | 
 | 4 | + * Copyright [2015] [Jeff Lee]  | 
 | 5 | + *  | 
 | 6 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 7 | + * you may not use this file except in compliance with the License.  | 
 | 8 | + * You may obtain a copy of the License at  | 
 | 9 | + *  | 
 | 10 | + *   http://www.apache.org/licenses/LICENSE-2.0  | 
 | 11 | + *  | 
 | 12 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 13 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 15 | + * See the License for the specific language governing permissions and  | 
 | 16 | + * limitations under the License.  | 
 | 17 | + */  | 
 | 18 | + | 
 | 19 | +import java.util.ArrayList;  | 
 | 20 | +import java.util.List;  | 
 | 21 | +import java.util.stream.Collectors;  | 
 | 22 | +import java.util.stream.Stream;  | 
 | 23 | + | 
 | 24 | +/**  | 
 | 25 | + * Stream API 集合的流式操作  | 
 | 26 | + *  | 
 | 27 | + * Created by bysocket on 16/7/13.  | 
 | 28 | + */  | 
 | 29 | +public class CollectionStreamTest {  | 
 | 30 | +    public static void main(String[] args) {  | 
 | 31 | +        List<String> list = new ArrayList<>();  | 
 | 32 | +        list.add("aa");  | 
 | 33 | +        list.add("cccc");  | 
 | 34 | +        list.add("bbb");  | 
 | 35 | + | 
 | 36 | +        /**  | 
 | 37 | +         * Stream的使用:  | 
 | 38 | +         *  创建/获取流 -> 中间操作(过滤、转换等) -> 终止操作( 聚合、收集结果)  | 
 | 39 | +         */  | 
 | 40 | +        list.stream().forEach(System.out::println);  | 
 | 41 | +        System.out.println();  | 
 | 42 | + | 
 | 43 | +        /**  | 
 | 44 | +         * 过滤  | 
 | 45 | +         *      collect语法 {@link StreamCollectTest}  | 
 | 46 | +         */  | 
 | 47 | +        List list0 = list.stream().filter(str -> str.startsWith("cc")).collect(Collectors.toList());  | 
 | 48 | +        List list1 = list.stream().filter(str -> str.startsWith("aa")).collect(Collectors.toList());  | 
 | 49 | + | 
 | 50 | +        list0.stream().forEach(System.out::println);  | 
 | 51 | +        list1.stream().forEach(System.out::println);  | 
 | 52 | +        System.out.println();  | 
 | 53 | + | 
 | 54 | +        /**  | 
 | 55 | +         * 转换  | 
 | 56 | +         */  | 
 | 57 | +        List list2 = list.stream().map(str -> str.replace("c","*")).collect(Collectors.toList());  | 
 | 58 | + | 
 | 59 | +        list2.stream().forEach(System.out::println);  | 
 | 60 | +        System.out.println();  | 
 | 61 | + | 
 | 62 | +        /**  | 
 | 63 | +         * 提取  | 
 | 64 | +         *      从skip开始至limit位置为止  | 
 | 65 | +         */  | 
 | 66 | +        List list3 = list.stream().skip(0).limit(1).collect(Collectors.toList());  | 
 | 67 | + | 
 | 68 | +        list3.stream().forEach(System.out::println);  | 
 | 69 | +        System.out.println();  | 
 | 70 | + | 
 | 71 | +        /**  | 
 | 72 | +         * 组合  | 
 | 73 | +         */  | 
 | 74 | +        List list4 = Stream.concat(list.stream(),list.stream()).collect(Collectors.toList());  | 
 | 75 | + | 
 | 76 | +        list4.stream().forEach(System.out::println);  | 
 | 77 | +        System.out.println();  | 
 | 78 | +    }  | 
 | 79 | +}  | 
0 commit comments