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


Java Stream sorted(Comparator comparator)用法及代碼示例


Stream sorted(比較器比較器)返回一個包含此流元素的流,並根據提供的Comparator對其進行排序。對於有序流,sort方法是穩定的,但對於無序流,則不能保證穩定性。這是有狀態的中間操作,即在處理新元素時,它可以合並先前看到的元素的狀態。在Java 8中,可以使用lambda表達式實例化Comparator。我們還可以顛倒自然順序以及Comparator提供的順序。句法:

Stream<T> sorted(Comparator<? super T> comparator)

Where, Stream is an interface and T
is the type of stream elements.
comparator is used to compare stream elements.

下麵給出一些示例,以更好地理解該函數的實現。

範例1:


// Implementation of Stream.sorted() 
// to get a stream of sorted elements 
// according to the provided Comparator 
import java.util.*; 
import java.util.stream.Stream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        // Creating a list of Integers 
        List<Integer> list = Arrays.asList(5, -10, 7, -18, 23); 
  
        System.out.println("The sorted stream according "
                           + "to provided Comparator is:"); 
  
        // Displaying the list of Strings in 
        // reverse order after sorting 
        list.stream().sorted(Comparator.reverseOrder()). 
                          forEach(System.out::println); 
    } 
}

輸出:

The sorted stream according to provided Comparator is:
23
7
5
-10
-18

範例2:

// Implementation of Stream.sorted() 
// to get a stream of sorted elements 
// according to the provided Comparator 
import java.util.*; 
import java.util.stream.Stream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        // Creating a list of Strings 
        List<String> list = Arrays.asList("Geeks", "for", 
                    "GeeksforGeeks", "GeeksQuiz", "GFG"); 
  
        System.out.println("The sorted stream according "
                           + "to provided Comparator is:"); 
  
        // Displaying the list of Strings in 
        // reverse order after sorting 
        list.stream().sorted(Comparator.reverseOrder()). 
                            forEach(System.out::println); 
    } 
}

輸出:

The sorted stream according to provided Comparator is:
for
GeeksforGeeks
GeeksQuiz
Geeks
GFG

範例3:

// Implementation of Stream.sorted() 
// to get a stream of sorted elements 
import java.util.*; 
import java.util.stream.Stream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        // Creating an array of Strings 
        String[] array = { "GFG", "Geeks", "for", 
                           "GeeksforGeeks", "GeeksQuiz" }; 
  
        System.out.println("The sorted stream is:"); 
  
        // sorting the elements of array based 
        // on their last character 
        Stream.of(array).sorted((str1, str2) 
                     -> Character.compare(str1 
                     .charAt(str1.length() - 1), 
                    str2.charAt(str2.length() - 1))) 
            .         forEach(System.out::println); 
    } 
}

輸出:

The sorted stream is:
GFG
for
Geeks
GeeksforGeeks
GeeksQuiz


相關用法


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