iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java8排序stream.sorted()的使用
  • 204
分享到

Java8排序stream.sorted()的使用

2024-04-02 19:04:59 204人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

    在这个页面上我们将提供java 8 Stream sorted()示例。我们可以按照自然排序以及Comparator提供的排序对流进行

    在这个页面上我们将提供java 8 Stream sorted()示例。我们可以按照自然排序以及Comparator提供的排序对流进行排序。在java 8中Comparator可以使用lambda表达式进行实例化。我们还可以反转自然排序以及提供的排序Comparator。自然排序使用提供的顺序Comparable,必须由其实例是流元素的类实现。在这个页面上我们将排序List,Map并Set使用java 8流sorted()方法。

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

1.1sorted():它使用自然顺序对流的元素进行排序。元素类必须实现Comparable接口。 

按自然升序对集合进行排序


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

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


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

1.2 sorted(Comparator<? super T> comparator):这里我们创建一个Comparator使用lambda表达式的实例。我们可以按升序和降序对流元素进行排序。 

使用Comparator来对列表进行自定义升序。 


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

使用Comparator提供reversed()方法来对列表进行自定义降序。 。 


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;
 }
}

执行结果

---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()));
 }
}

这是在英文网站看到的示例,觉得还不错就翻译过来了。

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

到此这篇关于Java8排序stream.sorted()的使用的文章就介绍到这了,更多相关Java8 stream.sorted()内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java8排序stream.sorted()的使用

本文链接: https://www.lsjlt.com/news/121827.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java8排序stream.sorted()的使用
        在这个页面上我们将提供java 8 Stream sorted()示例。我们可以按照自然排序以及Comparator提供的排序对流进行...
    99+
    2024-04-02
  • Java8的Lambda和排序
    目录对数组和集合进行排序是Java 8 lambda令人惊奇的一个应用,我们可以实现一个Comparators来实现各种排序。 看下面案例: static class Perso...
    99+
    2024-04-02
  • java8 使用stream排序空字段排在前面或后面
    java8 stream排序空字段排在前面或后面 直接粗暴sorted会NPE,这种写法可以避免 list.stream().sorted(Comparator.comparin...
    99+
    2024-04-02
  • java8 stream多字段排序的实现
    很多情况下sql不好解决的多表查询,临时表分组,排序,尽量用java8新特性stream进行处理 使用java8新特性,下面先来点基础的 List<类> list; ...
    99+
    2024-04-02
  • java8 stream的多字段排序实现(踩坑)
    关于java8 的stream排序用法这里不做多说,这里介绍下曾经在多字段排序时遇到过的一个坑。 需求:需要根据id去分组,然后取出每组中行号最大的一个对象值。 想到可以利用stre...
    99+
    2024-04-02
  • Java8 Collectors.toMap() 的使用
    目录 一、简单介绍用法1:根据某一属性,对对象的实例或属性做映射用法2:根据某一属性,对对象集合进行去重 二、Duplicate key 异常1)异常重现:2)异常截图:3)异常说明:4...
    99+
    2023-09-12
    java jvm 面试
  • pandas.DataFrameSeries排序的使用(sort_values,sort_index)
    目录按元素排序sort_values()升序,降序(参数ascending)多列排序缺失值NaN的处理(参数na_position)更改原始对象(参数inplace)按行方向排序(参...
    99+
    2023-02-23
    pandas DataFrame Series排序 pandas DataFrame sort_values pandas DataFrame sort_index
  • Java使用DualPivotQuicksort排序
    目录1. 插入排序(insertion sort)2. 计数排序(counting sort)3. 快速排序(Quicksort)3.1 对数组做近似7等分3.2 对五个切割点进行插...
    99+
    2024-04-02
  • java8 stream排序以及自定义比较器方式
    目录java8 stream排序及自定义比较器java stream排序问题说下用法总结java8 stream排序及自定义比较器 使用java 8 stream 排序 LIst 以...
    99+
    2023-03-23
    java8 stream排序 自定义比较器 java8比较器
  • java8 stream排序及自定义比较器的方法是什么
    这篇文章主要讲解了“java8 stream排序及自定义比较器的方法是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“java8 stream排序及自定义比较器的方法是...
    99+
    2023-07-05
  • Java使用Collections.sort()排序的方法
    Java中Collections.sort()的使用 在日常开发中,很多时候都需要对一些数据进行排序的操作。然而那些数据一般都是放在一个集合中如:Map ,Set ,List 等集...
    99+
    2024-04-02
  • Pandas数值排序sort_values()的使用
    参数解释 DataFrame.sort_values(by, axis=0, ascending=True, ...
    99+
    2024-04-02
  • c语言冒泡排序和选择排序的使用代码
    目录1.冒泡排序2.选择排序区别总结1.冒泡排序 冒泡排序将一个列表中的两个元素进行比较,并将最小的元素交换到顶部。两个元素中较小的会冒到顶部,而较大的会沉到底部,该过程将被重复执行...
    99+
    2024-04-02
  • Java8 ArrayList之forEach的使用
    目录Java8 ArrayList之forEach使用一、用法二、效率ArrayList在foreach中remove的问题分析iteratoritr.hasNext 和 itr.n...
    99+
    2024-04-02
  • java自带排序使用
    基本类型排序: int a[]={1,2,5,3,6,4}; Arrays.sort(a);//对a升序排序 Arrays.sort(a,0,6);//对a从下标0,到下标5排序 ...
    99+
    2024-04-02
  • Java8新特性 | List多字段排序(含示例代码)
    可以利用 List 的 sort 方法进行排序,Comparator.comparing 可以指定排序字段,thenComparing 可以继续指定其他的排序字段。 默认使用正序排列,如果想倒序可...
    99+
    2023-09-13
    java 算法 开发语言
  • Java8 Stream collect(Collectors.toMap())的使用
    目录Collectors.toMap的用法三个重载的方法Java8 stream特性 Collectors.toMapCollectors.toMap的用法 在我们实际开发过程中经常...
    99+
    2024-04-02
  • Java8的LocalDateTime怎么使用
    这篇文章主要讲解了“Java8的LocalDateTime怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java8的LocalDateTime怎么使用”吧!前言LocalDateTi...
    99+
    2023-07-05
  • MySQL 排序使 null 排最后
    MySQL 排序使 null 排最后 第一种方法: mysql 会把 null 当作最小值来排序,我们想根据某一字段升序【例如 sort】,又想让值为 null 排在最后面,可以在排序的字段加上负号,...
    99+
    2023-08-31
    mysql
  • Java的sort的排序及使用详解
    目录1.按升序排列:2. 随机排序:3.按降序排列:4.根据参数属性值排序5. 根据参数不同,来确定是升序排列,还是降序排序总结sort() 方法在适当的位置对数组的元素进行排序,并...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作