Example usage for java.util.stream Collectors mapping

List of usage examples for java.util.stream Collectors mapping

Introduction

In this page you can find the example usage for java.util.stream Collectors mapping.

Prototype

public static <T, U, A, R> Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper,
        Collector<? super U, A, R> downstream) 

Source Link

Document

Adapts a Collector accepting elements of type U to one accepting elements of type T by applying a mapping function to each input element before accumulation.

Usage

From source file:Main.java

public static void main(String... args) {
    Map<Type, Set<CaloricLevel>> o = Food.menu.stream()
            .collect(Collectors.groupingBy(Food::getType, Collectors.mapping(dish -> {
                if (dish.getCalories() <= 400)
                    return CaloricLevel.DIET;
                else if (dish.getCalories() <= 700)
                    return CaloricLevel.NORMAL;
                else
                    return CaloricLevel.FAT;
            }, Collectors.toSet())));

    System.out.println(o);// www.j a va 2s.c  o m

}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, String> namesByGender = Employee.persons().stream().collect(Collectors
            .groupingBy(Employee::getGender, Collectors.mapping(Employee::getName, Collectors.joining(", "))));
    System.out.println(namesByGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Boolean, String> partionedByMaleGender = Employee.persons().stream().collect(Collectors
            .partitioningBy(Employee::isMale, Collectors.mapping(Employee::getName, Collectors.joining(", "))));
    System.out.println(partionedByMaleGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, List<String>> namesByGender = Employee.persons().stream().collect(Collectors
            .groupingBy(Employee::getGender, Collectors.mapping(Employee::getName, Collectors.toList())));

    System.out.println(namesByGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, String> namesByGender = Employee.persons().stream()
            .collect(Collectors.groupingByConcurrent(Employee::getGender,
                    Collectors.mapping(Employee::getName, Collectors.joining(", "))));
    System.out.println(namesByGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map personsByGenderAndDobMonth = Employee.persons().stream().collect(
            Collectors.groupingBy(Employee::getGender, Collectors.groupingBy(p -> p.getDob().getMonth(),
                    Collectors.mapping(Employee::getName, Collectors.joining(", ")))));

    System.out.println(personsByGenderAndDobMonth);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Gender, Map<Object, String>> personsByGenderAndDobMonth = Employee.persons().stream().collect(
            Collectors.groupingBy(Employee::getGender, Collectors.groupingBy(p -> p.getDob().getMonth(),
                    Collectors.mapping(Employee::getName, Collectors.joining(", ")))));

    System.out.println(personsByGenderAndDobMonth);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Month, String> dobCalendar = Employee.persons().stream()
            .collect(Collectors.collectingAndThen(Collectors.groupingBy(p -> p.getDob().getMonth(),
                    Collectors.mapping(Employee::getName, Collectors.joining(" "))), result -> {
                        for (Month m : Month.values()) {
                            result.putIfAbsent(m, "None");
                        }//from   w w  w .jav a 2 s . c  om
                        return Collections.unmodifiableMap(new TreeMap<>(result));
                    }));

    dobCalendar.entrySet().forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> roster = createRoster();

    System.out.println("Names by gender:");
    Map<Person.Sex, List<String>> namesByGender = roster.stream().collect(
            Collectors.groupingBy(Person::getGender, Collectors.mapping(Person::getName, Collectors.toList())));

    List<Map.Entry<Person.Sex, List<String>>> namesByGenderList = new ArrayList<>(namesByGender.entrySet());

    namesByGenderList.stream().forEach(e -> {
        System.out.println("Gender: " + e.getKey());
        e.getValue().stream().forEach(f -> System.out.println(f));
    });//w  ww  . ja v  a  2  s . c o  m

}

From source file:Main.java

public static <T, K, V> HashMap<K, HashSet<V>> group(Collection<T> entities, Function<T, K> keyMapper,
        Function<T, V> valueMapper) {
    return entities.stream().collect(Collectors.groupingBy(keyMapper, HashMap::new,
            Collectors.mapping(valueMapper, Collectors.toCollection(HashSet::new))));
}