JAVA8-Collectors API:summingDouble、summingInt、testSummingLong、toCollection、toConcurrentMap、toList、toSet、toMap

JAVA8-Collectors API:summingDouble、summingInt、testSummingLong、toCollection、toConcurrentMap、toList、toSet、toMap

admin
2022-09-18 / 0 评论 / 99 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年09月18日,已超过591天没有更新,若内容或图片失效,请留言反馈。

JAVA8-Collectors API:summingDouble、summingInt、testSummingLong、toCollection、toConcurrentMap、toList、toSet、toMap

前置数据

    public static final 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));

范例:

1、summingDouble

    public static void testAveragingDouble(){
        System.out.println("testAveragingDouble");
        //用reduce聚合求和
        Optional.ofNullable(menu.stream().map(Dish::getCalories).reduce(Integer::sum)).get().ifPresent(System.out::println);
        //用collectors averagingDouble求平均值
        Optional.ofNullable(menu.stream().collect(averagingDouble(Dish::getCalories))).ifPresent(System.out::println);
    }

输出结果:

testSummingDouble
4200.0
4200

2、summingDouble

    public static void testSummingInt() {
        System.out.println("testSummingInt");
        Optional.ofNullable(menu.stream().collect(Collectors.summingInt(Dish::getCalories))).ifPresent(System.out::println);
    }

输出结果:

testSummingInt
4200

3、summingLong

    public static void testSummingLong() {
        System.out.println("testSummingLong");
        Optional.ofNullable(menu.stream().collect(Collectors.summingLong(Dish::getCalories))).ifPresent(System.out::println);
    }

输出结果:

testSummingLong
4200

4、toCollection 返回Collection子类

    public static void testToCollection() {
        System.out.println("testToCollection");
        Optional.ofNullable(menu.stream().collect(Collectors.toCollection(LinkedList::new))).ifPresent(System.out::println);
    }

输出结果:

testToCollection
[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}, 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}, Dish{name='prawns', vegetarian=false, calories=300, type=FISH}, Dish{name='salmon', vegetarian=false, calories=450, type=FISH}]

5、toConcurrentMap

    public static void testToConcurrentMap() {
        System.out.println("testToConcurrentMap");
        Optional.ofNullable(menu.stream().collect(Collectors.toConcurrentMap(Dish::getName,Dish::getCalories))).ifPresent(System.out::println);
    }

输出结果:

testToConcurrentMap
{season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300}

6、toConcurrentMap 按类型统计个数

    public static void testToConcurrentMapWithBinaryOperator() {
        System.out.println("testToConcurrentMapWithBinaryOperator");
        Optional.ofNullable(menu.stream().collect(Collectors.toConcurrentMap(Dish::getType,v->1L,(a,b)->a+b))).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });
    }

输出结果:

testToConcurrentMapWithBinaryOperator
{OTHER=4, MEAT=3, FISH=2}
class java.util.concurrent.ConcurrentHashMap

7、toConcurrentMap 按类型统计个数,返回指定类型Map

    public static void testToConcurrentMapWithBinaryOperatorAndSupplier() {
        System.out.println("testToConcurrentMapWithBinaryOperatorAndSupplier");
        Optional.ofNullable(menu.stream().collect(Collectors.toConcurrentMap(Dish::getType,v->1L,(a,b)->a+b, ConcurrentSkipListMap::new))).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });
    }

输出结果:

testToConcurrentMapWithBinaryOperatorAndSupplier
{MEAT=3, FISH=2, OTHER=4}
class java.util.concurrent.ConcurrentSkipListMap

8、toList 转成list

    public static void testTolist() {
        System.out.println("testTolist");
        Optional.ofNullable(menu.stream().filter(t->t.isVegetarian()).collect(Collectors.toList())).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });
    }

输出结果:

testTolist
[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}]
class java.util.ArrayList

9、toSett 转成Set

    public static void testToSet() {
        System.out.println("testToSet");
        Optional.ofNullable(menu.stream().filter(t->t.isVegetarian()).collect(Collectors.toSet())).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });
    }

输出结果:

testToSet
[Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}, Dish{name='rice', vegetarian=true, calories=350, type=OTHER}]
class java.util.HashSet

10、toMap

public static void testToMap() {
        System.out.println("testToMap");
        Optional.ofNullable(menu.stream().collect(Collectors.toMap(Dish::getName,Dish::getCalories))).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });

        //转成线程安全的map
        Optional.ofNullable(menu.stream().collect(Collectors.collectingAndThen(Collectors.toMap(Dish::getName, Dish::getCalories), Collections::synchronizedMap))).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });
    }

输出结果:

testToMap
{season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300}
class java.util.HashMap
{season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300}
class java.util.Collections$SynchronizedMap

11、toMap 按类型统计个数

public static void testToMapWithBinaryOperator() {
        System.out.println("testToMapWithBinaryOperator");
        Optional.ofNullable(menu.stream().collect(Collectors.toMap(Dish::getType,v->1L,(a,b)->a+b))).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });
    }

输出结果:

testToMapWithBinaryOperator
{MEAT=3, FISH=2, OTHER=4}
class java.util.HashMap

12、toMap 按类型统计个数,返回指定类型Ma ##p

    public static void testToMapWithBinaryOperatorAndSupplier() {
        System.out.println("testToMapWithBinaryOperatorAndSupplier");
        Optional.ofNullable(menu.stream().collect(Collectors.toMap(Dish::getType,v->1L,(a,b)->a+b, ConcurrentSkipListMap::new))).ifPresent(v->{
            System.out.println(v);
            System.out.println(v.getClass());
        });
    }

输出结果:

testToMapWithBinaryOperatorAndSupplier
{MEAT=3, FISH=2, OTHER=4}
class java.util.concurrent.ConcurrentSkipListMap
4

评论 (0)

取消