原创转载请注明出处:http://agilestyle.iteye.com/blog/2425966
Outline
testSummingDouble();
testSummingLong();
testSummingInt();
testToCollection();
testToConcurrentMap();
testToConcurrentMapWithBinaryOperator();
testToConcurrentMapWithBinaryOperatorAndSupplier();
testToList();
testToSet();
testToMap();
testToMapWithBinaryOperator();
testToMapWithBinaryOperatorAndSupplier();
SRC
package org.fool.java8.collector;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.stream.Collectors;
public class CollectorsTest4 {
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) {
testSummingDouble();
testSummingLong();
testSummingInt();
testToCollection();
testToConcurrentMap();
testToConcurrentMapWithBinaryOperator();
testToConcurrentMapWithBinaryOperatorAndSupplier();
testToList();
testToSet();
testToMap();
testToMapWithBinaryOperator();
testToMapWithBinaryOperatorAndSupplier();
}
private static void testSummingDouble() {
Optional.of(menu.stream().collect(Collectors.summingDouble(Dish::getCalories))).ifPresent(System.out::println);
Optional.of(menu.stream().mapToDouble(Dish::getCalories).sum()).ifPresent(System.out::println);
}
private static void testSummingLong() {
Optional.of(menu.stream().collect(Collectors.summingLong(Dish::getCalories))).ifPresent(System.out::println);
Optional.of(menu.stream().mapToLong(Dish::getCalories).sum()).ifPresent(System.out::println);
}
private static void testSummingInt() {
Optional.of(menu.stream().collect(Collectors.summingInt(Dish::getCalories))).ifPresent(System.out::println);
Optional.of(menu.stream().mapToInt(Dish::getCalories).sum()).ifPresent(System.out::println);
}
private static void testToCollection() {
LinkedList<Dish> collect = menu.stream().filter(d -> d.getCalories() > 600).collect(Collectors.toCollection(LinkedList::new));
Optional.of(collect).ifPresent(System.out::println);
}
private static void testToConcurrentMap() {
Optional.of(menu.stream().collect(Collectors.toConcurrentMap(Dish::getName, Dish::getCalories))).ifPresent(System.out::println);
}
private static void testToConcurrentMapWithBinaryOperator() {
Optional.of(menu.stream().collect(Collectors.toConcurrentMap(Dish::getType, v -> 1L, (a, b) -> a + b))).ifPresent(System.out::println);
}
private static void testToConcurrentMapWithBinaryOperatorAndSupplier() {
Optional.of(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());
});
}
private static void testToList() {
Optional.of(menu.stream().filter(Dish::isVegetarian).collect(Collectors.toList())).ifPresent(
r -> {
System.out.println(r.getClass());
System.out.println(r);
}
);
}
private static void testToSet() {
Optional.of(menu.stream().filter(Dish::isVegetarian).collect(Collectors.toSet())).ifPresent(
r -> {
System.out.println(r.getClass());
System.out.println(r);
}
);
}
private static void testToMap() {
Optional.of(menu.stream().collect(Collectors.toMap(Dish::getName, Dish::getCalories))).ifPresent(System.out::println);
}
private static void testToMapWithBinaryOperator() {
Optional.of(menu.stream().collect(Collectors.toMap(Dish::getType, v -> 1L, (a, b) -> a + b))).ifPresent(System.out::println);
}
private static void testToMapWithBinaryOperatorAndSupplier() {
Optional.of(menu.stream().collect(Collectors.toMap(Dish::getType, v -> 1L, (a, b) -> a + b, HashMap::new)))
.ifPresent(v -> {
System.out.println(v);
System.out.println(v.getClass());
});
}
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
4200.0 4200.0 4200 4200 4200 4200 [Dish{name='pork', vegetarian=false, calories=800, type=MEAT}, Dish{name='beef', vegetarian=false, calories=700, type=MEAT}] {season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, pork=800, rice=350, french fries=530, prawns=300} {MEAT=3, OTHER=4, FISH=2} {MEAT=3, FISH=2, OTHER=4} class java.util.concurrent.ConcurrentSkipListMap class java.util.ArrayList [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.HashSet [Dish{name='rice', vegetarian=true, calories=350, type=OTHER}, Dish{name='pizza', vegetarian=true, calories=550, type=OTHER}, Dish{name='season fruit', vegetarian=true, calories=120, type=OTHER}, Dish{name='french fries', vegetarian=true, calories=530, type=OTHER}] {season fruit=120, chicken=400, pizza=550, salmon=450, beef=700, rice=350, pork=800, prawns=300, french fries=530} {MEAT=3, OTHER=4, FISH=2} {MEAT=3, OTHER=4, FISH=2} class java.util.HashMap