搜索
首页Javajava教程Java中如何用lambda表达式来排序

1.lambda表达式排序

我们首先看几个比较常见的排序例子,基本数据类型的排序

    List list = Arrays.asList(1,3,2,5,4);
    list.sort(Comparator.naturalOrder());
    System.out.println(list);
    list.sort(Comparator.reverseOrder());
    System.out.println(list);
  
   输出结果:

    [1, 2, 3, 4, 5]
    [5, 4, 3, 2, 1]

我们可以看到执行结果是符合预期的,但是多数场景我们可能需要针对对象的某个属性进行排序,那么应该怎样做呢?我们看下边的例子:

public class Student {
    private String name;
    private String sexual;
    private Integer age;

    public Student(String name, String sexual,Integer age) {
        this.name = name;
        this.sexual = sexual;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSexual() {
        return sexual;
    }

    public void setSexual(String sexual) {
        this.sexual = sexual;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sexual='" + sexual + '\'' +
                ", age=" + age +
                '}';
    }

public class Starter {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(
                new Student("jack", 12),
                new Student("john", 13),
                new Student("lily", 11),
                new Student("lucy", 10)
        );
        list.sort(Comparator.comparing(Student::getAge));
        System.out.println(list);
        list.sort(Comparator.comparing(Student::getAge).reversed());
        System.out.println(list);
    }
}

输出结果:

[Student{name='lucy', age=10}, Student{name='lily', age=11}, Student{name='jack', age=12}, Student{name='john', age=13}]
[Student{name='john', age=13}, Student{name='jack', age=12}, Student{name='lily', age=11}, Student{name='lucy', age=10}]

 如果我们需要按照性别分组再排序又该如何实现呢?我们接着看下边的例子

public class Starter {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(
                new Student("jack", "male", 12),
                new Student("john", "male", 13),
                new Student("lily", "female", 11),
                new Student("david", "male", 14),
                new Student("luck", "female", 13),
                new Student("jones", "female", 15),
                new Student("han", "male", 13),
                new Student("alice", "female", 11),
                new Student("li", "male", 12)
        );
 Map<String, List<Student>> groupMap = list.stream().sorted(Comparator.comparing(Student::getAge))
.collect(Collectors.groupingBy(Student::getSexual, Collectors.toList()));
        System.out.println(groupMap.toString());
    }
}

输出结果:

{
	female = [
      Student {
		name = 'lily', sexual = 'female', age = 11
	  }, 
      Student {
		name = 'alice', sexual = 'female', age = 11
	}, Student {
		name = 'luck', sexual = 'female', age = 13
	}, Student {
		name = 'jones', sexual = 'female', age = 15
	}],
   male = [
    Student {
		name = 'jack', sexual = 'male', age = 12
	}, Student {
		name = 'li', sexual = 'male', age = 12
	}, Student {
		name = 'john', sexual = 'male', age = 13
	}, Student {
		name = 'han', sexual = 'male', age = 13
	}, Student {
		name = 'david', sexual = 'male', age = 14
	}]
}

我们看到上边的输出结果存在一个问题,如果年龄相同则没有按照姓名排序,怎样实现这个功能呢?我们接着看下边的例子

Map<String, List<Student>> groupMap = list.stream().sorted(Comparator.comparing(Student::getAge)
.thenComparing(Student::getName)).collect(Collectors.groupingBy(Student::getSexual, Collectors.toList()));

输出结果:

{
	female = [
     Student {
		name = 'alice', sexual = 'female', age = 11
	}, Student {
		name = 'lily', sexual = 'female', age = 11
	}, Student {
		name = 'luck', sexual = 'female', age = 13
	}, Student {
		name = 'jones', sexual = 'female', age = 15
	}],
   male = [
     Student {
		name = 'jack', sexual = 'male', age = 12
	}, Student {
		name = 'li', sexual = 'male', age = 12
	}, Student {
		name = 'han', sexual = 'male', age = 13
	}, Student {
		name = 'john', sexual = 'male', age = 13
	}, Student {
		name = 'david', sexual = 'male', age = 14
	}]
}

以上是Java中如何用lambda表达式来排序的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)