Java8 Stream操作用的很少,特地写一些例子理解,方便以后使用
/** * 功能描述: 书实体类 * @Author: XinHai.Ma * @Date: 2020/9/22 21:19 */ class Book { private long bookId; private String bookName; private String bookAuthor; private String bookDesc; public Book() {} public Book(long bookId, String bookName, String bookAuthor, String bookDesc) { this.bookId = bookId; this.bookName = bookName; this.bookAuthor = bookAuthor; this.bookDesc = bookDesc; } public long getBookId() { return bookId; } public void setBookId(long bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookAuthor() { return bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public String getBookDesc() { return bookDesc; } public void setBookDesc(String bookDesc) { this.bookDesc = bookDesc; } @Override public String toString() { return "Book{" + "bookId=" + bookId + ", bookName='" + bookName + '\'' + ", bookAuthor='" + bookAuthor + '\'' + ", bookDesc='" + bookDesc + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Book book = (Book) o; return bookId == book.bookId && Objects.equals(bookName, book.bookName) && Objects.equals(bookAuthor, book.bookAuthor) && Objects.equals(bookDesc, book.bookDesc); } @Override public int hashCode() { return Objects.hash(bookId, bookName, bookAuthor, bookDesc); } }
使用例子:
import java.util.*; import java.util.stream.Collectors; public class StreamDemo { public static void main(String[] args) { List<Book> bookList = initData(); // map 利用function提取出一个集合 // 提取id集合 bookList.stream().map(Book::getBookId).forEach(System.out::println); // 提取bookName集合 bookList.stream().map(Book::getBookName).forEach(System.out::println); // 过滤出满足条件的数据 bookList.stream().filter(book -> { if(book.getBookId() > 50) { return true; } return false; }).forEach(System.out::println); System.out.println("==去除重复元素=="); // 向bookList集合添加一个重复元素 bookList.add(new Book(1, "book1", "author1", "desc1")); // 去掉重复元素 distinct实现依赖于hashCode和equals方法 bookList.stream().distinct().forEach(System.out::println); System.out.println("==定制排序=="); // 定制排序 倒序 bookList.stream().sorted((book1, book2) -> { return Integer.parseInt(String.valueOf(book2.getBookId() - book1.getBookId())); }).forEach(System.out::println); // 匹配 boolean findResult = bookList.stream().allMatch(book -> book.getBookName().equals("book99")); System.out.println("匹配结果: " + findResult); findResult = bookList.stream().anyMatch(book -> book.getBookName().equals("book88")); System.out.println("匹配结果: " + findResult); findResult = bookList.stream().noneMatch(book -> book.getBookName().equals("book77")); System.out.println("匹配结果: " + findResult); // 查询最大值元素 Optional<Book> max = bookList.stream().max((book1, book2) -> { return Integer.parseInt(String.valueOf(book1.getBookId() - book2.getBookId())); }); System.out.println("max: " + max.get()); // 查询最小值元素 Optional<Book> min = bookList.stream().min((book1, book2) -> { return Integer.parseInt(String.valueOf(book1.getBookId() - book2.getBookId())); }); System.out.println("min: " + min.get()); // 筛选出bookName为book1的集合,找出集合第一个元素 Optional<Book> first = bookList.stream().filter(book -> { if(book.getBookName().equals("book1")) { return true; } return false; }).findFirst(); System.out.println("first: " + first.get()); long total = bookList.stream().count(); System.out.println("集合大小: " + total); // 去除重复元素,将结果转换为Set集合 Set<Book> set = bookList.stream().distinct().collect(Collectors.toSet()); // 将List根据BookName为key转化为Map Map<String, List<Book>> map = bookList.stream().collect(Collectors.groupingBy(Book::getBookName)); // 将bookName集合放到一个新集合 bookList.stream().map(Book::getBookName).collect(Collectors.toList()); // flatMap 层次结构扁平化 还不是很理解,就知道要这么用 List<Book> bookList1 = new ArrayList<>(); bookList1.addAll(bookList); List<Book> bookList2 = new ArrayList<>(); bookList2.addAll(bookList); List<Book> bookList3 = new ArrayList<>(); bookList3.addAll(bookList); List<List<Book>> books = new ArrayList<>(3); books.add(bookList1); books.add(bookList2); books.add(bookList3); List<Book> bookStream = books.stream().flatMap(List::stream).collect(Collectors.toList()); bookStream.forEach(System.out::println); } /** * 功能描述: 初始化数据 * @Param: [] * @Return: java.util.List<stream.Book> * @Author: XinHai.Ma * @Date: 2020/9/22 21:18 */ public static List<Book> initData() { List<Book> bookList = new ArrayList<>(100); for (int i=1; i<=100; i++) { Book book = new Book(i, "book"+i, "author"+i, "desc"+i); bookList.add(book); } return bookList; } }
越长大越孤单,哎