zoukankan      html  css  js  c++  java
  • Java8-Stream-No.10

    
    import java.util.Arrays;
    import java.util.IntSummaryStatistics;
    import java.util.List;
    import java.util.Map;
    import java.util.StringJoiner;
    import java.util.stream.Collector;
    import java.util.stream.Collectors;
    
    public class Streams10 {
    
        static class Person {
            String name;
            int age;
    
            Person(String name, int age) {
                this.name = name;
                this.age = age;
            }
    
            @Override
            public String toString() {
                return name;
            }
        }
    
        public static void main(String[] args) {
            List<Person> persons =
                Arrays.asList(
                    new Person("Max", 18),
                    new Person("Peter", 23),
                    new Person("Pamela", 23),
                    new Person("David", 12));
    
    //        test1(persons);
    //        test2(persons);
    //        test3(persons);
    //        test4(persons);
    //        test5(persons);
    //        test6(persons);
    //        test7(persons);
    //        test8(persons);
            test9(persons);
        }
    
        private static void test1(List<Person> persons) {
            List<Person> filtered =
                persons
                    .stream()
                    .filter(p -> p.name.startsWith("P"))
                    .collect(Collectors.toList());
    
            System.out.println(filtered);    // [Peter, Pamela]
        }
    
        private static void test2(List<Person> persons) {
            Map<Integer, List<Person>> personsByAge = persons
                .stream()
                .collect(Collectors.groupingBy(p -> p.age));
    
            personsByAge
                .forEach((age, p) -> System.out.format("age %s: %s
    ", age, p));
    
            // age 18: [Max]
            // age 23:[Peter, Pamela]
            // age 12:[David]
        }
    
        private static void test3(List<Person> persons) {
            Double averageAge = persons
                .stream()
                .collect(Collectors.averagingInt(p -> p.age));
    
            System.out.println(averageAge);     // 19.0
        }
    
        private static void test4(List<Person> persons) {
            IntSummaryStatistics ageSummary =
                persons
                    .stream()
                    .collect(Collectors.summarizingInt(p -> p.age));
    
            System.out.println(ageSummary);
            // IntSummaryStatistics{count=4, sum=76, min=12, average=19,000000, max=23}
        }
    
        private static void test5(List<Person> persons) {
            String names = persons
                .stream()
                .filter(p -> p.age >= 18)
                .map(p -> p.name)
                .collect(Collectors.joining(" and ", "In Germany ", " are of legal age."));
    
            System.out.println(names);
            // In Germany Max and Peter and Pamela are of legal age.
        }
    
        private static void test6(List<Person> persons) {
            Map<Integer, String> map = persons
                .stream()
                .collect(Collectors.toMap(
                    p -> p.age,
                    p -> p.name,
                    (name1, name2) -> name1 + ";" + name2));
    
            System.out.println(map);
            // {18=Max, 23=Peter;Pamela, 12=David}
        }
    
        private static void test7(List<Person> persons) {
            Collector<Person, StringJoiner, String> personNameCollector =
                Collector.of(
                    () -> new StringJoiner(" | "),          // supplier
                    (j, p) -> j.add(p.name.toUpperCase()),  // accumulator
                    (j1, j2) -> j1.merge(j2),               // combiner
                    StringJoiner::toString);                // finisher
    
            String names = persons
                .stream()
                .collect(personNameCollector);
    
            System.out.println(names);  // MAX | PETER | PAMELA | DAVID
        }
    
        private static void test8(List<Person> persons) {
            Collector<Person, StringJoiner, String> personNameCollector =
                Collector.of(
                    () -> {
                        System.out.println("supplier");
                        return new StringJoiner(" | ");
                    },
                    (j, p) -> {
                        System.out.format("accumulator: p=%s; j=%s
    ", p, j);
                        j.add(p.name.toUpperCase());
                    },
                    (j1, j2) -> {
                        System.out.println("merge");
                        return j1.merge(j2);
                    },
                    j -> {
                        System.out.println("finisher");
                        return j.toString();
                    });
    
            String names = persons
                .stream()
                .collect(personNameCollector);
    
            System.out.println(names);  // MAX | PETER | PAMELA | DAVID
        }
    
        private static void test9(List<Person> persons) {
            Collector<Person, StringJoiner, String> personNameCollector =
                Collector.of(
                    () -> {
                        System.out.println("supplier");
                        return new StringJoiner(" | ");
                    },
                    (j, p) -> {
                        System.out.format("accumulator: p=%s; j=%s
    ", p, j);
                        j.add(p.name.toUpperCase());
                    },
                    (j1, j2) -> {
                        System.out.println("merge");
                        return j1.merge(j2);
                    },
                    j -> {
                        System.out.println("finisher");
                        return j.toString();
                    });
    
            String names = persons
                .parallelStream()
                .collect(personNameCollector);
    
            System.out.println(names);  // MAX | PETER | PAMELA | DAVID
        }
    }
    
  • 相关阅读:
    ABAP学习(10):ALV显示之function alv
    ABAP学习(11):ALV显示之OO ALV使用示例
    ABAP学习(3):屏幕显示
    ABAP学习(6):ABAP GUI和开发工具
    ABAP学习(9):时间日期操作
    ABAP学习(8):操作EXCEL
    ABAP学习(2):控制语句
    ABAP学习(5):数据库语句
    ABAP学习(4):内表
    编程经验:高性能.NET WEB开发(1)http压缩
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210928.html
Copyright © 2011-2022 走看看