lambda expressions classification

public class StreamTest {
    public static void main(String[] args) {
        createStream();
        getForEach();
        getMap();
        getFilter();
        getLimit ();
        getSorted();
        getParallel();
        getCollectors();
        getStatistics();
        getSumAll ();
    }

    /**
     * Generate a Stream
     */
    public static void createStream(){
        System.out.println("-------------------->>>>>>>>>>生成Stream");
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        filtered.forEach(System.out::println);
    }

    /**
     * 2.forEach
     */
    public static void getForEach(){
        System.out.println("-------------------->>>>>>>>>>forEach");
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);
    }

    /**
     * 3.map
     */
    public static void getMap(){
        System.out.println("-------------------->>>>>>>>>>map");
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        // 获取对应的平方数
        List<Integer> squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        squaresList.forEach(System.out::println);
    }

    /**
     * 4.filter
     */
    public static void getFilter(){
        System.out.println("-------------------->>>>>>>>>>filter");
        List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        // 获取空字符串的数量
        int count = (int) strings.stream().filter(string -> string.isEmpty()).count();
        System.out.println ( "the number of null string:" + COUNT);
    }

    /**
     * 5.limit
     * / 
    Public  static  void getLimit () {
        System.out.println("-------------------->>>>>>>>>>limit");
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);
    }

    /**
     * 6.sorted
     */
    public static void getSorted(){
        System.out.println("-------------------->>>>>>>>>>sorted");
        Random random = new Random();
        random.ints().limit(10).sorted().forEach(System.out::println);
    }

    /**
     * 7. Parallel (Parallel) Program
     */
    public static void getParallel(){
        System.out.println("-------------------->>>>>>>>>>parallel");
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        // 获取空字符串的数量
        int count = (int) strings.parallelStream().filter(string -> string.isEmpty()).count();
        System.out.println("parallel: " + count);
    }

    /**
     * 8.Collectors
     */
    public static void getCollectors(){
        System.out.println("-------------------->>>>>>>>>>Collectors");
        List<String> list = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        List<String> filtered = list.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println ( "filter the list:" + Filtered);
        String mergedString = list.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println ( "Merge string:" + mergedString);
    }

    /**
     * 9. Statistics Statistics
     */
    public static void getStatistics(){
        System.out.println("-------------------->>>>>>>>>>Statistics");
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
        System.out.println ( "The maximum number of list:" + stats.getMax ());
        System.out.println ( "minimum number list:" + stats.getMin ());
        System.out.println ( "sum of the number of all:" + stats.getSum ());
        System.out.println ( "Mean:" + stats.getAverage ());
    }

    /**
     * 10. The sum set of a property
     */
    public static void getSumAll(){
        System.out.println("-------------------->>>>>>>>>>Statistics");
        List<GsonFormatModel> numbers = new ArrayList<>();
        GsonFormatModel gsonFormatModel = new GsonFormatModel();
        gsonFormatModel.setFrom(111);
        numbers.add(gsonFormatModel);
        GsonFormatModel gsonFormatModel1 = new GsonFormatModel();
        gsonFormatModel1.setFrom(999);
        numbers.add(gsonFormatModel1);
        int  stats = numbers.stream().mapToInt(GsonFormatModel::getFrom).sum();
        System.out.println ( "sum of the number of all:" + stats);
    }



}

 

Guess you like

Origin www.cnblogs.com/heqiyoujing/p/11139949.html