Java 8的一個大亮點是引入Lambda表達式和Stream,使用它設計的代碼會更加簡潔php
特性:html
格式:java
(Object ...args) -> expr
複製代碼
方法的引用mysql
函數式接口:spring
public class Test {
public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("測試線程");
}
}).start();
new Thread(() -> {
System.out.println("lambda測試線程");
}).start();
/*-------------------------------------*/
List<String> list = Arrays.asList("aa", "bbb", "ccc", "ddddd");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
System.out.println(list.toString());
Collections.sort(list, (a,b)-> b.length() - a.length());
System.out.println(list.toString());
/*--------------------------------*/
Supplier<String> stringSupplier = (Supplier<String>)() -> "哈哈";
String s = stringSupplier.get();
System.out.println(s);
/*-------------------------------*/
//無參無返回值
Runnable r = () -> {
System.out.println("hello");
};
r.run();
/*--------------------------------*/
//無參有返回值
Callable c = new Callable() {
@Override
public Object call() throws Exception {
return "hello";
}
};
Callable c1 = () -> {
return "hello";
};
Callable c2 = () -> "hello";
System.out.println(c.call());
System.out.println(c1.call());
System.out.println(c2.call());
/*------------------------------------*/
//有參有返回值
Function<Integer, Integer> f = a -> {
int count = 0;
for (int i = 0; i <= a; i++) {
count += i;
}
return count;
};
System.out.println(f.apply(10));
BiFunction<Integer, Integer, Integer> bf = (a, b) -> a + b;
System.out.println(bf.apply(10, 20));
/*--------------------------*/
}
}
複製代碼
方法引用:sql
public class FuncTest {
public static void main(String[] args) {
//靜態方法引用
Function<String, String> f = a -> Fun.staticUp(a);
Function<String, String> f1 = Fun::staticUp;
System.out.println("大寫:" + f.apply("aaaaa"));
System.out.println("大寫:" + f1.apply("bbbbb"));
//實例方法引用
Function<String, String> f2 = a -> new Fun().up(a);
Function<String, String> f3 = new Fun()::up;
System.out.println("大寫:" + f2.apply("ccccc"));
System.out.println("大寫:" + f3.apply("ddddd"));
//對象方法引用
BiFunction<Fun, String, String> f4 = (fun, a) -> new Fun().upCase(a);
BiFunction<Fun, String, String> f5 = Fun::upCase;
System.out.println("大寫:" + f4.apply(new Fun(), "eeee"));
System.out.println("大寫:" + f5.apply(new Fun(), "ffff"));
//構造方法引用
Supplier<Fun> s = () -> new Fun();
Supplier<Fun> s1 = Fun::new;
Supplier<List> s2 = ArrayList::new;
s.get();
s1.get();
s2.get();
}
}
class Fun{
public static String staticUp(String a){
return a.toUpperCase();
}
public String up(String a){
return a.toUpperCase();
}
public String upCase(String a){
return a.toUpperCase();
}
public Fun(){
System.out.println("new Fun()");
}
}
複製代碼
Stream特性編程
public class StreamTest {
//數組
static void gen1(){
String[] arr = {"a","b","c","d"};
Stream<String> stream = Stream.of(arr);
}
//集合
static void gen2(){
List<String> list = Arrays.asList("a", "b", "c", "d");
Stream<String> stream = list.stream();
}
//generate
static void gen3(){
Stream<Integer> stream = Stream.generate(() -> 1);
stream.limit(10).forEach(System.out::println);
}
//iterate
static void gen4(){
Stream<Integer> stream = Stream.iterate(1, x -> 1);
stream.limit(10).forEach(System.out::println);
}
//其它api
static void gen5(){
String str = "qwer";
IntStream intStream = str.chars();
//intStream.forEach(x -> System.out.println(x));
intStream.forEach(System.out::println);
}
//獲取偶數
static void gen6(){
Stream<Integer> stream = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9).stream();
Stream<Integer> stream1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9).stream();
stream.filter(x -> x%2 == 0).forEach(System.out::println);
int sum = stream1.filter(x -> x % 2 == 0).mapToInt(x -> x).sum();
System.out.println("偶數和:" + sum);
}
//排序查找
static void gen7(){
Stream<Integer> stream = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9).stream();
Optional<Integer> any = stream.filter(x -> x % 2 == 0).sorted((a, b) -> b - a).findAny();
System.out.println(any.get());
}
//1-50 偶數存入新的list
static void gen8(){
List<Integer> list = Stream.iterate(1, x -> x + 1).limit(50).filter(x -> x % 2 == 0).collect(Collectors.toList());
System.out.println(list);
}
//去重
static void gen9(){
Stream<Integer> stream = Arrays.asList(2, 2, 3, 5, 5, 6, 7, 3, 9).stream();
Stream<Integer> stream1 = Arrays.asList(2, 2, 3, 5, 5, 6, 7, 3, 9).stream();
stream.distinct().forEach(System.out::println);
Set<Integer> set = stream1.collect(Collectors.toSet());
System.out.println(set);
}
//字符串分隔,轉成數字求和
static void gen10(){
String str = "9,8,7,6";
int sum = Stream.of(str.split(",")).mapToInt(x -> Integer.valueOf(x)).sum();
System.out.println(sum);
}
//url參數解析成map
static void gen11(){
String str = "id=1&name=tom&password=123456";
Map<String, String> map = Stream.of(str.split("&")).map(x -> x.split("=")).collect(Collectors.toMap(s -> s[0], s -> s[1]));
System.out.println(map);
}
public static void main(String[] args) {
//StreamTest.gen1();
//StreamTest.gen2();
//StreamTest.gen3();
//StreamTest.gen4();
//StreamTest.gen5();
//StreamTest.gen6();
//StreamTest.gen7();
//StreamTest.gen8();
//StreamTest.gen9();
//StreamTest.gen10();
StreamTest.gen11();
}
}
複製代碼
Lambda 和 Stream 實例:api
public class LambdaAndStreamUseTest {
public static void main(String[] args) {
// test1();
// test2();
// test3();
// test4();
test5();
}
//取屬性
private static void test1() {
//id集合
List<Integer> ids = books().stream().map(Book::getId).collect(Collectors.toList());
System.out.println("id集合:" + ids);
//拼接id
String str = books().stream().map(book -> "'" + book.getId() + "'").collect(Collectors.joining(",", "(", ")"));
System.out.println("拼接id:" + str);
}
//價格排序
private static void test2() {
//價格排序
List<Book> bookList = books().stream().sorted((book1, book2) -> Double.compare(book1.getPrice(), book2.getPrice())).collect(Collectors.toList());
// bookList.forEach(System.out::println);
//價格+發佈日期排序
Comparator<Book> priceComparator = Comparator.comparingDouble(Book::getPrice);
Comparator<Book> dateComparator = Comparator.comparing(Book::getPublishDate);
List<Book> bookList1 = books().stream().sorted(priceComparator.thenComparing(dateComparator.reversed())).collect(Collectors.toList());
bookList1.forEach(System.out::println);
}
//轉map
private static void test3() {
Map<Integer, Book> bookMap = books().stream().collect(Collectors.toMap(Book::getId, book -> book));
bookMap.forEach((key, value) -> System.out.println(key+ ":" + value));
}
//全部書的平均價
private static void test4() {
Double aDouble = books().stream().collect(Collectors.averagingDouble(Book::getPrice));
System.out.println("平均價:" + aDouble);
}
//分組統計
private static void test5() {
Map<Double, Long> map = books().stream().collect(Collectors.groupingBy(Book::getPrice, Collectors.counting()));
System.out.println("按價格統計:" + map);
Map<String, Double> map1 = books().stream().collect(Collectors.groupingBy(Book::getBookName, Collectors.summingDouble(Book::getPrice)));
System.out.println("按書名統計總金額:" + map1);
Map<String, Double> map2 = books().stream().collect(Collectors.groupingBy(Book::getBookName, Collectors.averagingDouble(Book::getPrice)));
System.out.println("按書名統計平均價格:" + map2);
Map<String, Optional<Book>> map3 = books().stream().collect(Collectors.groupingBy(Book::getBookName, Collectors.maxBy(Comparator.comparing(Book::getPrice))));
System.out.println("每種書最貴的價格:" + map3);
}
private static List<Book> books() {
List<Book> bookList = new ArrayList<>();
bookList.add(new Book(1, "java", 35d, LocalDate.parse("2019-06-06")));
bookList.add(new Book(2, "js", 35d, LocalDate.parse("2019-06-06")));
bookList.add(new Book(3, "php", 55d, LocalDate.parse("2019-03-06")));
bookList.add(new Book(4, "php", 35d, LocalDate.parse("2019-06-06")));
bookList.add(new Book(5, "html", 45d, LocalDate.parse("2019-06-06")));
bookList.add(new Book(6, "spring", 85d, LocalDate.parse("2019-07-06")));
bookList.add(new Book(7, "mybatis", 45d, LocalDate.parse("2019-06-06")));
bookList.add(new Book(8, "mysql", 35d, LocalDate.parse("2019-08-06")));
bookList.add(new Book(9, "spring", 55d, LocalDate.parse("2019-06-01")));
bookList.add(new Book(10, "springboot", 35d, LocalDate.parse("2019-03-06")));
return bookList;
}
}
class Book{
private Integer id;
private String bookName;
private Double price;
private LocalDate publishDate;
public Book(Integer id, String bookName, Double price, LocalDate publishDate) {
this.id = id;
this.bookName = bookName;
this.price = price;
this.publishDate = publishDate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public LocalDate getPublishDate() {
return publishDate;
}
public void setPublishDate(LocalDate publishDate) {
this.publishDate = publishDate;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", bookName='" + bookName + '\'' + ", price=" + price + ", publishDate=" + publishDate + '}'; } } 複製代碼