java 8 stream
java 8 stream
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.stream.Collectors;
public class Java8Stream {
public static class CouponInfo {
private Long capacity;
private Integer number;
private String messageString;
public Long getCapacity() {
return capacity;
}
public void setCapacity(Long capacity) {
this.capacity = capacity;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getMessageString() {
return messageString;
}
public void setMessageString(String messageString) {
this.messageString = messageString;
}
public CouponInfo(Long capacity, Integer number, String messageString) {
super();
this.capacity = capacity;
this.number = number;
this.messageString = messageString;
}
public CouponInfo() {
super();
// TODO Auto-generated constructor stub
}
}
public static void main(String[] args) {
CouponInfo couponInfo1 = new CouponInfo(123L, 10001, "5元现金券");
CouponInfo couponInfo2 = new CouponInfo(124L, 10001, "10元现金券");
CouponInfo couponInfo3 = new CouponInfo(125L, 10002, "全场9折");
CouponInfo couponInfo4 = new CouponInfo(126L, 10002, "全场8折");
CouponInfo couponInfo5 = new CouponInfo(127L, 10003, "全场7折");
List<CouponInfo> couponInfoList = new ArrayList<>();
couponInfoList.add(couponInfo1);
couponInfoList.add(couponInfo2);
couponInfoList.add(couponInfo3);
couponInfoList.add(couponInfo4);
couponInfoList.add(couponInfo5);
// stream foreach
foreach(couponInfoList);
// stream filter
filter(couponInfoList);
// limit
limit(couponInfoList);
// sort
sort(couponInfoList);
// map
map(couponInfoList);
// statistic
statistic(couponInfoList);
}
public static void foreach(List<CouponInfo> couponInfoList) {
couponInfoList.stream().forEach(n -> System.out.print(n));
System.out.println();
}
public static void filter(List<CouponInfo> couponInfoList) {
couponInfoList.stream().filter(n -> n.getCapacity() > 126L).forEach(n -> System.out.print(n.getCapacity()));
System.out.println();
}
public static void limit(List<CouponInfo> couponInfoList) {
couponInfoList.stream().limit(3).forEach(n -> System.out.print(n.getCapacity()));
System.out.println();
}
public static void sort(List<CouponInfo> couponInfoList) {
couponInfoList.stream().sorted(Comparator.comparing(CouponInfo::getCapacity).reversed())
.forEach(n -> System.out.print(n.getCapacity()));
System.out.println();
}
public static void map(List<CouponInfo> couponInfoList) {
couponInfoList.stream().map(n -> n.getCapacity() * 2).forEach(n -> System.out.print(n));
System.out.println();
}
public static void statistic(List<CouponInfo> couponInfoList) {
List<Long> lists = couponInfoList.stream().map(n -> n.getCapacity()).collect(Collectors.toList());
LongSummaryStatistics res = lists.stream().mapToLong(n -> n).summaryStatistics();
System.out.println(res.getAverage());
System.out.println(res.getCount());
System.out.println(res.getMax());
System.out.println(res.getMin());
System.out.println(res.getSum());
}
}
不恋尘世浮华,不写红尘纷扰