JAVA8实战
java8发布时间2014年3月18发布[Java 8 新特性][http://www.runoob.com/java/java8-new-features.html]:
- Lambda 表达式 − Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中。
- 方法引用
- **默认方法 **
- 新工具 − 新的编译工具,如:Nashorn引擎 jjs、 类依赖分析器jdeps
- Stream API −新添加的Stream API(java.util.stream) 把真正的函数式编程风格引入到Java中。
- Date Time API − 加强对日期与时间的处理。
- Optional 类 − Optional 类已经成为 Java 8 类库的一部分,用来解决空指针异常。
- Nashorn, JavaScript 引擎 − Java 8提供了一个新的Nashorn javascript引擎,它允许我们在JVM上运行特定的javascript应用。
函数式接口
- 代表一个输出
Supplier
- 代表一个输入
Consumer
- 代表两个输入
BiConsumer
- 代表一个输入,一个输出(一般输入和输出是不同类型的)
Function
- 代表一个输入,一个输出(输入和输出是相同类型的)
UnaryOperator
- 代表两个输入,一个输出(一般输入和输出是不同类型的)
BiFunction
- 代表两个输入,一个输出(输入和输出是相同类型的)
BinaryOperator
JAVA8代码示例
JAVA8Stram流操作示例,例如:
/**
* 请求字符串转成Map对象
* index.jsp?itemId=1&userId=1001&password=2388&token=14324324132432414324123&key=index
* 参数转换成为Map
*/
@Test
public void test1() {
String queryStr = "itemId=1&userId=1001&password=2388&token=14324324132432414324123&key=index";
//map是转换的中间操作,对之前的steam进行加工
Map<String,String> params = Stream.of(queryStr.split("&")).map(str -> str.split("=")).collect(Collectors.toMap(arr -> arr[0], arr -> (arr.length>1 ? arr[1] : "")));
System.out.println(params);
}
@Test
public void test2() {
//List<Book> --> List<Long>
List<Long> ids1 = books().stream().map(book -> book.getId()).collect(Collectors.toList());
List<Long> ids2 = books().stream().map(Book::getId).collect(Collectors.toList());
System.out.println("对象集合转成id集合:" + ids1);
String ids3Str = books().stream().map(book -> String.valueOf(book.getId())).collect(Collectors.joining(",","(", ")"));
System.out.println("对象集合转成id逗号分割字符串:" + ids3Str);
List<LocalDate> ids4 = books().stream().map(Book::getPublishTime).distinct().collect(Collectors.toList());
Set<LocalDate> ids5 = books().stream().map(Book::getPublishTime).collect(Collectors.toSet());
System.out.println("对象集合转成日期数组并去重:" + ids4);
System.out.println("对象集合转成日期数组并去重:" + ids5);
System.out.println("直接排序:");
books().stream().sorted(Comparator.comparing(Book::getPrice).reversed().thenComparing(Book::getPublishTime)).
//distinct().
forEach(System.out::println);
System.out.println("集合转换成为Map");
Map<Long,Book> bookMap = books().stream().collect(Collectors.toMap(Book::getId,book -> book));
System.out.println(bookMap);
System.out.println("寻找平均价");
Double avePrice = books().stream().collect(Collectors.averagingDouble(Book::getPrice));
System.out.println(avePrice);
System.out.println("查询最贵的书籍");
Optional<Book> maxPrice = books().stream().collect(Collectors.maxBy(Comparator.comparing(Book::getPrice)));
System.out.println(maxPrice);
}
@Test
public void test3() {
System.out.println("测试分组Group by");
Map<Double,List<Book>> map = books().stream().collect(Collectors.groupingByConcurrent(Book::getPrice));
System.out.println("分组:" + map);
System.out.println();
System.out.println("分组为set:" + map);
Map<BusShopInfoEntity, Set<BusShoppingCartVo>> map = cartVos.stream().collect(Collectors.groupingBy(
BusShoppingCartVo::getBusShopInfoEntity,
Collectors.mapping( o -> o, Collectors.toSet())
));
System.out.println();
Map<Double,Long> countMap = books().stream().collect(Collectors.groupingByConcurrent(Book::getPrice, Collectors.counting()));
System.out.println("分组并计算每组数量:" + countMap);
Map<Double,Optional<Book>> couontMapMax = books().stream().collect(Collectors.groupingByConcurrent(Book::getPrice, Collectors.maxBy(Comparator.comparing(Book::getPublishTime))));
System.out.println("分组并计算得到每组初版最晚的书:" + couontMapMax);
couontMapMax.keySet().forEach(key -> {
System.out.println(key);
try {
couontMapMax.get(key).orElseThrow((() -> null));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
});
System.out.println();
System.out.println("过滤找出80块以上的书,并且按照时间排序");
List<Book> books = books().stream().filter(book -> book.getPrice()>80).sorted(Comparator.comparing(Book::getPublishTime)).collect(Collectors.toList());
System.out.println(books);
}
@Test
public void test4() {
execute(new Book());
executeByOptional(books().get(0));
}
@Test
public void test5() {
System.out.println("Map Reduce");
Optional<Double> allPrice = books().stream().map((book)->book.getPrice()*book.getId()).reduce((sum, aDouble2) -> sum + aDouble2);
System.out.println("统计所有商品总和:"+allPrice.orElse(0D));
System.out.println();
System.out.println("统计:(和、最大、最小、平均)");
DoubleSummaryStatistics statistics = books().stream().mapToDouble((book)->book.getPrice()*book.getId()).summaryStatistics();
System.out.println("average:"+statistics.getAverage());
System.out.println("max:"+statistics.getMax());
System.out.println("min:"+statistics.getMin());
System.out.println("count:"+statistics.getCount());
}
private void execute(Book book) {
System.out.println("吧书本的name变成大写");
if (book != null) {
String name = book.getName();
if (name != null) {
System.out.println(name.toUpperCase());
}
}
}
private void executeByOptional(Book book) {
System.out.println("把书本的name变成大写 -- optional");
String str = Optional.ofNullable(book).map(Book::getName).map(String::toUpperCase).orElse(null);
System.out.println(str);
}
private List<Book> books() {
List<Book> books = new ArrayList<>();
books.add(new Book(1L, "java", 99D, LocalDate.parse("2018-12-11")));
books.add(new Book(2L, "php", 55D, LocalDate.parse("1992-09-16")));
books.add(new Book(3L, "python", 55D, LocalDate.parse("2018-12-11")));
books.add(new Book(4L, "vue", 55D, LocalDate.parse("2018-12-11")));
books.add(new Book(5L, "jquery", 55D,LocalDate.parse("2018-12-11")));
books.add(new Book(6L, "linux", 55D, LocalDate.parse("2018-12-11")));
books.add(new Book(77L, "tomcat", 76D,LocalDate.parse("2018-12-11")));
books.add(new Book(8L, "nginx", 99D, LocalDate.parse("2011-01-14")));
return books;
}
private class Book {
private Long id;
private String name;
private Double price;
private LocalDate publishTime;
public Book() {
}
public Book(Long id, String name, Double price, LocalDate publishTime) {
this.id = id;
this.name = name;
this.price = price;
this.publishTime = publishTime;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public LocalDate getPublishTime() {
return publishTime;
}
public void setPublishTime(LocalDate publishTime) {
this.publishTime = publishTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Objects.equals(price, book.price) &&
Objects.equals(publishTime, book.publishTime);
}
@Override
public int hashCode() {
return Objects.hash(price, publishTime);
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", publishTime=" + publishTime +
'}';
}
}