jdk1.8 - Collectors of use

 

 

package com.collector;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.zpb.video_06.Dish;
import com.zpb.video_06.Dish.Type;

/**
 * @des        Collector API的使用
 * Three functions: aggregated packet statistics                           
 * @author   zhao 
 * @date 2019 Nian 9 22 afternoon 11:53:54 
 * 
 * / 
public  class CollectorsAPI { 
    
    public  static  Final List <Dish> the MENU =   Arrays.asList (
             new new Dish ( "Pork", to false , 800 , Dish.Type.MEAT),
             new new Dish ( "Beef", to false , 700 , Dish.Type.MEAT), 
             new new Dish ( "Chicken", to false , 400 , Dish.Type.MEAT) ,
             new new Dish ( "french Fries", to true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER), 
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 300, Dish.Type.FISH), 
            new Dish("salmon", false, 450, Dish.Type.FISH));
    
    public static void main(String[] args) {
        //1.求平均值
        testAveragingDouble();
        testaveragingInt();
        testAveragingLong();
        testCollectingAndThen();
        
        //2.统计
        testCounting();
        testGroupByFunction();
        testGroupByFunctionAndCollectors();
        testGroupByFunctionAndAndCollectors();
        testSummarizingInt();
    }
    
    private static void testSummarizingInt() {
        IntSummaryStatistics intSummary = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
        Optional.ofNullable(intSummary).ifPresent(System.out::println);
    }
    
    
    private static void testGroupByFunctionAndAndCollectors() {
        System.out.println("...testGroupByFunctionAndCollectors...");
        Map<Type, Double> map = menu.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.averagingDouble(Dish::getCalories)));
        Optional.ofNullable(map.getClass()).ifPresent(System.out::println);        //hashmap 
        //我们得到的是hashMap, 下面把它改成treeMap
        TreeMap<Type, Double> map2 = menu.stream().collect(Collectors.groupingBy(Dish::getType, TreeMap::new, Collectors.averagingDouble(Dish::getCalories)));
        Optional.ofNullable(map2.getClass()).ifPresent(System.out::println);
        
    }
    
    private static void testGroupByFunctionAndCollectors() {
        System.out.println("...testGroupByFunctionAndCollectors...");
        Optional.ofNullable(menu.stream().collect(Collectors.groupingBy(Dish::getType, Collectors.counting())))
        .ifPresent(System.out::println);
        
        //每个分类下卡路里平均值
        Optional.ofNullable(
        menu.stream().collect(Collectors.groupingBy(Dish::getType, Collectors.averagingDouble(Dish::getCalories))))
        .ifPresent(System.out::println);
    }
    private static void testGroupByFunction() {
        System.out.println("...testGroupByFunction...");
        Optional.ofNullable(menu.stream().collect(Collectors.groupingBy(Dish::getType)))
                .ifPresent(System.out::println);
    }
    
    private static void testCounting() {
        System.out.println("...testCounting...");
        Optional.ofNullable(menu.stream().collect(Collectors.counting())).ifPresent(System.out::println);
    }
    private static void testAveragingDouble() {
        System.out.println("...testAveragingDouble...");
        Optional.ofNullable(menu.stream().collect(Collectors.averagingDouble(Dish::getCalories)))
        .ifPresent(System.out::println);
    }
    
    private static void testaveragingInt() {
        System.out.println("...testaveragingInt...");
        Optional.ofNullable(menu.stream().collect(Collectors.averagingInt(Dish::getCalories)))
        .ifPresent(System.out::println);
    }
    
    private static void testAveragingLong() {
        System.out.println("...testAveragingLong...");
        Optional.ofNullable(menu.stream().collect(Collectors.averagingLong(Dish::getCalories)))
        .ifPresent(System.out::println);
    }
    private static voidtestCollectingAndThen () {
         / ** m.getType (). the equals (Type.MEAT))
         * collectingAndThen(Collector<T,A,R> downstream,Function<R,RR> finisher)
         * The first parameter is: Collector, which is to get the collection by Collectors 
         * The first two parameters are: Function, which is passed two parameters, R RR, returns RR 
         * The main method is to use good how parameters are passed in 
         * / 
        System.out.println ( "... testCollectingAndThen ..." ); 
        
        // 1. acquired average value then becomes the output string 
        Optional.ofNullable (menu.stream (). 
                the collect (Collectors.collectingAndThen (Collectors.averagingLong (Dish :: getCalories), A -> "AVG iS servingSize, calories" + A))) 
                .ifPresent (the System.out :: the println); 
        
        // 2. give a specified set, can not be others modified 
        List <Dish> = menu.stream the collect () filter (M->. 
            .collect (Collectors.collectingAndThen (Collectors.toList (), the Collections :: unmodifiableList));
        Optional.ofNullable(collect).orElseGet(ArrayList::new).forEach(System.out::println);
        
    }
    
}

 

package com.collector;

import java.awt.Menu;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.collector.CollectorsAPI;
import com.zpb.video_06.Dish;
import com.zpb.video_06.Dish.Type;
/**
 * @des                                  
 * @author  zhao
 * @date    2019年9月24日上午12:13:00
 * 
 */
public class CollectorsAPI2 {
    
    static List<Dish> menu = CollectorsAPI.menu;
    
    public static void main(String[] args) {
        
        testGroupingByConcurrent();
        testGroupingByConcurrentAndCollectors();
        testGroupingByConcurrentAndSkipAndCollectors();
        testJoining();
        testJoiningAndPrefixAndSuffix();
        testMapping();
    }
    
    public static void testMapping() {
        System.out.println(">>>>>>>>>>>>>>> testMapping  >>>>>>>>>>>>>>>");
        /**
         Mapping * (Function <? Super T,? The extends the U-> Mapper, Collector <? The U-Super, A, R & lt> downstream) { 
         * Results obtained in the first parameter as a second parameter of operation of the source data 
         * / 
        Optional.ofNullable (menu.stream () the collect (Collectors.mapping (Dish :: getName, Collectors.joining (. "," )))) 
                .ifPresent (the System.out :: the println); 
                
    } 
    
    // operatively connected 
    public  static  void testJoining ( ) { 
        System.out.println ( ">>>>>>>>>>>>>>> testJoining () >>>>>>>>>>>>>>>" ); 
        Optional.ofNullable (MENU .stream (). the Map (Dish :: getName) .collect (Collectors.joining ( "#")))
                .ifPresent(System.out::println);
    }
    public static void testJoiningAndPrefixAndSuffix() {
        System.out.println(">>>>>>>>>>>>>>> testJoiningAndPrefixAndSuffix()  >>>>>>>>>>>>>>>");
        Optional.ofNullable(menu.stream().map(Dish::getName).collect(Collectors.joining(",","Name[","]")))
        .ifPresent(System.out::println);
    }
    
    public static void testGroupingByConcurrentAndSkipAndCollectors() {
        System.out.println(">>>>>>>>>>>>>>> testGroupingByConcurrentAndSkipAndCollectors()  >>>>>>>>>>>>>>>");
        ConcurrentSkipListMap<Type, Double> skipListMap = 
            menu.stream().collect(Collectors.groupingBy(Dish::getType, ConcurrentSkipListMap::new,Collectors.averagingDouble(Dish::getCalories)));
    
        Optional.ofNullable(skipListMap.getClass()).ifPresent(System.out::println);
        Optional.ofNullable(skipListMap).ifPresent(System.out::println);
                
    }
    
    public static void testGroupingByConcurrentAndCollectors() {
        System.out.println(">>>>>>>>>>>>>>> testGroupingByConcurrentAndCollectors >>>>>>>>>>>>>>>");
        Optional.ofNullable(menu.stream().collect(Collectors.groupingBy(Dish::getType, Collectors.averagingDouble(Dish::getCalories)))) 
                .ifPresent(System.out::println);
    1. categorized by type, return type is: CurrentMap//
    }
    public static void testGroupingByConcurrent() {
        System.out.println(">>>>>>>>>>>>>>> testGroupingByConcurrent >>>>>>>>>>>>>>>");
        Optional.ofNullable(menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType)))
                .ifPresent(System.out::println);
    }
    
}

 

Guess you like

Origin www.cnblogs.com/MrRightZhao/p/11576067.html