一. 簡介java
1. 在語言層面上修改現有的Java,增長Lambda表達式,讓代碼在多核CPU上高效運行;git
2. 面向對象編程是對數據進行抽象,函數式編程是對行爲進行抽象;express
3. 一個簡單的例子編程
botton.addActionListener(new ActionListener() { public void actionPerformed() { System.out.println("button clicked"); } });
匿名內部類的目的是爲了方便將代碼做爲數據傳遞;數組
button.addActionListener(event -> System.out.println("botton clicked"));
和傳入一個實現某接口的對象不一樣,咱們傳入了一段代碼塊,event是參數名,無需指定類型,javac根據程序的上下文能夠推斷出參數的類型,->將參數和Lambda表達式的主體分開,主體是用戶點擊按鈕時會運行的一些代碼;app
4. Lambda表達式的幾種不一樣形式函數式編程
Runnable noArguments = () -> System.out.print("Hello world"); // 使用空括號表示沒有參數 ActionListener oneArgument = event -> System.out.print("button clicked"); // 有且只有一個參數能夠省略參數的括號 Runnable multiStatement = () -> { // 表達式主體能夠是一段代碼塊 System.out.print("Hello"); System.out.println("world"); } BinaryOperator<Long> add = (x, y) -> x + y; // 包含多個參數;也能夠顯示聲明參數類型
5. 引用值而不是變量函數
能夠引用非final變量,可是該變量在既成事實上必須是final;this
若是試圖給變量屢次賦值,而後在Lambda表達式中引用它,編譯器會報錯:local variables referenced from a Lambda expression must be final or effectively final;spa
// name是一個既成事實上的final變量 String name = getUserName(); button.addActionListener(event -> System.out.println("hi" + name)); // 沒法經過編譯 String name = getUserName(); name = formatUserName(name); button.addActionListener(event -> System.out.println("hi" + name));
6. 函數接口
函數接口是隻有一個抽象方法的接口,用做Lambda表達式;
一些重要的函數接口:
Predicate<T> 參數T,返回類型boolean;
Consumer<T> 參數T,返回類型void;
Function<T, R> 參數T,返回類型R;
Supplier<T> 參數None,返回類型T;
UnaryOperator<T> 參數T,返回類型T;
BinaryOperator<T> 參數(T, T),返回類型T;
7. 方法引用
artist -> artist.getName() 能夠寫成 Artist::getName;
標準語法爲Classname::methodName;
二. 流
經常使用的流操做
List<String> collected = Stream.of("a", "b", "c").collect(Collectors.toList()); List<String> collected = Stream.of("a", "b", "c").map(string -> string.toUpperCase()).collect(Collectors.toList()); List<String> collected = Stream.of("a", "1abc", "abc1").filter(value -> isDigit(value.charAt(0))).collect(toList()); List<Integer> together = Stream.of(asList(1,2), asList(3,4)).flatMap(numbers -> numbers.stream()).collect(toList()); Track shortestTrack = tracks.stream().min(Comparator.comparing(track -> track.getLength())).get(); int count = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
List<Integer> sameOrder = numbers.stream().sorted().collect(toList());
stream.collect(toCollection(TreeSet::new));
artists.collect(maxBy(comparing(getCount)));
albums.stream().collect(averagingInt(album -> album.getTrackList().size()));
artists.collect(partitioningBy(artist -> artist.isSolo()));
albums.collect(groupingBy(album -> album.getMainMusician()));
artists.stream().map(Artist::getName).collect(Collectors.joining(",", "[", "]"));
albums.collect(groupingBy(Album::getMainMusician, mapping(Album::getName, toList())));
artistCache.computeIfAbsent(name, this::readArtistFromDB);
albumsByArtist.forEach((artist, albums) -> {countOfAlbums.put(artist, albums.size())});
album.getMusicians().peek(nation -> System.out.print("Found nationality:" + nation)).collect(Collectors.<String>toSet());
三. 數據並行化
albums.parallelStream().flatMap(Album::getTracks).mapToInt(Track::getLength).sum();
數組上的並行化操做:
parallelPrefix();parallelSetAll();parallelSort();