• <nav id="wkkge"><strong id="wkkge"></strong></nav>
  • <menu id="wkkge"></menu>
  • Java8新特性
    Java9新特性

    Java8 Stream流的使用

     

     

            //2流的中間操作
            //2.1 流的篩選與切片
            //distinct()去掉重復的數據
            stream.distinct().forEach(System.out::println);
            System.out.println("-------------package com.wkcto.stream;
    
    import javax.sound.midi.Soundbank;
    import java.util.*;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    import java.util.stream.Stream;
    
    /**
     * 流的基本操作
     * Author : 動力節點老崔
     */
    public class Test02 {
        public static void main(String[] args) {
            List list = new ArrayList<>();
            Collections.addAll(list, "wkcto", "good", "hello", "jj", "xx", "jj", "jj","bjpowernode","xx", "good");
            //1)通過List集合獲得Stream流
            Stream stream = list.stream();
    
    ----------");
    
            //filter()過濾
    //        stream.filter(x->x.length() > 4).forEach(System.out::println);  //java.lang.IllegalStateException: 流已關閉, 流只能使用一次
            list.stream().filter(x->x.length() > 4).forEach(System.out::println);
            System.out.println("-----------------------");
    
            //sort()排序
            list.stream().sorted(String::compareTo).forEach(System.out::println);
            System.out.println("-----------------------");
    
            //limt()截斷操作
            list.stream().limit(5).forEach(System.out::println);
            System.out.println("-----------------------");
    
            //skip()跳過
            list.stream().skip(3).forEach(System.out::println);
            System.out.println("-----------------------");
    
            //2.2 map映射
            //map映射轉換元素, map方法接受一個函數, 把這個函數應用于每個元素上,并將它映射為一個新的元素
            list.stream()
                    .map(String::toUpperCase)   //為每個元素應用toUpperCase()把小寫轉換為大寫
                    .forEach(System.out::println);
            System.out.println("-----------------------");
    
            list.stream()
                    .map(String::length)
                    .map(len -> len + "  ")
                    .forEach(System.out::print);
            System.out.println();
            System.out.println("-----------------------");
    
            //轉換為數值流
            List integerList = Arrays.asList(54,1,78,90,345);
            IntStream intStream = integerList.stream().mapToInt(x -> x);
            intStream.forEach(x -> System.out.print( x + " "));
            System.out.println();
            System.out.println("-----------------------");
    
            //2.3 匹配與查找操作
            //allMatch()判斷流中所有的元素是否都匹配給定的謂詞
            System.out.println( list.stream().allMatch(s -> s.length() > 3 ) );     //false
            //anyMatch()判斷流中是否有某個元素可以匹配指定的謂詞
            System.out.println( list.stream().anyMatch(s -> s.equals("wkcto")));    //true
            //noneMathc()判斷流中的元素是否都沒有匹配指定謂詞的
            System.out.println( list.stream().noneMatch(s -> s.equals("jj")));      //false
    
            //查找
            System.out.println(list.stream().filter(s -> s.length() > 10).findAny().get() );
            System.out.println( list.stream().filter(s -> s.length() > 20).findFirst().orElse("不存在"));
    
            //3 Stream流的終端操作
            //3.1 forEach遍歷
            list.stream().forEach(System.out::println);
    
            //3.2 cout統計
            System.out.println( list.stream().filter(x->x.length() > 2).count());
    
            //3.3 reduce歸納合并
            Optional reduce = list.stream().reduce((s1, s2) -> s1 + "--" + s2);
            System.out.println(reduce.get());
            reduce.ifPresent(System.out::println);
    
            //數值操作
            List list2 = Arrays.asList(6,21,87,34,1,78,54);
            //求和,指定初始值
            System.out.println(list2.stream().reduce(0, Integer::sum) );
            //求和,沒有初始值
            System.out.println(list2.stream().reduce((x,y)->x+y).orElse(0) );
            //最值
            System.out.println(list2.stream().reduce(Math::max).get() );
            System.out.println(list2.stream().reduce(Math::min).get() );
    
            //3.4映射到數值流
            System.out.println(list2.stream().mapToInt(x->x).sum() );       //求和
            System.out.println(list2.stream().mapToInt(x->x).max().getAsInt() );    //最大值
            System.out.println(list2.stream().mapToInt(x->x).min().orElse(0) );     //最小值
            System.out.println(list2.stream().mapToDouble(x->x).average().getAsDouble());   //平均值
            System.out.println(list2.stream().max(Integer::compareTo).get());
            list2.stream().min(Integer::compareTo).ifPresent(System.out::println);
    
            //3.5 collect歸約
            //把stream流轉換為集合
            Set stringSet = list.stream().collect(Collectors.toSet());
            System.out.println( stringSet );
    
            //把Stream流轉換為數組
            Object[] objects = list.stream().toArray();
            System.out.println( Arrays.toString(objects));
            String[] toArray = list.stream().toArray(String[]::new);
            System.out.println(Arrays.toString(toArray));
    
            //Stream流轉換為字符串
            String collect = list.stream().collect(Collectors.joining(","));
            System.out.println(collect);
        }
    }
    

     

    全部教程
  • <nav id="wkkge"><strong id="wkkge"></strong></nav>
  • <menu id="wkkge"></menu>
  • 面对面棋牌游戏