ArrayList集合排序:
集合中存放了学生对象,按照学生分数降序排序:
package cn.gf.exercise;public class Stu {private String stuno;private String stuName;private int age;private double score;//省略访问器}
package cn.gf.exercise;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;public class Test {public static void main(String[] args) {// TODO Auto-generated method stub//存放对象Stu stu1 = new Stu();stu1.setAge(20);stu1.setScore(60);stu1.setStuName("一号学生");stu1.setStuno("001");Stu stu2 = new Stu();stu2.setAge(10);stu2.setScore(95.8);stu2.setStuName("二号学生");stu2.setStuno("002");Stu stu3 = new Stu();stu3.setAge(30);stu3.setScore(75);stu3.setStuName("三号学生");stu3.setStuno("003");Stu stu4 = new Stu();stu4.setAge(10);stu4.setScore(95.9);stu4.setStuName("四号学生");stu4.setStuno("004");ArrayList
stuList = new ArrayList
();stuList.add(stu1);stuList.add(stu2);stuList.add(stu3);stuList.add(stu4);System.out.println("姓名 "+"\t"+" 年龄"+"\t"+" 分数");System.out.println("排序前:");for(Stu s: stuList) {System.out.println(s.getStuName()+"\t"+s.getAge()+"\t"+s.getScore());}System.out.println("");//参数1:要排序的集合//参数2:排序规则,排序接口的实现类,在实现类中重写比较方法Collections.sort(stuList,new Comparator
() {@Overridepublic int compare(Stu o1, Stu o2) {// TODO Auto-generated method stub//需要明确比较的内容/*按年龄排序的第一种方法当年龄相等的时候再按照成绩排序缺点:因为compare方法的返回值是int类型的,只适合整型的属性排序,所以不够灵活*///int i = o1.getAge() - o2.getAge();//if(i == 0) {//return (int) (o1.getScore() - o2.getScore());//}//return i;//返回0:相等 返回1:o1>o2 返回-1:o1
//只按年龄排序第2中方法//if(o1.getAge()>o2.getAge()){//return 1;//}else if(o1.getAge() 1;//小配大,所以是降序排}else {return 0;}}});System.out.println("**********************");System.out.println("排序后");for(Stu s: stuList){System.out.println(s.getStuName()+"\t"+s.getAge()+"\t"+s.getScore());}}}
//return -1;//}else{//return 0;//}//按成绩/*升序排序:大于配大值,小于配小值 * 降序排序:大于配小值,小于配大值 * (口诀:正序大配大,小配小;倒序相反) */if(o1.getScore()>o2.getScore()) {return -1;//大配小}else if(o1.getScore()
return
HashMap排序:
使用keySet()方法获取HashMap集合的key,并存入Set集合中,所存放的Set集合已是升序排好的(原因是存的时候按照hash值大小来存的)(键是升序排好的,但并不代表键对应的值是有序的):
package cn.gf.exercise;import java.util.HashMap;import java.util.Iterator;import java.util.Set;public class Map_Of_Sort {public static void main(String[] args) {HashMap
map = new HashMap
();Stu s1 = new Stu("1001", "张三", 15, 60);Stu s2 = new Stu("1002", "李狗蛋", 10, 80);Stu s3 = new Stu("1007", "王五", 20, 70);Stu s4 = new Stu("1004", "刘二麻子", 20, 90);Stu s5 = new Stu("1008", "赵四", 18, 80);Stu s6 = new Stu("1005", "刘能", 16, 100);Stu s7 = new Stu("1006", "宋小宝", 17, 95);Stu s8 = new Stu("1003", "程野", 9, 88);/* 因为put的时候会根据hash算法计算当前key对应的hash值, 所以插入进去的“键值对”会根据hash值(键)的大小在数组中排序, HashMap是数组+链表结构, 所以当key为字符数字时,存的时候有序,取出来的key值也是有序的。*/map.put("8", s1);map.put("7", s4);map.put("1", s3);map.put("6", s2);map.put("5", s8);map.put("4", s6);map.put("3", s7);map.put("2", s5);Set
keys = map.keySet();Iterator
it = keys.iterator();System.out.println("键"+"\t"+"学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"分数");while(it.hasNext()) {String next = it.next();Stu s = map.get(next);System.out.println();System.out.println(next+"\t"+s.getStuno()+"\t"+s.getStuName()+"\t"+s.getAge()+"\t"+s.getScore());}}}
HashMap集合中存放了学生对象,按照学生的分数降序排序:
思路:
1.通过map集合的entrySet()方法获取每一对 “键----值” 对(此方法返回一个Set集合)
2.使用ArrayList集合的构造方法,把Set集合转化成list集合(因为Collections.sort()方法中只能传入list集合)
3.调用Collections类的sort()方法,在sort()方法中自定义排序的规则
4.排序结束后map集合仍然是无序的,但是list集合是有序的(因为sort方法中传入的参数是list)
package cn.gf.exercise;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.Iterator;import java.util.Map.Entry;import java.util.Set;public class Map_Of_Sort {@SuppressWarnings("unlikely-arg-type")public static void main(String[] args) {HashMap
map = new HashMap
();Stu s1 = new Stu("1001", "张三", 15, 60);Stu s2 = new Stu("1002", "李狗蛋", 10, 80);Stu s3 = new Stu("1007", "王五", 20, 70);Stu s4 = new Stu("1004", "刘二麻子", 20, 90);Stu s5 = new Stu("1008", "赵四", 18, 80);Stu s6 = new Stu("1005", "刘能", 16, 100);Stu s7 = new Stu("1006", "宋小宝", 17, 95);Stu s8 = new Stu("1003", "程野", 9, 88);map.put("8", s1);map.put("7", s4);map.put("1", s3);map.put("6", s2);map.put("5", s8);map.put("4", s6);map.put("3", s7);map.put("2", s5);//通过keySet()方法获取所有的键,并存入Set集合中Set
keys = map.keySet();//获取keys集合的迭代器Iterator
it = keys.iterator();while(it.hasNext()) {/*这样获取到的是null,会报错Stu stu2 = map.get(it); *///通过迭代器的.next()方法来获取keyString next = it.next();//通过集合的.get(key)方法来获取当前键对应的值Stu stu = map.get(next);//输出未排序的集合System.out.println(stu.getStuName()+"\t"+stu.getScore());}//通过map集合的entrySet()方法获取 键---值对 (获取map集合里的所有“映射”)Set
> entrySet = map.entrySet();/*编译错误:The method sort(List
, Comparator) in the type Collections is not applicable for the arguments (Set
>, new Comparator(){})意思是:sort()方法只能传入list集合,不能传set集合*///通过ArrayList集合的构造方法,把set集合转为list集合ArrayList
> list = new ArrayList<>(entrySet);/*调用Collections.sort(ListCollections.sort(list, new Comparator
, Comparator)方法,自定义排序规则。Parameters:list: the list to be sorted.c: the comparator to determine the order of the list. A null value indicates that the elements' naturalordering should be used.*/
>() {@Overridepublic int compare(Entry
o1, Entry
o2) {// TODO Auto-generated method stubif(o1.getValue().getScore()>o2.getValue().getScore()) {return -1;}else if(o1.getValue().getScore()
return 1;}else {return 0;}}});System.out.println("**********************");System.out.println("**********************");System.out.println("**********************");//因为Collection.sort()方法中传入的是list集合,所以应该遍历list结合来判断排序是否成功for(Entry
e: list) {System.out.println(e.getValue().getStuName()+"\t"+e.getValue().getScore());}}}
以上就是动力节点小编介绍的"Java集合排序的方式"的内容,希望对大家有帮助。
动力节点Java培训机构官网:http://www.bjpowernode.com/
热门跟贴