1. 转Map
List<Book> books = bookMapper.list();
Map<String, Book> bookMap = books.stream().collect(Collectors.toMap(Book::getBookId, book -> book));
{
"1": {
"bookId": "1",
"bookTitle": "书1"
},
"2": {
"bookId": "2",
"bookTitle": "书2"
},
}
2. 字符串拼接
List<Book> books = bookMapper.list();
String bookNames = books.stream().map(Book::getBookTitle).collect(Collectors.joining(","));
"书1,书2"
3. 类型转换
List<Book> books = bookMapper.list();
List<Book2> book2s = books.stream().map(book -> {
Book2 book2 = new Book2();
BeanUtils.copyProperties(book, book2);
}).collect(Collectors.toList());
4. 条件判断(存在任意匹配的)
List<Book> books = bookMapper.list();
if (books.stream().anyMatch(book -> !book.getBookTitle.contains("书"))) {
throw new RuntimeException("存在非正常书名称");
} else {
return books;
}
5. 条件判断(无匹配的)
List<Book> books = bookMapper.list();
if (books.stream().noneMatch(book -> "1".equals(book.getType()))) {
throw new RuntimeException("无指定类型的书籍");
}
6. 修改数据项
List<Book> books = bookMapper.list();
return books.stream().peek(book -> book.setBookTitle(null)).collect(Collectors.toList());
7. 过滤
List<Book> books = bookMapper.list();
return books.stream().filter(book -> "1".equals(book.getType())).collect(Collectors.toList());
8. 排序
List<Book> books = bookMapper.list();
return books.stream().sorted(Comparator.comparing(Book::getCreateTime)).collect(Collectors.toList());
原文地址:https://blog.csdn.net/jl15988/article/details/134716498
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_17737.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。