当前位置:网站首页>List filtering, sorting, verification and other processing methods

List filtering, sorting, verification and other processing methods

2022-06-11 02:55:00 bin_ d

List filter: Filter 、map: mapping 、sorted: Sort 、forEach、collect: polymerization 、statistics: Statistics 、parallelStream: Parallel flow



public class Example{
    private String name;
    private Double score;

    public Example(String name, Double score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Example{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}

+--------------------+       +------+   +------+   +---+   +-------+
| stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
+--------------------+       +------+   +------+   +---+   +-------+
 

Processing test classes

import java.util.ArrayList;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



public class StreamTest {


    public static void main(String args[]){

        List<Example> list = new ArrayList<>();
        list.add(new Example(" Xiao feng ", 20.d));
        list.add(new Example(" False bamboo ", 40.d));
        list.add(new Example(" Duan Yu ", 80.d));
        list.add(new Example(" Sweeping monk ", 50.d));
        list.add(new Example(" Buddhist abbot ", null));
        list.add(new Example(" Wang Yu Yan ", 30.d));

        long count = 0;
        List<Example> filterList = null;


        // filter  Use of filters 
        //  Select the number of students whose grades are not empty 
        count = list.stream().filter(p -> null != p.getScore()).count();
        System.out.println(" The number of students taking the exam :" + count);

        // collect
        //  Filter out the student set whose score is not empty 
        filterList = list.stream().filter(p -> null != p.getScore()).collect(Collectors.toList());
        System.out.println(" Information about students taking the exam :");
        filterList.stream().forEach(System.out::println);

        // map  Map a set to another set 
        //  Take out all the students' grades 
        List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());
        System.out.println(" All students' scores are collected :" + scoreList);

        //  String the student name set into a string , Separate with commas 
        String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));
        System.out.println(" All student name strings :" + nameString);

        // sorted Sort 
        //  Sort students' grades in reverse order   Positive order does not require addition .reversed()
        filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());
        System.out.println(" All students' scores are collected , Reverse order :");
        filterList.stream().forEach(System.out::println);

        System.out.println(" Collect according to students' grades :");
        Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore())
                .collect(Collectors.groupingBy(UserPo::getScore));
        for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) {
            System.out.println(" achievement :" + entry.getKey() + "  The number of :" + entry.getValue().size());
        }

        // forEach
        filterList.stream().forEach(p -> p.setScore(p.getScore() + 10));
        System.out.println(" There are too few qualified people , Add to everyone 10 branch ");
        filterList.stream().forEach(System.out::println);

        // count
        count = filterList.stream().filter(p -> p.getScore() >= 60).count();
        System.out.println(" The number of people who have passed the final exam " + count);

        DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).summaryStatistics();
        System.out.println(" The largest number in the list  : " + statistics.getMax());
        System.out.println(" The smallest number in the list  : " + statistics.getMin());
        System.out.println(" Sum of all Numbers  : " + statistics.getSum());
        System.out.println(" The average  : " + statistics.getAverage());

        //  Parallel flow   Use 
        count = list.parallelStream().filter(p -> null != p.getScore()).count();
        System.out.println("test:" + count);

    }

}

原网站

版权声明
本文为[bin_ d]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110150278829.html