當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Stream map()用法及代碼示例


流映射(函數映射器)返回一個流,該流包含將給定函數應用於此流的元素的結果。

流映射(函數映射器)是一個中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。

用法:


<R> Stream<R> map(Function<? super T, ? extends R> mapper)

where, R is the element type of the new stream.
Stream is an interface and T is the type 
of stream elements. mapper is a stateless function 
which is applied to each element and the function
returns the new stream.

示例1:流map()函數,對流的每個元素進行數字* 3運算。

// Java code for Stream map(Function mapper) 
// to get a stream by applying the 
// given function to this stream. 
import java.util.*; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        System.out.println("The stream after applying "
                           + "the function is : "); 
  
        // Creating a list of Integers 
        List<Integer> list = Arrays.asList(3, 6, 9, 12, 15); 
  
        // Using Stream map(Function mapper) and 
        // displaying the corresponding new stream 
        list.stream().map(number -> number * 3).forEach(System.out::println); 
    } 
}

輸出:

The stream after applying the function is : 
9
18
27
36
45

示例2:流map()函數,並將小寫轉換為大寫。

// Java code for Stream map(Function mapper) 
// to get a stream by applying the 
// given function to this stream. 
import java.util.*; 
import java.util.stream.Collectors; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        System.out.println("The stream after applying "
                           + "the function is : "); 
  
        // Creating a list of Integers 
        List<String> list = Arrays.asList("geeks", "gfg", "g", 
                                          "e", "e", "k", "s"); 
  
        // Using Stream map(Function mapper) to 
        // convert the Strings in stream to 
        // UpperCase form 
        List<String> answer = list.stream().map(String::toUpperCase). 
        collect(Collectors.toList()); 
  
        // displaying the new stream of UpperCase Strings 
        System.out.println(answer); 
    } 
}

輸出:

The stream after applying the function is : 
[GEEKS, GFG, G, E, E, K, S]

示例3:流map()函數,使用映射字符串長度的操作代替字符串。

// Java code for Stream map(Function mapper) 
// to get a stream by applying the 
// given function to this stream. 
import java.util.*; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        System.out.println("The stream after applying "
                           + "the function is : "); 
  
        // Creating a list of Strings 
        List<String> list = Arrays.asList("Geeks", "FOR", "GEEKSQUIZ", 
                                          "Computer", "Science", "gfg"); 
  
        // Using Stream map(Function mapper) and 
        // displaying the length of each String 
        list.stream().map(str -> str.length()).forEach(System.out::println); 
    } 
}

輸出:

The stream after applying the function is : 
5
3
9
8
7
3


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Stream map() in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。