欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java8排序stream.sorted()的使用

 更新時(shí)間:2021年03月17日 09:55:22   作者:延陵縹緲  
這篇文章主要介紹了Java8排序stream.sorted()的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

    在這個(gè)頁面上我們將提供java 8 Stream sorted()示例。我們可以按照自然排序以及Comparator提供的排序?qū)α鬟M(jìn)行排序。在java 8中Comparator可以使用lambda表達(dá)式進(jìn)行實(shí)例化。我們還可以反轉(zhuǎn)自然排序以及提供的排序Comparator。自然排序使用提供的順序Comparable,必須由其實(shí)例是流元素的類實(shí)現(xiàn)。在這個(gè)頁面上我們將排序List,Map并Set使用java 8流sorted()方法。

1.sorted()方法的語法示例。 

1.1sorted():它使用自然順序?qū)α鞯脑剡M(jìn)行排序。元素類必須實(shí)現(xiàn)Comparable接口。 

按自然升序?qū)线M(jìn)行排序

list.stream().sorted() .stream().sorted();

自然序降序使用Comparator提供reverseOrder()方法

list.stream().sorted(Comparator.reverseOrder()) .stream().sorted(Comparator.reverseOrder());

1.2 sorted(Comparator<? super T> comparator):這里我們創(chuàng)建一個(gè)Comparator使用lambda表達(dá)式的實(shí)例。我們可以按升序和降序?qū)α髟剡M(jìn)行排序。 

使用Comparator來對(duì)列表進(jìn)行自定義升序。 

list.stream().sorted(Comparator.comparing(Student::getAge)) .stream().sorted(Comparator.comparing(Student::getAge));

使用Comparator提供reversed()方法來對(duì)列表進(jìn)行自定義降序。 。 

list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) .stream().sorted(Comparator.comparing(Student::getAge).reversed());

2.使用List流排序()

package com.stream.demo;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
 
public class StreamListDemo {
 public static void main(String[] args) {
 List<Student> list = new ArrayList<>();
 list.add(new Student(1, "Mahesh", 12));
 list.add(new Student(2, "Suresh", 15));
 list.add(new Student(3, "Nilesh", 10));
 
 System.out.println("---Natural Sorting by Name---");
 List<Student> slist = list.stream().sorted().collect(Collectors.toList());
 slist.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 
 System.out.println("---Natural Sorting by Name in reverse order---");
 slist = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
 slist.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 
 System.out.println("---Sorting using Comparator by Age---");
 slist = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
 slist.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 
 System.out.println("---Sorting using Comparator by Age with reverse order---");
 slist = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
 slist.forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 }
}
package com.stream.demo;
 
public class Student implements Comparable<Student> {
 private int id;
 private String name;
 private int age;
 
 public Student(int id, String name, int age) {
 this.id = id;
 this.name = name;
 this.age = age;
 }
 
 public int getId() {
 return id;
 }
 
 public String getName() {
 return name;
 }
 
 public int getAge() {
 return age;
 }
 
 @Override
 public int compareTo(Student ob) {
 return name.compareTo(ob.getName());
 }
 
 @Override
 public boolean equals(final Object obj) {
 if (obj == null) {
  return false;
 }
 final Student std = (Student) obj;
 if (this == std) {
  return true;
 } else {
  return (this.name.equals(std.name) && (this.age == std.age));
 }
 }
 
 @Override
 public int hashCode() {
 int hashno = 7;
 hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
 return hashno;
 }
}

執(zhí)行結(jié)果

---Natural Sorting by Name---
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Id:2, Name: Suresh, Age:15
---Natural Sorting by Name in reverse order---
Id:2, Name: Suresh, Age:15
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
---Sorting using Comparator by Age---
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
Id:2, Name: Suresh, Age:15
---Sorting using Comparator by Age with reverse order---
Id:2, Name: Suresh, Age:15
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10

3.使用set流排序

package com.stream.demo;
 
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
 
public class StreamSetDemo {
 public static void main(String[] args) {
 Set<Student> set = new HashSet<>();
 set.add(new Student(1, "Mahesh", 12));
 set.add(new Student(2, "Suresh", 15));
 set.add(new Student(3, "Nilesh", 10));
 
 System.out.println("---Natural Sorting by Name---");
 System.out.println("---Natural Sorting by Name---");
 set.stream().sorted().forEach(e -> System.out.println("Id:"
   + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 
 System.out.println("---Natural Sorting by Name in reverse order---");
 set.stream().sorted(Comparator.reverseOrder()).forEach(e -> System.out.println("Id:"
   + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 
 System.out.println("---Sorting using Comparator by Age---");
 set.stream().sorted(Comparator.comparing(Student::getAge))
   .forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 
 System.out.println("---Sorting using Comparator by Age in reverse order---");
 set.stream().sorted(Comparator.comparing(Student::getAge).reversed())
   .forEach(e -> System.out.println("Id:" + e.getId() + ", Name: " + e.getName() + ", Age:" + e.getAge()));
 }
}

4.使用Map流排序

package com.stream.demo;
 
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
 
public class StreamMapDemo {
 public static void main(String[] args) {
 Map<Integer, String> map = new HashMap<>();
 map.put(15, "Mahesh");
 map.put(10, "Suresh");
 map.put(30, "Nilesh");
 
 System.out.println("---Sort by Map Value---");
 map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
   .forEach(e -> System.out.println("Key: "+ e.getKey() +", Value: "+ e.getValue()));
 
 System.out.println("---Sort by Map Key---");System.out.println("---Sort by Map Key---");
 map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
   .forEach(e -> System.out.println("Key: "+ e.getKey() +", Value: "+ e.getValue()));
 }
}

這是在英文網(wǎng)站看到的示例,覺得還不錯(cuò)就翻譯過來了。

原文鏈接:http://www.concretepage.com/java/jdk-8/java-8-stream-sorted-example 

到此這篇關(guān)于Java8排序stream.sorted()的使用的文章就介紹到這了,更多相關(guān)Java8 stream.sorted()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot項(xiàng)目中連接Gauss數(shù)據(jù)庫

    SpringBoot項(xiàng)目中連接Gauss數(shù)據(jù)庫

    本文主要介紹了SpringBoot項(xiàng)目中連接Gauss數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • java為什么不建議用equals判斷對(duì)象相等

    java為什么不建議用equals判斷對(duì)象相等

    本文主要介紹了java為什么不建議用equals判斷對(duì)象相等,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • jdk自帶定時(shí)器使用方法詳解

    jdk自帶定時(shí)器使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了jdk自帶定時(shí)器的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • IDEA中WebService生成Java代碼并調(diào)用外部接口實(shí)現(xiàn)代碼

    IDEA中WebService生成Java代碼并調(diào)用外部接口實(shí)現(xiàn)代碼

    這篇文章主要介紹了IDEA中WebService生成Java代碼并調(diào)用外部接口實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • Java解析XML的四種方法詳解

    Java解析XML的四種方法詳解

    XML現(xiàn)在已經(jīng)成為一種通用的數(shù)據(jù)交換格式,平臺(tái)的無關(guān)性使得很多場合都需要用到XML。本文將詳細(xì)介紹用Java解析XML的四種方法
    2012-10-10
  • 解決Request.getParameter獲取不到特殊字符bug問題

    解決Request.getParameter獲取不到特殊字符bug問題

    這篇文章主要介紹了解決Request.getParameter獲取不到特殊字符bug問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • java如何刪除以逗號(hào)隔開的字符串中某一個(gè)值

    java如何刪除以逗號(hào)隔開的字符串中某一個(gè)值

    這篇文章主要介紹了java如何刪除以逗號(hào)隔開的字符串中某一個(gè)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 使用IDEA如何拉取GitLab項(xiàng)目

    使用IDEA如何拉取GitLab項(xiàng)目

    使用IDEA拉取GitLab項(xiàng)目,首先需要組長提供的socket和賬號(hào)密碼登錄內(nèi)網(wǎng)的GitLab,打開IDEA,選擇新建項(xiàng)目,選擇Project from Version Control,然后在項(xiàng)目路徑后面添加.git,以上步驟為個(gè)人操作經(jīng)驗(yàn),希望能為大家提供參考
    2024-10-10
  • 詳解如何判斷Java線程池任務(wù)已執(zhí)行完

    詳解如何判斷Java線程池任務(wù)已執(zhí)行完

    線程池的使用并不復(fù)雜,麻煩的是如何判斷線程池中的任務(wù)已經(jīng)全部執(zhí)行完了,所以接下來,我們就來看看如何判斷線程中的任務(wù)是否已經(jīng)全部執(zhí)行完吧
    2023-08-08
  • Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口

    Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口

    這篇文章主要給大家介紹了關(guān)于Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06

最新評(píng)論