编程知识 cdmana.com

Java 8 list.stream Common use of

public static void main(String[] args) {
        List<Student> list = Lists.newArrayList();
        list.add(new Student(" test ", " male ", 18));
        list.add(new Student(" Development ", " male ", 20));
        list.add(new Student(" Operation and maintenance ", " Woman ", 19));
        list.add(new Student("DBA", " Woman ", 22));
        list.add(new Student(" operating ", " male ", 24));
        list.add(new Student(" product ", " Woman ", 21));
        list.add(new Student(" The manager ", " Woman ", 25));
        list.add(new Student(" product ", " Woman ", 21));
 
        // Students who want to be male 
        List<Student> l1 = list.stream().filter(student -> student.sex.equals(" male ")).collect(toList());
        
        //map Of key value true For male ,false A collection for women 
        Map<Boolean, List<Student>> map = list.stream().collect(partitioningBy(student -> student.getSex().equals(" male ")));
 
        // Ask for the total age of male students 
        Integer sum = list.stream().filter(student -> student.sex.equals(" male ")).mapToInt(Student::getAge).sum();
 
        // The number of people was counted by gender 
        Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.summingInt(p -> 1)));
 
        // Judge whether there is any age older than 25 Year old student 
        boolean check = list.stream().anyMatch(student -> student.getAge() > 25);
 
        // Get the name set of all students 
        List<String> l2 = list.stream().map(Student::getName).collect(toList());
 
        // Ask for the average age of everyone 
        double avg = list.stream().collect(averagingInt(Student::getAge));
 
        // Ask the oldest student 
        Student s = list.stream().reduce((student, student2) -> student.getAge() > student2.getAge() ? student:student2).get();
        
        Student stu = list.stream().collect(maxBy(Comparator.comparing(Student::getAge))).get();
 
        // Sort by age 
        List<Student> l3 = list.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(toList());
 
        // For the youngest two students 
        List<Student> l4 = l3.stream().limit(2).collect(toList());
 
        // Get all the names , Make a statement 
        String str = list.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));
        
        // Get the maximum age 、 minimum value 、 Average 、 Sum up, etc 
        IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(Student::getAge).summaryStatistics();
        System.out.println(intSummaryStatistics.getMax());
        System.out.println(intSummaryStatistics.getCount());
    }
 
    @Data
    @AllArgsConstructor
    static class Student{
        String name;
        String sex;
        Integer age;
 
    }

Five factors that affect performance : data size 、 Source data structure 、 Is it worth packing 、 Usable CPU The amount and the time it takes to process each element .

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[The blank of writing memory]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224104023955x.html

Scroll to Top