List<Pool> list = new ArrayList<Pool>(){ {

add(new Pool("A", 1));

add(new Pool("A", 2)); add(new Pool("A", 3)); add(new Pool("B", 4));

add(new Pool("B", 5)); } };

//求和 int sum = list.stream().mapToInt(Pool::getValue).sum();

// 最大值 OptionalInt max = list.stream().mapToInt(Pool::getValue).max();

// 最小值 OptionalInt min = list.stream().mapToInt(Pool::getValue).min();

// 平均值 OptionalDouble average = list.stream().mapToInt(Pool::getValue).average();

 

//对long值处理

list.stream().mapToLong(Pool::getValue).sum();

list.stream().mapToLong(Pool::getValue).max();

list.stream().mapToLong(Pool::getValue).min();

list.stream().mapToLong(Pool::getValue).average();

//对double处理

list.stream().mapToDouble(Pool::getValue).sum();

list.stream().mapToDouble(Pool::getValue).max();

list.stream().mapToDouble(Pool::getValue).min();

list.stream().mapToDouble(Pool::getValue).average();

 

 

java8很多用法还是需要多积累,用熟了能大量简化代码。今天记录一下用java8对集合处理的用法,由于一下子想不起来全部用法,因此遇到了相应的场景就过来更新。

1、首先我们新建一个Book类,用来描述书籍的主要信息,get set及构造方法略。

public class Book {

/**
* 书名
*/
private String bookName;

/**
* 分类
*/
private String type;

/**
* 作者
*/
private String author;

/**
* 价格
*/
private BigDecimal price;
}
2、然后初始化一个集合books,用来做处理

public static void main(String[] args) {

//初始化集合信息
List<Book> books = new ArrayList<Book>();

Book book1 = new Book("java从入门到放弃","java","大炮",new BigDecimal("100.5"));
Book book2 = new Book("mysql从删库到跑路","数据库","大忽悠",new BigDecimal("99.9"));
Book book3 = new Book("Python从熟悉到陌生","Python","猫蛋",new BigDecimal("109.8"));
Book book4 = new Book("oracle从入门到删库","数据库","狗蛋",new BigDecimal("128.8"));
Book book5 = new Book("程序员防脱生发指南","养生","二狗子",new BigDecimal("88.8"));

books.add(book1);
books.add(book2);
books.add(book3);
books.add(book4);
books.add(book5);
}
场景一:java8筛选出数据库类并且价格低于100的书籍(filter过滤用法)

// 1、筛选出数据库类且价格低于100的书籍
List<Book> exampleBook1 = books.stream().filter(e->"数据库".equals(e.getType()) &&
new BigDecimal("100").compareTo(e.getPrice()) > 0).collect(Collectors.toList());
运行结果

场景二:java8求最大、最小值

// 2、筛选价格最高(低)的书籍
Book exampleBook2 = books.stream().min(Comparator.comparing(Book::getPrice)).get();
Book exampleBook3 = books.stream().max(Comparator.comparing(Book::getPrice)).get();
运行结果

场景三:java8按照种类分类并统计每大类总价(单字段分组统计求和)

//写法1
Map<String, BigDecimal> resultMap = books.stream().collect(Collectors.toMap(Book::getType, Book::getPrice, BigDecimal::add));
运行结果

/写法二
Map<String, BigDecimal> resultMap = books.stream().collect(Collectors.groupingBy(Book::getType,Collectors.mapping(Book::getPrice, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
运行结果

场景四:java8按照书籍种类和作者两个字段分组并统计总价(多字段分组统计求和)

//按照书籍种类和作者两个字段分组并统计总价
Map<String, BigDecimal> resultMap = books.stream().collect(Collectors.groupingBy(e -> e.getType() + "_" + e.getAuthor(),
Collectors.mapping(Book::getPrice, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))));
运行结果

场景四:java8求所有书籍总价 (求和)

//求所有书籍总价
BigDecimal totalPrice = books.stream().map(Book::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
运行结果

 

场景五:java8根据书籍价格升序、降序排序并取出价格单个字段建立新的集合

//根据书籍价格升序、降序排序
//升序
List<BigDecimal> risePrices = books.stream().sorted(Comparator.comparing(Book::getPrice)).map(Book::getPrice).collect(Collectors.toList());
运行结果

 

//降序
List<BigDecimal> reversedPrices = books.stream().sorted(Comparator.comparing(Book::getPrice).reversed()).map(Book::getPrice).collect(Collectors.toList());
运行结果

场景六:排序并取出第一个元素

//排序并取回指定元素
Book firstBook = books.stream().sorted(Comparator.comparing(Book::getPrice).reversed()).findFirst().get();
运行结果:

场景七:java8根据书籍种类进行去重

//种类去重
List<String> distinctBooks = books.stream().map(Book::getType).distinct().collect(Collectors.toList());

 

对map集合进行降序



**
* @author: daizy
* @since: 2022/11/9 19:49
* @description:对list集合属性分组,求和
*/
@Data
public class CountVO {
private String name;
private Long count;

private String type;

public CountVO() {
}

public CountVO(String name, Long count, String type) {
this.name = name;
this.count = count;
this.type = type;
}
public static void main(String[] args) {
List<CountVO> countVOList = new LinkedList<>();
countVOList.add(new CountVO("苹果",100L,"红富士"));
countVOList.add(new CountVO("苹果",130L,"金元帅"));
countVOList.add(new CountVO("香蕉",120L,"芭蕉"));
countVOList.add(new CountVO("香蕉",130L,"米蕉"));
countVOList.add(new CountVO("西瓜",140L,"麒麟瓜"));
countVOList.add(new CountVO("西瓜",120L,"黑金刚"));

countVOList = countVOList.stream()
// 根据name分组求和, 返回的是Map
// CountVO::getName 需要分组的字段
// Collectors.summingLong(CountVO::getCount) 统计总数
.collect(Collectors.groupingBy(item->(item.getName()+item.getType()), Collectors.summingLong(CountVO::getCount)))
//转为List集合
.entrySet().stream()
.map(e -> new CountVO( e.getKey(),e.getValue(),null))
.collect(Collectors.toList());
System.out.println(JSON.toJSONString(countVOList));
}