下面更多是bigDecimal的操作
public class Jdk8StreamTest {
@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);
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());
});
}
@Test
public void testSorted() {
List<Invoice> invoiceList = genInvoiceList();
List<Invoice> sortedInvoiceList = invoiceList.stream().sorted(Comparator.comparing(Invoice::getTotalAmount).reversed()).collect(Collectors.toList());
}
@Test
public void testMapReduce() {
List<Invoice> invoiceList = genInvoiceList();
BigDecimal sum = invoiceList.stream().map(Invoice::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
}
@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());
}
@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;
}
}