posts - 325,  comments - 25,  trackbacks - 0

          public class TestStreamAPI1 {
           
           List<Employee> emps = Arrays.asList(
             new Employee(102, "李四", 59, 6666.66),
             new Employee(101, "張三", 18, 9999.99),
             new Employee(103, "王五", 28, 3333.33),
             new Employee(104, "趙六", 8, 7777.77),
             new Employee(104, "趙六", 8, 7777.77),
             new Employee(104, "趙六", 8, 7777.77),
             new Employee(105, "田七", 38, 5555.55)
           );
           
           //2. 中間操作
           /*
            映射
            map——接收 Lambda , 將元素轉換成其他形式或提取信息。接收一個函數作為參數,該函數會被應用到每個元素上,并將其映射成一個新的元素。
            flatMap——接收一個函數作為參數,將流中的每個值都換成另一個流,然后把所有流連接成一個流
            */
           @Test
           public void test1(){
            Stream<String> str = emps.stream()
             .map((e) -> e.getName());
            
            System.out.println("-------------------------------------------");
            
            List<String> strList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
            
            Stream<String> stream = strList.stream()
                .map(String::toUpperCase);
            
            stream.forEach(System.out::println);
            
            Stream<Stream<Character>> stream2 = strList.stream()
                .map(TestStreamAPI1::filterCharacter);
            
            stream2.forEach((sm) -> {
             sm.forEach(System.out::println);
            });
            
            System.out.println("---------------------------------------------");
            
            Stream<Character> stream3 = strList.stream()
                .flatMap(TestStreamAPI1::filterCharacter);
            
            stream3.forEach(System.out::println);
           }

           public static Stream<Character> filterCharacter(String str){
            List<Character> list = new ArrayList<>();
            
            for (Character ch : str.toCharArray()) {
             list.add(ch);
            }
            
            return list.stream();
           }
           
           /*
            sorted()——自然排序
            sorted(Comparator com)——定制排序
            */
           @Test
           public void test2(){
            emps.stream()
             .map(Employee::getName)
             .sorted()
             .forEach(System.out::println);
            
            System.out.println("------------------------------------");
            
            emps.stream()
             .sorted((x, y) -> {
              if(x.getAge() == y.getAge()){
               return x.getName().compareTo(y.getName());
              }else{
               return Integer.compare(x.getAge(), y.getAge());
              }
             }).forEach(System.out::println);
           }

          posted on 2018-03-06 08:38 長春語林科技 閱讀(123) 評論(0)  編輯  收藏 所屬分類: java8
          <2018年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 青川县| 格尔木市| 班戈县| 建始县| 黔西县| 榆树市| 渭南市| 阳西县| 雷波县| 多伦县| 绍兴县| 嘉祥县| 通许县| 普定县| 大足县| 巫山县| 洛隆县| 同德县| 泾源县| 民乐县| 江北区| 庐江县| 武川县| 盐源县| 漯河市| 鄢陵县| 嘉义市| 息烽县| 崇明县| 儋州市| 牙克石市| 丹凤县| 新乡市| 甘德县| 临城县| 周至县| 大洼县| 蓬安县| 哈巴河县| 义乌市| 铅山县|