Collectors - Demo1

原创转载请注明出处:http://agilestyle.iteye.com/blog/2425964

 

Outline

testGroupingByConcurrent();

testGroupingByConcurrentWithFunctionAndCollector();

testGroupingByConcurrentWithFunctionAndSupplierAndCollector();

testJoining();

testJoiningWithDelimiter();

testJoiningWithDelimiterAndPrefixAndSuffix();

testMapping();

testMaxBy();

testMinBy();

 

SRC

package org.fool.java8.collector;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.stream.Collectors;

public class CollectorsTest2 {

    private static List<Dish> menu = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", 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) {
        testGroupingByConcurrent();
        testGroupingByConcurrentWithFunctionAndCollector();
        testGroupingByConcurrentWithFunctionAndSupplierAndCollector();
        testJoining();
        testJoiningWithDelimiter();
        testJoiningWithDelimiterAndPrefixAndSuffix();
        testMapping();
        testMaxBy();
        testMinBy();
    }

    private static void testGroupingByConcurrent() {
        ConcurrentMap<Dish.Type, List<Dish>> map = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType));

        Optional.ofNullable(map.getClass()).ifPresent(System.out::println);
        Optional.ofNullable(map).ifPresent(System.out::println);
    }

    private static void testGroupingByConcurrentWithFunctionAndCollector() {
        ConcurrentMap<Dish.Type, Double> map = menu.stream().collect(Collectors.groupingByConcurrent(Dish::getType, Collectors.averagingInt(Dish::getCalories)));

        Optional.ofNullable(map).ifPresent(System.out::println);
    }

    private static void testGroupingByConcurrentWithFunctionAndSupplierAndCollector() {
        ConcurrentMap<Dish.Type, Double> map = menu.stream()
                .collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingInt(Dish::getCalories)));

        Optional.ofNullable(map.getClass()).ifPresent(System.out::println);

        Optional.ofNullable(map).ifPresent(System.out::println);
    }

    private static void testJoining() {
        Optional.ofNullable(menu.stream().map(Dish::getName).collect(Collectors.joining())).ifPresent(System.out::println);
    }

    private static void testJoiningWithDelimiter() {
        Optional.ofNullable(menu.stream().map(Dish::getName).collect(Collectors.joining(","))).ifPresent(System.out::println);
    }

    private static void testJoiningWithDelimiterAndPrefixAndSuffix() {
        Optional.ofNullable(menu.stream().map(Dish::getName).collect(Collectors.joining(",", "Name[", "]"))).ifPresent(System.out::println);
    }

    private static void testMapping() {
        Optional.ofNullable(menu.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(",")))).ifPresent(System.out::println);

        Optional.ofNullable(menu.stream().map(Dish::getName).collect(Collectors.joining(","))).ifPresent(System.out::println);
    }

    private static void testMaxBy() {
        Optional.ofNullable(menu.stream().collect(Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)))).ifPresent(System.out::println);

        Optional.ofNullable(menu.stream().max(Comparator.comparingInt(Dish::getCalories))).ifPresent(System.out::println);
    }

    private static void testMinBy() {
        Optional.ofNullable(menu.stream().collect(Collectors.minBy(Comparator.comparingInt(Dish::getCalories)))).ifPresent(System.out::println);

        Optional.ofNullable(menu.stream().min(Comparator.comparingInt(Dish::getCalories))).ifPresent(System.out::println);
    }

    public static class Dish {
        private String name;
        private boolean vegetarian;
        private int calories;
        private Type type;

        public Dish(String name, boolean vegetarian, int calories, Type type) {
            this.name = name;
            this.vegetarian = vegetarian;
            this.calories = calories;
            this.type = type;
        }

        public enum Type { MEAT, FISH, OTHER }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isVegetarian() {
            return vegetarian;
        }

        public void setVegetarian(boolean vegetarian) {
            this.vegetarian = vegetarian;
        }

        public int getCalories() {
            return calories;
        }

        public void setCalories(int calories) {
            this.calories = calories;
        }

        public Type getType() {
            return type;
        }

        public void setType(Type type) {
            this.type = type;
        }

        @Override
        public String toString() {
            return "Dish{" +
                    "name='" + name + '\'' +
                    ", vegetarian=" + vegetarian +
                    ", calories=" + calories +
                    ", type=" + type +
                    '}';
        }
    }
}

 Console Output

class java.util.concurrent.ConcurrentHashMap
{OTHER=[Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}], MEAT=[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}, Dish{name='chicken', vegetarian=false, calories=400, type=MEAT}], FISH=[Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}]}
{OTHER=387.5, MEAT=633.3333333333334, FISH=375.0}
class java.util.concurrent.ConcurrentSkipListMap
{MEAT=633.3333333333334, FISH=375.0, OTHER=387.5}
porkbeefchickenfrench friesriceseason fruitpizzaprawnssalmon
pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon
Name[pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon]
pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon
pork,beef,chicken,french fries,rice,season fruit,pizza,prawns,salmon
Optional[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}]
Optional[Dish{name='pork', vegetarian=false, calories=800, type=MEAT}]
Optional[Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}]
Optional[Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}]

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值