首先定義一下準備工作
package com.chaojilaji.work.statedemo.lambdademo2;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
@Getter
@Setter
public class Book{
private double money;
private String bookName;
private String title;
private List<String> auther;
private String topic;
private int pageCount;
private LocalDate pubDate;
private double height;
public Book(Builder builder){
this.auther = builder.auther;
this.bookName = builder.bookName;
this.height = builder.height;
this.money = builder.money;
this.pageCount = builder.pageCount;
this.pubDate = builder.pubDate;
this.title = builder.title;
this.topic = builder.topic;
}
@Override
public boolean equals(Object object){
if (object instanceof Book){
Book book = (Book) object;
if (book.getAuther()==this.getAuther() && book.getPageCount()==this.getPageCount()){
return true;
}
}
return false;
}
public static class Builder{
private double money;
private String bookName;
private String title;
private List<String> auther;
private String topic;
private int pageCount;
private LocalDate pubDate;
private double height;
public Builder setMoney(double money){
this.money = money;
return this;
}
public Builder setBookName(String bookName){
this.bookName = bookName;
return this;
}
public Builder setAuther(List<String> authers){
this.auther = authers;
return this;
}
public Builder setTitle(String title){
this.title = title;
return this;
}
public Builder setTopic(String topic){
this.topic = topic;
return this;
}
public Builder setPageCount(int pageCount){
this.pageCount = pageCount;
return this;
}
public Builder setPubDate(LocalDate pubDate){
this.pubDate = pubDate;
return this;
}
public Builder setHeight(int height){
this.height = height;
return this;
}
public Book build(){
return new Book(this);
}
}
}
初始化一個Book的列表
private static List<Book> initBooks() {
int n = 100;
List<Book> books = new ArrayList<>();
for (int i = 0; i < n; i++) {
int m = 10;
List<String> s = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");
StringBuffer stringBuffer = new StringBuffer();
for (int j = 0; j < m; j++) {
stringBuffer.append(s.get(new Random().nextInt(s.size())));
}
Book book = new Book.Builder()
.setBookName("精通".concat(stringBuffer.toString()))
.setAuther(Arrays.asList("aaa", "bbb", "ccc", "abc"))
.setHeight(20)
.setMoney(new Random().nextDouble() * 100)
.setPageCount(new Random().nextInt(1000))
.setPubDate(LocalDate.now())
.setTitle(stringBuffer.toString())
.setTopic(stringBuffer.substring(0, m - 5 > 0 ? m - 5 : 1)).build();
books.add(book);
}
return books;
}
上面的內容大家可以自由發揮,跟教程無關。下面,進入正題。
獲取書的流
List<Book> library = initBooks();
Stream<Book> bookStream = library.parallelStream();
parallelStream()方法獲取的是多核並行處理流
獲取屬性流
List<Book> library = initBooks();
Stream<Book> bookStream = library.parallelStream(); // 獲取書的流
Stream<String> titles = bookStream.map(Book::getTitle); // 獲取標題流
Stream<Integer> pageCounts = bookStream.map(Book::getPageCount); // 頁數流
主流.map(類名::get屬性)
根據xxx屬性排序
Stream<Book> bookStream1 = library.parallelStream()
.sorted(Comparator.comparing(Book::getBookName));
flatMap
如果屬性是List,如何獲取屬性流,flatMap 不僅是將單個的list屬性拿出來,而且還會將多個整理成一個統一的流,即flatMap接受的lambda參數是一個流,功能是 合在一起
Stream<String> authers = library.parallelStream()
.sorted(Comparator.comparing(Book::getBookName))
.flatMap(book -> book.getAuther().parallelStream());
int pageCountSum = library.parallelStream()
.sorted(Comparator.comparing(Book::getBookName))
.flatMapToInt(book -> IntStream.of(book.getPageCount()))
.sum();
去重
Stream<String> authers = library.parallelStream()
.sorted(Comparator.comparing(Book::getBookName))
.flatMap(book -> book.getAuther().parallelStream());
authers = authers.distinct();
截斷
包含向前截斷和向後截斷兩種,limit是取前n個,skip是捨去前n個
List<Book> books2 = library.parallelStream()
.sorted(Comparator.comparing(Book::getMoney))
.skip(50)
.collect(Collectors.toList()); // 截斷前50,返回剩下的
List<Book> books3 = library.parallelStream()
.sorted(Comparator.comparing(Book::getMoney))
.limit(50)
.collect(Collectors.toList()); // 獲取前50
獲取最早出版的圖書
Optional<Book> optionalBooks = bookStream2.min(Comparator.comparing(Book::getPubDate));
過濾
optionalBooks = optionalBooks.filter(book -> (book.getMoney() - 30.0) < 0.0001);
bookStream2 = bookStream2.filter(book -> (book.getMoney() - 30.0) < 0.0001);
peek的用法 : 一般用於輸出中間結果,對調試提供支持
library.parallelStream().peek(book -> log.info("{}", book.getBookName()));
將流轉回數據結構
Set<Book> books1 = bookStream2.collect(Collectors.toSet());
Set<String> titleSet = titles.collect(Collectors.toSet());
List<Book> books = bookStream1.collect(Collectors.toList());
搜索操作 anyMatch allMatch noneMatch
boolean res = library.parallelStream()
.mapToInt(Book::getPageCount)
.noneMatch(pageCount -> pageCount > 500);
res = library.parallelStream()
.mapToInt(Book::getPageCount)
.allMatch(pageCount -> pageCount <= 500);
res = library.parallelStream()
.mapToInt(Book::getPageCount)
.anyMatch(pageCount -> pageCount > 500);
findFirst & findAny
Optional<Book> books4 = library.parallelStream()
.findAny();
Optional<Book> book5 = library.parallelStream()
.findFirst();
統計
IntSummaryStatistics intSummaryStatistics = bookStream1.mapToInt(Book::getPageCount)
.summaryStatistics();
結果變成map
library.parallelStream()
.collect(Collectors.groupingBy(Book::getTopic));
library.parallelStream()
.filter(book -> book.getPageCount() < 300)
.collect(Collectors.toMap(Book::getAuther, Book::getBookName));
library.parallelStream()
.filter(book -> book.getPageCount() < 300)
.collect(Collectors.toMap(Book::getBookName, Book::getPubDate, (x, y) -> x.isAfter(y) ? x : y));
Map<String,String> ans123 = library.parallelStream()
.collect(Collectors.toMap(Book::getBookName,Book::getTitle, BinaryOperator.maxBy(naturalOrder()),TreeMap::new));
forEach會終止一個流,但是是並行的
library.parallelStream().forEach(book -> {
log.info("{} {} {} {}", book.getBookName(), book.getAuther(), book.getPubDate(), LocalDateTime.now());
});
forEachOrdered是同步的
library.parallelStream().forEachOrdered(book -> {
log.info("{} {} {} {}", book.getBookName(), book.getAuther(), book.getPubDate(), LocalDateTime.now());
});