JDK8 Stream代码段

import lombok.Data;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class Jdk8StreamTest {

    /**
     * 分组
     * 按BuyerName的值,将原来的List分为多组
     */
    @Test
    public void testCollectorsGroupingBy() {
        List<Invoice> invoiceList = genInvoiceList();
        Map<String, List<Invoice>> map = invoiceList.stream().collect(Collectors.groupingBy(Invoice::getBuyerName));
    }

    /**
     * 分组
     */
    @Test
    public void testCollectorsGroupingBy2() {
       List<User> userList = genUserList();

        //按名字分组
        Map<String, List<User>> nameMap = userList.stream().collect(Collectors.groupingBy(User::getName));
        System.out.println("nameMap = " + nameMap);

        //按分数分组
        Map<Integer, List<User>> scoreMap = userList.stream().collect(Collectors.groupingBy(User::getScore));
        System.out.println("scoreMap = " + scoreMap);

        //按名字分组,并求出组内元素个数
        Map<String, Long> nameMap2 = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.counting()));
        System.out.println("nameMap2 = " + nameMap2);

        //按名字分组,组内的分数加和后取平值值
        Map<String, Double> nameMap3 = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.averagingDouble(User::getScore)));
        System.out.println("nameMap3 = " + nameMap3);

        //分区:分区是分组的一种特殊情况,分组可以分成多组,但分区只分成2组
        //分数及格的人
        Map<Boolean, List<User>> map4 = userList.stream().collect(Collectors.partitioningBy(user -> user.getScore() >= 60));
        System.out.println("map4 = " + map4);

        //按名字分组,同一组内仅取分数最大的那个
        Map<String, Optional<User>> map = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.maxBy(Comparator.comparing(User::getScore))));

        map.forEach((k, v) -> {
            System.out.println("### k=" + k);
            System.out.println("### v=" + v.get());
        });
    }

    /**
     * 排序
     * 按totalAmount的值,将原来的List重新排序,金额小-->大
     */
    @Test
    public void testSorted() {
        List<Invoice> invoiceList = genInvoiceList();
        List<Invoice> sortedInvoiceList = invoiceList.stream().sorted(Comparator.comparing(Invoice::getTotalAmount).reversed()).collect(Collectors.toList());
    }

    /**
     * 加和
     * 将List里每个对象的totalAmount加和
     */
    @Test
    public void testMapReduce() {
        List<Invoice> invoiceList = genInvoiceList();
        BigDecimal sum = invoiceList.stream().map(Invoice::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * 过滤
     * 过滤出totalAmount大于100的对象
     */
    @Test
    public void testFilter() {
        List<Invoice> invoiceList = genInvoiceList();
        List<Invoice> moreThan100List = invoiceList.stream().filter(e -> e.getTotalAmount().compareTo(new BigDecimal("100")) == 1).collect(Collectors.toList());
    }

    /**
     * 过滤
     * 过滤出buyerName,剔重,并按字母顺序排序
     */
    @Test
    public void testFilter2() {
        List<Invoice> invoiceList = genInvoiceList();
        List<String> buyerNameList = invoiceList.stream().map(Invoice::getBuyerName).distinct().sorted().collect(Collectors.toList());
    }

    /**
     * 发票信息
     */
    @Data
    private static final class Invoice {
        /**
         * 购方名称
         */
        private String buyerName;
        
        /**
         * 价税合计
         */
        private BigDecimal totalAmount;
    }

    @Data
    private static final class User {
        private String name;
        private int score;
        private int age;
    }

    private List<Invoice> genInvoiceList() {
        List<Invoice> invoiceList = new ArrayList<>();

        Invoice vo1 = new Invoice();
        vo1.setBuyerName("A公司-统计");
        vo1.setTotalAmount(new BigDecimal("100"));

        Invoice vo2 = new Invoice();
        vo2.setBuyerName("A公司-统计");
        vo2.setTotalAmount(new BigDecimal("200"));

        Invoice vo3 = new Invoice();
        vo3.setBuyerName("B公司-统计");
        vo3.setTotalAmount(new BigDecimal("300"));

        Invoice vo4 = new Invoice();
        vo4.setBuyerName("B公司-统计");
        vo4.setTotalAmount(new BigDecimal("400"));

        invoiceList.add(vo4);
        invoiceList.add(vo2);
        invoiceList.add(vo3);
        invoiceList.add(vo1);

        return invoiceList;
    }

    private List<User> genUserList() {
        User user1 = new User();
        user1.setName("zhangsan");
        user1.setScore(60);
        user1.setAge(20);

        User user2 = new User();
        user2.setName("lisi");
        user2.setScore(80);
        user2.setAge(23);

        // 重名了:)
        User user3 = new User();
        user3.setName("zhangsan");
        user3.setScore(80);
        user3.setAge(24);

        User user4 = new User();
        user4.setName("wangwu");
        user4.setScore(50);
        user4.setAge(24);

        // 重名了:)
        User user5 = new User();
        user5.setName("wangwu");
        user5.setScore(100);
        user5.setAge(22);

        List<User> userList = Arrays.asList(user1, user2, user3, user4, user5);
        return userList;
    }
}

上一篇:php-从Mage_Sales_Model_Order_Invoice对象获取发票ID


下一篇:公司mysql脚本