Java 排序整合篇
前言
Java代码的编写过程中,不可避免会使用到排序功能,本篇博文将常见的数据结构排序罗列出来,供大家参考使用。
正文
List<String>
、List<Integer>
、List<Double >
排序
升序:
List<String> stringList = Arrays.asList("d","e","a","b");
Collections.sort(stringList); //对stringList升序
降序:
List<String> stringList = Arrays.asList("d","e","a","b");
Collections.sort(stringList); //对stringList降序
Collections.reverse(stringList); //翻转
Map
根据 key
排序
使用 TreeMap
:
TreeMap
会根据 key
对 put
进去的进行升序排序。
升序:
Map<Integer,String> treeMap = new TreeMap<>();
treeMap.put(1,"111");
treeMap.put(8,"888");
treeMap.put(4,"444");
treeMap.put(2,"222");
System.out.println(treeMap); //输出{1=111, 2=222, 4=444, 8=888}
降序:
Map<Integer,String> treeMap = new TreeMap<>((o1,o2)->{
return o2.compareTo(o1);
});
treeMap.put(1,"111");
treeMap.put(8,"888");
treeMap.put(4,"444");
treeMap.put(2,"222");
System.out.println(treeMap);输出{8=888, 4=444, 2=222, 1=111}
注:不推荐用
Double
作为Map
的key
,由于Map
的底层hashcode
的实现,会导致取出数据时有时有问题。如果可以确定小数点后的位数,比如小数点后只有两位小数,可以乘以100
化为Integer
作为key
,要取出来时除以10
再取0
Map
根据 value
排序
对 value
降序:
Map<String, Double> budget = new HashMap<>();
budget.put("普通账户", 1000.00);
budget.put("信用账户", 2000.00);
budget.put("期权账户",500.00);
budget.put("理财账户", 4000.00);
//jdk8通过stream实现对Map降序排序
Map<String, Double> sorted = budget
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
//sorted为{理财账户=4000.0, 信用账户=2000.0, 普通账户=1000.0, 期权账户=500.0}
对 key
降序并只要前 2
个:
Map<String, Double> budget = new HashMap<>();
budget.put("普通账户", 1000.00);
budget.put("信用账户", 2000.00);
budget.put("期权账户",500.00);
budget.put("理财账户", 4000.00);
//jdk8通过stream实现对Map降序排序
Map<String, Double> sorted = budget
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(2)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
//sorted为{理财账户=4000.0, 信用账户=2000.0}
对 value
升序:
Map<String, Double> budget = new HashMap<>();
budget.put("普通账户", 1000.00);
budget.put("信用账户", 2000.00);
budget.put("期权账户",500.00);
budget.put("理财账户", 4000.00);
//jdk8通过stream实现对Map升序排序
Map<String, Double> sorted = budget
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.naturalOrder()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
//sorted为{期权账户=500.0, 普通账户=1000.0, 信用账户=2000.0, 理财账户=4000.0}
List<Map>
根据 map
里某一 value
排序
匿名方式:
List<Map<String, Object>> resultlist = new ArrayList<>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", "1");
map1.put("name", "张三");
map1.put("Score", 86.5);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", "2");
map2.put("name", "李四");
map2.put("Score", 90.0);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", "3");
map3.put("name", "王五");
map3.put("Score", 70.5);
resultlist.add(map1);
resultlist.add(map2);
resultlist.add(map3);
Collections.sort(resultlist, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
Double one = (Double) o1.get("Score");
Double two = (Double) o2.get("Score");
return one.compareTo(two); //one.compareTo(two)为升序,two.compareTo(one)为降序
}
});
System.out.println(resultlist);
//输出[{Score=70.5, name=王五, id=3}, {Score=86.5, name=张三, id=1}, {Score=90.0, name=李四, id=2}]
lambda
方式:
List<Map<String, Object>> resultlist = new ArrayList<>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", "1");
map1.put("name", "张三");
map1.put("Score", 86.5);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", "2");
map2.put("name", "李四");
map2.put("Score", 90.0);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", "3");
map3.put("name", "王五");
map3.put("Score", 70.5);
resultlist.add(map1);
resultlist.add(map2);
resultlist.add(map3);
Collections.sort(resultlist, (o1, o2) -> {
Double one = Double.valueOf(o1.get("Score").toString()) ;
Double two = Double.valueOf(o2.get("Score").toString()) ;
return one.compareTo(two); //one.compareTo(two)为升序,two.compareTo(one)为降序
});
System.out.println(resultlist);
//输出[{Score=70.5, name=王五, id=3}, {Score=86.5, name=张三, id=1}, {Score=90.0, name=李四, id=2}]
List<Object>
根据 Object
的某一属性对 List
进行排序
Student
类:
public class Student {
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
对 List<Student>
排序:
List<Student> list = new ArrayList<>();
Student student1=new Student();
Student student2=new Student();
Student student3=new Student();
student1.setName("张三");
student1.setScore(92);
student2.setName("李四");
student2.setScore(86);
student3.setName("王五");
student3.setScore(100);
list.add(student1);
list.add(student2);
list.add(student3);
System.out.println("排序前:");
for(int i = 0;i < list.size();i++){
System.out.println(list.get(i).getName()+":"+list.get(i).getScore());
}
System.out.println("--------");
System.out.println("排序后:");
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o1.getScore() - o2.getScore();
return i;
}
});
for(int i = 0;i < list.size();i++){
System.out.println(list.get(i).getName()+":"+list.get(i).getScore());
}
结果:
排序前:
张三:92
李四:86
王五:100
--------
排序后:
李四:86
张三:92
王五:100
复杂 List<Map<String, List<Map<String, Object>>>>
排序实例
要求:不仅内部要根据 Score
排序,整个 List
也要根据 Score
排序
List<Map<String, List<Map<String, Object>>>> tagsList = new ArrayList<>();
List<Map<String, Object>> resultlist1 = new ArrayList<>();
Map<String, List<Map<String, Object>>> listMap1 = new HashMap<>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", "1");
map1.put("name", "张一");
map1.put("Score", 7L);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", "2");
map2.put("name", "李一");
map2.put("Score", 100L);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", "3");
map3.put("name", "王一");
map3.put("Score", 86L);
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("id", "4");
map4.put("name", "赵一");
map4.put("Score", 0L);
resultlist1.add(map1);
resultlist1.add(map2);
resultlist1.add(map3);
resultlist1.add(map4);
listMap1.put("1", resultlist1);
tagsList.add(listMap1);
List<Map<String, Object>> resultlist2 = new ArrayList<>();
Map<String, List<Map<String, Object>>> listMap2 = new HashMap<>();
Map<String, Object> map2_1 = new HashMap<String, Object>();
map2_1.put("id", "1");
map2_1.put("name", "张二");
map2_1.put("Score", 709L);
Map<String, Object> map2_2 = new HashMap<String, Object>();
map2_2.put("id", "2");
map2_2.put("name", "李二");
map2_2.put("Score", 0L);
Map<String, Object> map2_3 = new HashMap<String, Object>();
map2_3.put("id", "3");
map2_3.put("name", "王二");
map2_3.put("Score", 456L);
Map<String, Object> map3_4 = new HashMap<String, Object>();
map3_4.put("id", "4");
map3_4.put("name", "赵二");
map3_4.put("Score", 0L);
resultlist2.add(map2_1);
resultlist2.add(map2_1);
resultlist2.add(map2_3);
resultlist2.add(map3_4);
listMap2.put("2", resultlist2);
tagsList.add(listMap2);
List<Map<String, Object>> resultlist3 = new ArrayList<>();
Map<String, List<Map<String, Object>>> listMap3 = new HashMap<>();
Map<String, Object> map3_1 = new HashMap<String, Object>();
map3_1.put("id", "1");
map3_1.put("name", "张三");
map3_1.put("Score", 9L);
Map<String, Object> map3_2 = new HashMap<String, Object>();
map3_2.put("id", "2");
map3_2.put("name", "李三");
map3_2.put("Score", 0L);
Map<String, Object> map3_3 = new HashMap<String, Object>();
map3_3.put("id", "3");
map3_3.put("name", "王三");
map3_3.put("Score", 86L);
Map<String, Object> map2_4 = new HashMap<String, Object>();
map2_4.put("id", "4");
map2_4.put("name", "赵三");
map2_4.put("Score", 920L);
resultlist3.add(map3_1);
resultlist3.add(map3_2);
resultlist3.add(map3_3);
resultlist3.add(map2_4);
listMap3.put("3", resultlist3);
tagsList.add(listMap3);
System.out.println("未排序时的tagsList:" + tagsList);
//先对每一个里的Score降序排序,排序后tagsList里的每一个元素中的List<Map<String, Object>>都是有序的
for (Map<String, List<Map<String, Object>>> ins : tagsList) {
for (List<Map<String, Object>> v : ins.values()) {
Collections.sort(v, (o1, o2) -> {
Long one = Long.valueOf(o1.get("Score").toString());
Long two = Long.valueOf(o2.get("Score").toString());
return two.compareTo(one);
});
}
}
System.out.println("对每一个Score内部降序排序后的tagsList:" + tagsList);
Collections.sort(tagsList, (o1, o2) -> {
Long one = 0L;
Long two = 0L;
for (List<Map<String, Object>> v : o1.values()) {
one = Long.valueOf(v.get(0).get("Score").toString());
break;
}
for (List<Map<String, Object>> v : o2.values()) {
two = Long.valueOf(v.get(0).get("Score").toString());
break;
}
return two.compareTo(one);
});
System.out.println("根据最内层的最大值排序后的tagsList:" + tagsList);
输出结果:
未排序时的tagsList:
[{1=[{Score=7, name=张一, id=1}, {Score=100, name=李一, id=2}, {Score=86, name=王一, id=3}, {Score=0, name=赵一, id=4}]},
{2=[{Score=709, name=张二, id=1}, {Score=709, name=张二, id=1}, {Score=456, name=王二, id=3}, {Score=0, name=赵二, id=4}]},
{3=[{Score=9, name=张三, id=1}, {Score=0, name=李三, id=2}, {Score=86, name=王三, id=3}, {Score=920, name=赵三, id=4}]}]
对每一个Score内部降序排序后的tagsList:
[{1=[{Score=100, name=李一, id=2}, {Score=86, name=王一, id=3}, {Score=7, name=张一, id=1}, {Score=0, name=赵一, id=4}]},
{2=[{Score=709, name=张二, id=1}, {Score=709, name=张二, id=1}, {Score=456, name=王二, id=3}, {Score=0, name=赵二, id=4}]},
{3=[{Score=920, name=赵三, id=4}, {Score=86, name=王三, id=3}, {Score=9, name=张三, id=1}, {Score=0, name=李三, id=2}]}]
根据最内层的最大值排序后的tagsList:
[{3=[{Score=920, name=赵三, id=4}, {Score=86, name=王三, id=3}, {Score=9, name=张三, id=1}, {Score=0, name=李三, id=2}]},
{2=[{Score=709, name=张二, id=1}, {Score=709, name=张二, id=1}, {Score=456, name=王二, id=3}, {Score=0, name=赵二, id=4}]},
{1=[{Score=100, name=李一, id=2}, {Score=86, name=王一, id=3}, {Score=7, name=张一, id=1}, {Score=0, name=赵一, id=4}]}]
后记
文章摘录地址,点我跳转原文