您好,登錄后才能下訂單哦!
Java8中map()方法和flatMap()方法的使用?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
兩個方法的背景
這兩個方法看起來做著同樣的事情,但實際上又有些不一樣。看源碼部分是這樣的
package java.util.stream;
map()方法
/** * @param <R> The element type of the new stream * @param mapper a <a href="package-summary.html#NonInterference" rel="external nofollow" rel="external nofollow" >non-interfering</a>, * <a href="package-summary.html#Statelessness" rel="external nofollow" rel="external nofollow" >stateless</a> * function to apply to each element * @return the new stream */ <R> Stream<R> map(Function<? super T, ? extends R> mapper);
flatMap()方法
/** * @param <R> The element type of the new stream * @param mapper a <a href="package-summary.html#NonInterference" rel="external nofollow" rel="external nofollow" >non-interfering</a>, * <a href="package-summary.html#Statelessness" rel="external nofollow" rel="external nofollow" >stateless</a> * function to apply to each element which produces a stream * of new values * @return the new stream */ <R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
Stream map() Method
看源碼做推測,map是一種中間操作,返回的是Stream
代碼測試
map()方法
public static void main(String[] args) { System.out.println("Output with simple list"); List<String> vowels = Arrays.asList("A", "E", "I", "O", "U"); vowels.stream().map(vowel -> vowel.toLowerCase()) .forEach(value -> System.out.println(value)); List<String> haiList = new ArrayList<>(); haiList.add("hello"); haiList.add("hai"); haiList.add("hehe"); haiList.add("hi"); System.out.println("Output with nested List of List<String>"); List<String> welcomeList = new ArrayList<>(); welcomeList.add("You got it"); welcomeList.add("Don't mention it"); welcomeList.add("No worries."); welcomeList.add("Not a problem"); List<List<String>> nestedList = Arrays.asList(haiList, welcomeList); nestedList.stream().map(list -> { return list.stream().map(value -> value.toUpperCase()); }).forEach(value -> System.out.println(value)); }
Output
Output with simple list a e i o u Output with nested List of List<String> java.util.stream.ReferencePipeline$3@3b9a45b3 java.util.stream.ReferencePipeline$3@7699a589
flatMap()方法
public static void main(String[] args) { List<String> haiList = new ArrayList<>(); haiList.add("hello"); haiList.add("hai"); haiList.add("hehe"); haiList.add("hi"); System.out.println("Output with nested List of List<String>"); List<String> welcomeList = new ArrayList<>(); welcomeList.add("You got it"); welcomeList.add("Don't mention it"); welcomeList.add("No worries."); welcomeList.add("Not a problem"); List<List<String>> nestedList = Arrays.asList(haiList, welcomeList); nestedList.stream().flatMap( list -> list.stream()) .map(value -> value.toUpperCase()) .forEach(value -> System.out.println(value)); }
Output
Output with nested List of List<String> HELLO HAI HEHE HI YOU GOT IT DON'T MENTION IT NO WORRIES. NOT A PROBLEM
Java 8 map() vs flatMap()
代碼
public static void main(String[] args) { List<Stream> together = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4)) // Stream of List<Integer> .map(List::stream) .collect(Collectors.toList()); System.out.println("Output with map() -> "+together); List<Integer> togetherFlatMap = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4)) // Stream of List<Integer> .flatMap(List::stream) .map(integer -> integer + 1) .collect(Collectors.toList()); System.out.println("Output with flatMap() -> "+togetherFlatMap); }
Output
Output with map() -> [java.util.stream.ReferencePipeline$Head@16b98e56, java.util.stream.ReferencePipeline$Head@7ef20235] Output with flatMap() -> [2, 3, 4, 5]
關于Java8中map()方法和flatMap()方法的使用問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。