面试回顾——List<T>排序
1、如何对List<T>排序:
public static void main(String[] args) {
Student stu1=new Student("张三","男",25);
Student stu2=new Student("李四","男",22);
Student stu3=new Student("王五","男",26);
Student stu4=new Student("赵六","男",25);
Student stu5=new Student("麻七","男",28);
Student stu6=new Student("二狗","男",21);
List<Student> list=new ArrayList<Student>();
list.add(stu1);list.add(stu2);
list.add(stu3);list.add(stu4);
list.add(stu5);list.add(stu6);
System.out.println("排序前:");
for (Student student : list) {
System.out.println(student.toString());
}
Collections.sort(list, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
if(o1.getStuAge()>o2.getStuAge()){
return 1;
}
if(o1.getStuAge()==o2.getStuAge()){
return 0;
}
return -1;
}
});
System.out.println("排序后:");
for (Student student : list) {
System.out.println(student.toString());
}
}
Java8新流式排序:
package com.concretepage;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortList {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "Mahesh", 12));
list.add(new Student(2, "Suresh", 15));
list.add(new Student(3, "Nilesh", 10));
//以自然序排序一个list
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()));
//自然序逆序元素,使用Comparator提供的reverseOrder()方法
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()));
//使用Comparator来排序一个list
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()));
}
}
面试回顾——List<T>排序的更多相关文章
- 前端面试回顾---javascript的面向对象
转:https://segmentfault.com/a/1190000011061136 前言 前一阵面试,过程中发现问到一些很基础的问题时候,自己并不能很流畅的回答出来.或者遇到一些基础知识的应用 ...
- 【PHP面试题】通俗易懂的两个面试必问的排序算法讲解:冒泡排序和快速排序
又到了金三银四找工作的时间,相信很多开发者都在找工作或者准备着找工作了.一般应对面试,我们无可厚非的去刷下面试题.对于PHPer来说,除了要熟悉自己所做的项目,还有懂的基本的算法.下面来分享下PHP面 ...
- 面试常考各类排序算法总结.(c#)
前言 面试以及考试过程中必会出现一道排序算法面试题,为了加深对排序算法的理解,在此我对各种排序算法做个总结归纳. 1.冒泡排序算法(BubbleSort) 1.1 算法描述 (1)比较相邻的元素.如果 ...
- 前端面试回顾(1)---javascript的面向对象
前言 前一阵面试,过程中发现问到一些很基础的问题时候,自己并不能很流畅的回答出来.或者遇到一些基础知识的应用,由于对这些点理解的不是很深入,拿着笔居然什么都写不出来,于是有了回顾一下这些基础知识的想法 ...
- java面试准备之基础排序——冒泡与选择排序
选择排序: [java] public void select(int[] arr){ for(int i=0;i<arr.length;i++){ ...
- 面试常用算法总结——排序算法(java版)
排序算法 重要性不言而喻,很多算法问题往往选择一个好的排序算法往往问题可以迎刃而解 1.冒泡算法 冒泡排序(Bubble Sort)也是一种简单直观的排序算法.它重复地走访过要排序的数列,一次比较两个 ...
- 【JS面试向】选择排序、桶排序、冒泡排序和快速排序简介
新年伊始,又到了金三银四的时候了.面对前端越来越多的算法面试题,我简单的整理了一下几种比较常见的数组排序方式,分别介绍其基本原理和优劣势.(ps:才疏学浅,希望大家可以在issues下面指出问题) 选 ...
- Python面试题目之字典排序
按照字典的内的年龄排序 待排序的字典 d1 = [ {'name':'alice', 'age':38}, {'name':'bob', 'age':18}, {'name':'Carl', 'age ...
- 面试回顾——kafka
关于消息队列的使用场景:https://www.cnblogs.com/linjiqin/p/5720865.html kafka: Topic Kafka将消息种子(Feed)分门别类 每一类的消息 ...
随机推荐
- iOS sqlite大数据分段加载的实现,sqlite数据库的操作
数据库管理类(自己封装的,挺简单的) // // MyDataBaseManger.m // DB_Test // // Created by admin on 17/2/7. // Copy ...
- 使用matlab生成用于ROM初始化的coe文件(转)
reference:https://www.cnblogs.com/chensimin1990/p/9759368.html t=0:2*pi/2^12:2*pi; y=0.5*sin(t)+0.5; ...
- JAVA条件语句:if;switch case
if(布尔表达式) { //如果布尔表达式为true将执行的语句 } 如果布尔表达式为true 执行里面的代码 if...else语句: if(布尔表达式){ //如果布尔表达式的值为true } ...
- CSS学习笔记-05 过渡模块的基本用法
话说 1对情侣两情相悦,你情我愿.时机成熟,夜深人静...咳 ,如果就这么直奔主题,是不是有点猴急,所以,还是要来点前戏@. 铛 铛, 这个时候 过渡模块出现了. 划重点: 上代码: <!DOC ...
- DisplayLink 安装错误
根据 在官网论坛上的反馈结果,程序自己有验证数字签名,数字签名验证不通过,即如上图所示.可能的原因:病毒:下载不完全:证书链出问题
- ::WritePrivateProfileString()的用法,以及GetPrivateProfileString的用法注意事项
WritePrivateProfileString(_T("Section1"),_T("Field1"),Field,savePath); 函数说明,这是在写 ...
- 剑指Offer 47. 求1+2+3+...+n (其他)
题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 题目地址 https://www.nowcod ...
- Python全栈之路----函数----嵌套函数
函数内部可以再次定义函数 要执行函数,必须调用 def func1(): print('alex') def func2(): print('eric') func2() #如果没有这一句,不会pri ...
- EBS打补丁参考
EBS Application打补丁参考:http://blog.csdn.net/cunxiyuan108/article/details/6009784 整体步骤: 0. 停止应用(注意确认FND ...
- PythonStudy——字符串重要方法 String important method
# 1.索引(目标字符串的索引位置) s1 = '123abc呵呵' print(s1.index('b')) # 2.去留白(默认去两端留白,也可以去指定字符) s2 = '***好 * 的 *** ...