Java 8 簡明中文教程
歡迎閱讀我編寫的Java 8介紹。本教程將帶領你一步一步地認識這門語言的新特性。通過簡單明了的代碼示例,你將會學習到如何使用默認接口方法,Lambda表達式,方法引用和重復注解。看完這篇教程后,你還將對最新推出的API有一定的了解,例如:流控制,函數式接口,map擴展和新的時間日期API等等。
允許在接口中有默認方法實現
Java 8 允許我們使用default關鍵字,為接口聲明添加非抽象的方法實現。這個特性又被稱為擴展方法。下面是我們的第一個例子:
interface Formula { double calculate(int a); default double sqrt(int a) { return Math.sqrt(a); } }
在接口Formula中,除了抽象方法caculate以外,還定義了一個默認方法sqrt。Formula的實現類只需要實現抽象方法caculate就可以了。默認方法sqrt可以直接使用。
Formula formula = new Formula() { @Override public double calculate(int a) { return sqrt(a * 100); } }; formula.calculate(100); // 100.0 formula.sqrt(16); // 4.0
formula對象以匿名對象的形式實現了Formula接口。代碼很啰嗦:用了6行代碼才實現了一個簡單的計算功能:a*100開平方根。我們在下一節會看到,Java 8 還有一種更加優美的方法,能夠實現包含單個函數的對象。
Lambda表達式
讓我們從最簡單的例子開始,來學習如何對一個string列表進行排序。我們首先使用Java 8之前的方法來實現:
List<String> names = Arrays.asList("peter", "anna", "mike", "xenia"); Collections.sort(names, new Comparator<String>() { @Override public int compare(String a, String b) { return b.compareTo(a); } });
靜態工具方法Collections.sort接受一個list,和一個Comparator接口作為輸入參數,Comparator的實現類可以對輸入的list中的元素進行比較。通常情況下,你可以直接用創建匿名Comparator對象,并把它作為參數傳遞給sort方法。
除了創建匿名對象以外,Java 8 還提供了一種更簡潔的方式,Lambda表達式。
Collections.sort(names, (String a, String b) -> { return b.compareTo(a); });
你可以看到,這段代碼就比之前的更加簡短和易讀。但是,它還可以更加簡短:
Collections.sort(names, (String a, String b) -> b.compareTo(a));
只要一行代碼,包含了方法體。你甚至可以連大括號對{}和return關鍵字都省略不要。不過這還不是最短的寫法:
Collections.sort(names, (a, b) -> b.compareTo(a));
Java編譯器能夠自動識別參數的類型,所以你就可以省略掉類型不寫。讓我們再深入地研究一下lambda表達式的威力吧。
函數式接口
Lambda表達式如何匹配Java的類型系統?每一個lambda都能夠通過一個特定的接口,與一個給定的類型進行匹配。一個所謂的函數式接口必須要有且僅有一個抽象方法聲明。每個與之對應的lambda表達式必須要與抽象方法的聲明相匹配。由于默認方法不是抽象的,因此你可以在你的函數式接口里任意添加默認方法。
任意只包含一個抽象方法的接口,我們都可以用來做成lambda表達式。為了讓你定義的接口滿足要求,你應當在接口前加上@FunctionalInterface 標注。編譯器會注意到這個標注,如果你的接口中定義了第二個抽象方法的話,編譯器會拋出異常。
舉例:
@FunctionalInterface interface Converter<F, T> { T convert(F from); } Converter<String, Integer> converter = (from) -> Integer.valueOf(from); Integer converted = converter.convert("123"); System.out.println(converted); // 123
注意,如果你不寫@FunctionalInterface 標注,程序也是正確的。
方法和構造函數引用
上面的代碼實例可以通過靜態方法引用,使之更加簡潔:
Converter<String, Integer> converter = Integer::valueOf; Integer converted = converter.convert("123"); System.out.println(converted); // 123
Java 8 允許你通過::關鍵字獲取方法或者構造函數的的引用。上面的例子就演示了如何引用一個靜態方法。而且,我們還可以對一個對象的方法進行引用:
class Something { String startsWith(String s) { return String.valueOf(s.charAt(0)); } } Something something = new Something(); Converter<String, String> converter = something::startsWith; String converted = converter.convert("Java"); System.out.println(converted); // "J"
讓我們看看如何使用::關鍵字引用構造函數。首先我們定義一個示例bean,包含不同的構造方法:
class Person { String firstName; String lastName; Person() {} Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } }
接下來,我們定義一個person工廠接口,用來創建新的person對象:
interface PersonFactory<P extends Person> { P create(String firstName, String lastName); }
然后我們通過構造函數引用來把所有東西拼到一起,而不是像以前一樣,通過手動實現一個工廠來這么做。
PersonFactory<Person> personFactory = Person::new; Person person = personFactory.create("Peter", "Parker");
我們通過Person::new來創建一個Person類構造函數的引用。Java編譯器會自動地選擇合適的構造函數來匹配PersonFactory.create函數的簽名,并選擇正確的構造函數形式。
Lambda的范圍
對于lambdab表達式外部的變量,其訪問權限的粒度與匿名對象的方式非常類似。你能夠訪問局部對應的外部區域的局部final變量,以及成員變量和靜態變量。
訪問局部變量
我們可以訪問lambda表達式外部的final局部變量:
final int num = 1; Converter<Integer, String> stringConverter = (from) -> String.valueOf(from + num); stringConverter.convert(2); // 3
但是與匿名對象不同的是,變量num并不需要一定是final。下面的代碼依然是合法的:
int num = 1; Converter<Integer, String> stringConverter = (from) -> String.valueOf(from + num); stringConverter.convert(2); // 3
然而,num在編譯的時候被隱式地當做final變量來處理。下面的代碼就不合法:
int num = 1; Converter<Integer, String> stringConverter = (from) -> String.valueOf(from + num); num = 3;
在lambda表達式內部企圖改變num的值也是不允許的。
訪問成員變量和靜態變量
與局部變量不同,我們在lambda表達式的內部能獲取到對成員變量或靜態變量的讀寫權。這種訪問行為在匿名對象里是非常典型的。
class Lambda4 { static int outerStaticNum; int outerNum; void testScopes() { Converter<Integer, String> stringConverter1 = (from) -> { outerNum = 23; return String.valueOf(from); }; Converter<Integer, String> stringConverter2 = (from) -> { outerStaticNum = 72; return String.valueOf(from); }; } }
訪問默認接口方法
還記得第一節里面formula的那個例子么? 接口Formula定義了一個默認的方法sqrt,該方法能夠訪問formula所有的對象實例,包括匿名對象。這個對lambda表達式來講則無效。
默認方法無法在lambda表達式內部被訪問。因此下面的代碼是無法通過編譯的:
Formula formula = (a) -> sqrt( a * 100);
內置函數式接口
JDK 1.8 API中包含了很多內置的函數式接口。有些是在以前版本的Java中大家耳熟能詳的,例如Comparator接口,或者Runnable接口。對這些現成的接口進行實現,可以通過@FunctionalInterface 標注來啟用Lambda功能支持。
此外,Java 8 API 還提供了很多新的函數式接口,來降低程序員的工作負擔。有些新的接口已經在Google Guava庫中很有名了。如果你對這些庫很熟的話,你甚至閉上眼睛都能夠想到,這些接口在類庫的實現過程中起了多么大的作用。
Predicates
Predicate是一個布爾類型的函數,該函數只有一個輸入參數。Predicate接口包含了多種默認方法,用于處理復雜的邏輯動詞(and, or,negate)
Predicate<String> predicate = (s) -> s.length() > 0; predicate.test("foo"); // true predicate.negate().test("foo"); // false Predicate<Boolean> nonNull = Objects::nonNull; Predicate<Boolean> isNull = Objects::isNull; Predicate<String> isEmpty = String::isEmpty; Predicate<String> isNotEmpty = isEmpty.negate();
Functions
Function接口接收一個參數,并返回單一的結果。默認方法可以將多個函數串在一起(compse, andThen)
Function<String, Integer> toInteger = Integer::valueOf; Function<String, String> backToString = toInteger.andThen(String::valueOf); backToString.apply("123"); // "123"
Suppliers
Supplier接口產生一個給定類型的結果。與Function不同的是,Supplier沒有輸入參數。
Supplier<Person> personSupplier = Person::new; personSupplier.get(); // new Person
Consumers
Consumer代表了在一個輸入參數上需要進行的操作。
Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); greeter.accept(new Person("Luke", "Skywalker"));
Comparators
Comparator接口在早期的Java版本中非常著名。Java 8 為這個接口添加了不同的默認方法。
Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); Person p1 = new Person("John", "Doe"); Person p2 = new Person("Alice", "Wonderland"); comparator.compare(p1, p2); // > 0 comparator.reversed().compare(p1, p2); // < 0
Optionals
Optional不是一個函數式接口,而是一個精巧的工具接口,用來防止NullPointerEception產生。這個概念在下一節會顯得很重要,所以我們在這里快速地瀏覽一下Optional的工作原理。
Optional是一個簡單的值容器,這個值可以是null,也可以是non-null。考慮到一個方法可能會返回一個non-null的值,也可能返回一個空值。為了不直接返回null,我們在Java 8中就返回一個Optional.
Optional<String> optional = Optional.of("bam"); optional.isPresent(); // true optional.get(); // "bam" optional.orElse("fallback"); // "bam" optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b"
Streams
java.util.Stream表示了某一種元素的序列,在這些元素上可以進行各種操作。Stream操作可以是中間操作,也可以是完結操作。完結操作會返回一個某種類型的值,而中間操作會返回流對象本身,并且你可以通過多次調用同一個流操作方法來將操作結果串起來(就像StringBuffer的append方法一樣————譯者注)。Stream是在一個源的基礎上創建出來的,例如java.util.Collection中的list或者set(map不能作為Stream的源)。Stream操作往往可以通過順序或者并行兩種方式來執行。
我們先了解一下序列流。首先,我們通過string類型的list的形式創建示例數據:
List<String> stringCollection = new ArrayList<>(); stringCollection.add("ddd2"); stringCollection.add("aaa2"); stringCollection.add("bbb1"); stringCollection.add("aaa1"); stringCollection.add("bbb3"); stringCollection.add("ccc"); stringCollection.add("bbb2"); stringCollection.add("ddd1");
Java 8中的Collections類的功能已經有所增強,你可以之直接通過調用Collections.stream()或者Collection.parallelStream()方法來創建一個流對象。下面的章節會解釋這個最常用的操作。
Filter
Filter接受一個predicate接口類型的變量,并將所有流對象中的元素進行過濾。該操作是一個中間操作,因此它允許我們在返回結果的基礎上再進行其他的流操作(forEach)。ForEach接受一個function接口類型的變量,用來執行對每一個元素的操作。ForEach是一個中止操作。它不返回流,所以我們不能再調用其他的流操作。
stringCollection .stream() .filter((s) -> s.startsWith("a")) .forEach(System.out::println); // "aaa2", "aaa1"
Sorted
Sorted是一個中間操作,能夠返回一個排過序的流對象的視圖。流對象中的元素會默認按照自然順序進行排序,除非你自己指定一個Comparator接口來改變排序規則。
stringCollection .stream() .sorted() .filter((s) -> s.startsWith("a")) .forEach(System.out::println); // "aaa1", "aaa2"
一定要記住,sorted只是創建一個流對象排序的視圖,而不會改變原來集合中元素的順序。原來string集合中的元素順序是沒有改變的。
System.out.println(stringCollection); // ddd2, aaa2, bbb1, aaa1, bbb3, ccc, bbb2, ddd1
Map
map是一個對于流對象的中間操作,通過給定的方法,它能夠把流對象中的每一個元素對應到另外一個對象上。下面的例子就演示了如何把每個string都轉換成大寫的string. 不但如此,你還可以把每一種對象映射成為其他類型。對于帶泛型結果的流對象,具體的類型還要由傳遞給map的泛型方法來決定。
stringCollection .stream() .map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a)) .forEach(System.out::println); // "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1"
Match
匹配操作有多種不同的類型,都是用來判斷某一種規則是否與流對象相互吻合的。所有的匹配操作都是終結操作,只返回一個boolean類型的結果。
boolean anyStartsWithA = stringCollection .stream() .anyMatch((s) -> s.startsWith("a")); System.out.println(anyStartsWithA); // true boolean allStartsWithA = stringCollection .stream() .allMatch((s) -> s.startsWith("a")); System.out.println(allStartsWithA); // false boolean noneStartsWithZ = stringCollection .stream() .noneMatch((s) -> s.startsWith("z")); System.out.println(noneStartsWithZ); // true
Count
Count是一個終結操作,它的作用是返回一個數值,用來標識當前流對象中包含的元素數量。
long startsWithB = stringCollection .stream() .filter((s) -> s.startsWith("b")) .count(); System.out.println(startsWithB); // 3
Reduce
該操作是一個終結操作,它能夠通過某一個方法,對元素進行削減操作。該操作的結果會放在一個Optional變量里返回。
Optional<String> reduced = stringCollection .stream() .sorted() .reduce((s1, s2) -> s1 + "#" + s2); reduced.ifPresent(System.out::println); // "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2"
Parallel Streams
像上面所說的,流操作可以是順序的,也可以是并行的。順序操作通過單線程執行,而并行操作則通過多線程執行。
下面的例子就演示了如何使用并行流進行操作來提高運行效率,代碼非常簡單。
首先我們創建一個大的list,里面的元素都是唯一的:
int max = 1000000; List<String> values = new ArrayList<>(max); for (int i = 0; i < max; i++) { UUID uuid = UUID.randomUUID(); values.add(uuid.toString()); }
現在,我們測量一下對這個集合進行排序所使用的時間。
順序排序
long t0 = System.nanoTime(); long count = values.stream().sorted().count(); System.out.println(count); long t1 = System.nanoTime(); long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("sequential sort took: %d ms", millis)); // sequential sort took: 899 ms
并行排序
long t0 = System.nanoTime(); long count = values.parallelStream().sorted().count(); System.out.println(count); long t1 = System.nanoTime(); long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("parallel sort took: %d ms", millis)); // parallel sort took: 472 ms
如你所見,所有的代碼段幾乎都相同,唯一的不同就是把stream()改成了parallelStream(), 結果并行排序快了50%。
Map
正如前面已經提到的那樣,map是不支持流操作的。而更新后的map現在則支持多種實用的新方法,來完成常規的任務。
Map<Integer, String> map = new HashMap<>(); for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "val" + i); } map.forEach((id, val) -> System.out.println(val));
上面的代碼風格是完全自解釋的:putIfAbsent避免我們將null寫入;forEach接受一個消費者對象,從而將操作實施到每一個map中的值上。
下面的這個例子展示了如何使用函數來計算map的編碼
map.computeIfPresent(3, (num, val) -> val + num); map.get(3); // val33 map.computeIfPresent(9, (num, val) -> null); map.containsKey(9); // false map.computeIfAbsent(23, num -> "val" + num); map.containsKey(23); // true map.computeIfAbsent(3, num -> "bam"); map.get(3); // val33
接下來,我們將學習,當給定一個key值時,如何把一個實例從對應的key中移除:
map.remove(3, "val3"); map.get(3); // val33 map.remove(3, "val33"); map.get(3); // null
另一個有用的方法:
map.getOrDefault(42, "not found"); // not found
將map中的實例合并也是非常容易的:
map.merge(9, "val9", (value, newValue) -> value.concat(newValue)); map.get(9); // val9 map.merge(9, "concat", (value, newValue) -> value.concat(newValue)); map.get(9); // val9concat
合并操作先看map中是否沒有特定的key/value存在,如果是,則把key/value存入map,否則merging函數就會被調用,對現有的數值進行修改。
時間日期API
Java 8 包含了全新的時間日期API,這些功能都放在了java.time包下。新的時間日期API是基于Joda-Time庫開發的,但是也不盡相同。下面的例子就涵蓋了大多數新的API的重要部分。
Clock
Clock提供了對當前時間和日期的訪問功能。Clock是對當前時區敏感的,并可用于替代System.currentTimeMillis()方法來獲取當前的毫秒時間。當前時間線上的時刻可以用Instance類來表示。Instance也能夠用于創建原先的java.util.Date對象。
Clock clock = Clock.systemDefaultZone(); long millis = clock.millis(); Instant instant = clock.instant(); Date legacyDate = Date.from(instant); // legacy java.util.Date
Timezones
時區類可以用一個ZoneId來表示。時區類的對象可以通過靜態工廠方法方便地獲取。時區類還定義了一個偏移量,用來在當前時刻或某時間與目標時區時間之間進行轉換。
System.out.println(ZoneId.getAvailableZoneIds()); // prints all available timezone ids ZoneId zone1 = ZoneId.of("Europe/Berlin"); ZoneId zone2 = ZoneId.of("Brazil/East"); System.out.println(zone1.getRules()); System.out.println(zone2.getRules()); // ZoneRules[currentStandardOffset=+01:00] // ZoneRules[currentStandardOffset=-03:00]
LocalTime
本地時間類表示一個沒有指定時區的時間,例如,10 p.m.或者17:30:15,下面的例子會用上面的例子定義的時區創建兩個本地時間對象。然后我們會比較兩個時間,并計算它們之間的小時和分鐘的不同。
LocalTime now1 = LocalTime.now(zone1); LocalTime now2 = LocalTime.now(zone2); System.out.println(now1.isBefore(now2)); // false long hoursBetween = ChronoUnit.HOURS.between(now1, now2); long minutesBetween = ChronoUnit.MINUTES.between(now1, now2); System.out.println(hoursBetween); // -3 System.out.println(minutesBetween); // -239
LocalTime是由多個工廠方法組成,其目的是為了簡化對時間對象實例的創建和操作,包括對時間字符串進行解析的操作。
LocalTime late = LocalTime.of(23, 59, 59); System.out.println(late); // 23:59:59 DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedTime(FormatStyle.SHORT) .withLocale(Locale.GERMAN); LocalTime leetTime = LocalTime.parse("13:37", germanFormatter); System.out.println(leetTime); // 13:37
LocalDate
本地時間表示了一個獨一無二的時間,例如:2014-03-11。這個時間是不可變的,與LocalTime是同源的。下面的例子演示了如何通過加減日,月,年等指標來計算新的日期。記住,每一次操作都會返回一個新的時間對象。
LocalDate today = LocalDate.now(); LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); LocalDate yesterday = tomorrow.minusDays(2); LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); System.out.println(dayOfWeek); // FRIDAY<span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px;">Parsing a LocalDate from a string is just as simple as parsing a LocalTime:</span>
解析字符串并形成LocalDate對象,這個操作和解析LocalTime一樣簡單。
DateTimeFormatter germanFormatter = DateTimeFormatter .ofLocalizedDate(FormatStyle.MEDIUM) .withLocale(Locale.GERMAN); LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter); System.out.println(xmas); // 2014-12-24
LocalDateTime
LocalDateTime表示的是日期-時間。它將剛才介紹的日期對象和時間對象結合起來,形成了一個對象實例。LocalDateTime是不可變的,與LocalTime和LocalDate的工作原理相同。我們可以通過調用方法來獲取日期時間對象中特定的數據域。
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59); DayOfWeek dayOfWeek = sylvester.getDayOfWeek(); System.out.println(dayOfWeek); // WEDNESDAY Month month = sylvester.getMonth(); System.out.println(month); // DECEMBER long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY); System.out.println(minuteOfDay); // 1439
如果再加上的時區信息,LocalDateTime能夠被轉換成Instance實例。Instance能夠被轉換成以前的java.util.Date對象。
Instant instant = sylvester .atZone(ZoneId.systemDefault()) .toInstant(); Date legacyDate = Date.from(instant); System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014
格式化日期-時間對象就和格式化日期對象或者時間對象一樣。除了使用預定義的格式以外,我們還可以創建自定義的格式化對象,然后匹配我們自定義的格式。
DateTimeFormatter formatter = DateTimeFormatter .ofPattern("MMM dd, yyyy - HH:mm"); LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter); String string = formatter.format(parsed); System.out.println(string); // Nov 03, 2014 - 07:13
不同于java.text.NumberFormat,新的DateTimeFormatter類是不可變的,也是線程安全的。
更多的細節,請看這里
Annotations
Java 8中的注解是可重復的。讓我們直接深入看看例子,弄明白它是什么意思。
首先,我們定義一個包裝注解,它包括了一個實際注解的數組
@interface Hints { Hint[] value(); } @Repeatable(Hints.class) @interface Hint { String value(); }
只要在前面加上注解名:@Repeatable,Java 8 允許我們對同一類型使用多重注解,
變體1:使用注解容器(老方法)
@Hints({@Hint("hint1"), @Hint("hint2")}) class Person {}
變體2:使用可重復注解(新方法)
@Hint("hint1") @Hint("hint2") class Person {}
使用變體2,Java編譯器能夠在內部自動對@Hint進行設置。這對于通過反射來讀取注解信息來說,是非常重要的。
Hint hint = Person.class.getAnnotation(Hint.class); System.out.println(hint); // null Hints hints1 = Person.class.getAnnotation(Hints.class); System.out.println(hints1.value().length); // 2 Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class); System.out.println(hints2.length); // 2
盡管我們絕對不會在Person類上聲明@Hints注解,但是它的信息仍然可以通過getAnnotation(Hints.class)來讀取。并且,getAnnotationsByType方法會更方便,因為它賦予了所有@Hints注解標注的方法直接的訪問權限。
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) @interface MyAnnotation {}
先到這里
我的Java 8編程指南就到此告一段落。當然,還有很多內容需要進一步研究和說明。這就需要靠讀者您來對JDK 8進行探究了,例如:Arrays.parallelSort, StampedLock和CompletableFuture等等 ———— 我這里只是舉幾個例子而已。
我希望這個博文能夠對您有所幫助,也希望您閱讀愉快。完整的教程源代碼放在了GitHub上。您可以盡情地fork,并請通過推ter告訴我您的反饋。
原文鏈接: winterbe 翻譯: ImportNew.com - 黃小非
譯文鏈接: http://www.importnew.com/10360.html