java中对List进行分组和排序
排序
对List进行排序,有两种办法
第一个是用java提供的工具类Collections提供的sort方法进行排序
废话不多说,上代码
首先定义一个Student
public class Student {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
}
下面是进行排序的代码
public static void main(String[] args) {
List<Student> list= new ArrayList<Student>();
list.add(new Student(5, "aa"));
list.add(new Student(7, "bb"));
list.add(new Student(6, "cc"));
Collections.sort(list,new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
//会把集合里面的对象两两传进方法里面比较,这里比较age,降序就O2-O1,升序就O1-O2
return o2.getAge()-o1.getAge();
}
});
//打印list每一项的age
list.forEach(a -> System.out.println(a.getAge()));
}
}
第二种方法:
List集合提供了sort方法,依然用Student做集合,进行排序
public static void main(String[] args) {
List<Student> list= new ArrayList<Student>();
list.add(new Student(5, "aa"));
list.add(new Student(7, "bb"));
list.add(new Student(6, "cc"));
//差别在这里,这里直接用list的sort方法,不需要吧list作为参数,其他的和Comparable排序是一样的
list.sort(new Comparator<Student>(){
@Override
public int compare(Studento1, Studento2) {
//会把集合里面的对象两两传进方法里面比较,这里比较age,降序就O2-O1,升序就O1-O2
return o2.getAge()-o1.getAge();
}
});
//打印list每一项的age
list.forEach(a -> System.out.println(a.getAge()));
}
}
对list进行分组:
同样的,用Student的集合作为示例,代码如下
public class test2 {
/**
* 创建比较器
*/
public static <T> List<List<T>> dividerList(List<T> list,Comparator<? super T> comparator) {
List<List<T>> lists = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
boolean isContain = false;
for (int j = 0; j < lists.size(); j++) {
if (lists.get(j).size() == 0||comparator.compare(lists.get(j).get(0),list.get(i)) == 0) {
lists.get(j).add(list.get(i));
isContain = true;
break;
}
}
if (!isContain) {
List<T> newList = new ArrayList<>();
newList.add(list.get(i));
lists.add(newList);
}
}
return lists;
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
//实在不会起名字,用字母代替吧
list.add(new Student(17,"aa"));
list.add(new Student(15,"bb"));
list.add(new Student(16,"cc"));
list.add(new Student(15,"dd"));
list.add(new Student(16,"ee"));
list.add(new Student(17,"ff"));
List<List<Student>> list2 = dividerList(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 按年龄分组,这里注意一点,返回的值为0,就会认为这两个Studeng是一组的,返回其他值,则认为不是,所以下面的-1可以替换为任意非0数字
return o1.getAge == o2.getAge ? 0:-1;
//也可以按照姓名分组,返回结果如下,因为是比较两个值是否相等,所以先后是没有区别的
//return o1.getName().compareTo(o1.getName())
}
});
for(List<Student> stList: list2){
stList.forEach(a -> System.out.printIn(a.getName+":"+a.getAge));
System.out.printIn("=========================================");
}
}
}
java中对List进行分组和排序的更多相关文章
- Java中的经典算法之选择排序(SelectionSort)
Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...
- JAVA中运用数组的四种排序方法
JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法. 快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现. 冒泡法是运用遍历数组进 ...
- AJPFX关于Java中运用数组的四种排序方法
JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法.快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现.冒泡法是运用遍历数组进行比 ...
- Java基础知识强化12:Java中运用数组的四种排序方法
1.使用JavaApi文档中的Arrays类中的sort()进行快速排序 首先我们直接看代码如下: package himi.text; import java.util.Arrays; public ...
- java中Collections.sort()方法实现集合排序
1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>(); //定 ...
- 十七、Java中数组常见的几种排序方法!
转载自:https://www.cnblogs.com/bekeyuan123/p/6891875.html 数组的定义: // 3种定义方式 int[] arr = new int[5]; int[ ...
- JAVA中,数组的操作与排序
自己写了正向反向的冒泡排序,还用了静态和NEW方法实现. import java.util.Arrays; public class HelloJava { public static void ma ...
- java中实现Comparable接口实现自定义排序
class Student implements Comparable{ String name; int gpa; @Override public int compareTo(Object arg ...
- Java中Lambda表达式的使用
简介(译者注:虽然看着很先进,其实Lambda表达式的本质只是一个"语法糖",由编译器推断并帮你转换包装为常规的代码,因此你可以使用更少的代码来实现同样的功能.本人建议不要乱用,因 ...
随机推荐
- 可视化工具与pymongo
可视化工具 链接:https://robomongo.org/ pymongo 官网:http://api.mongodb.com/python/current/tutorial.html from ...
- Database: coursera assignment 1
q.1: Find the titles of all movies directed by Steven Spielberg. select title from moviewhere direct ...
- MVC常见错误记录
1 找到了多个与名为“Home”的控制器匹配的类型.如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间来搜索匹配此请求的 根项目下的RouteCon ...
- Spark- Spark Yarn模式下跑yarn-client无法初始化SparkConext,Over usage of virtual memory
在spark yarn模式下跑yarn-client时出现无法初始化SparkContext错误. // :: INFO mapreduce.Job: Task Id : attempt_142829 ...
- css中字体大小在不同浏览器兼容性问题
css中使用font-size设定字体大小,不同浏览器的字体height一样,但是width不同,比如在火狐和谷歌中,font-size:20px,字体的高度变为20px,但是谷歌的字体宽度比火狐长 ...
- poj-2336 Ferry Loading II(dp)
题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ...
- 百度之星2017初赛A
雪崩,没晋级,补题 1001 分析:求n-1的约数个数 #include "iostream" #include "cstdio" #include " ...
- codevs2821 天使之城
传送门 2821 天使之城 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 天使城有一个火车站,每辆火车都从A方向驶入车站 ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- java定时器,留着用
说明:该定时器作用是 设定定时器首次执行的时间firstTime和执行间隔period,如firstTime=2015-3-25 9:00:00,period=24小时,若程序启动时,已经超过firs ...