java中如何对Map的key顺序排序
1.使用List的默认方法 sort 或者 Collections.sort 进行排序这种方法需要对map的key进行转换
Map<String,String> map=new HashMap<>();
map.put("4","maliu");
map.put("1","张三");
map.put("3","李四");
map.put("7","王五");
map.put("9","赵六");
map.put("2","老六");
ArrayList<Map.Entry<String, String>> entries = new ArrayList<>(map.entrySet());
//排序条件
entries.sort(Comparator.comparingInt(entry -> Integer.parseInt(entry.getKey())));
//Collections.sort(entries, Comparator.comparingInt(entry -> Integer.parseInt(entry.getKey())));
HashMap<String, String> news = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : entries) {
news.put(entry.getKey(),entry.getValue());
}
news.forEach((a,b)->{
System.out.printf("k -> %s | v -> %s%n", a,b);
});
2.使用TreeMap的特性进行排序
Map<String,String> map=new HashMap<>();
map.put("4","maliu");
map.put("1","张三");
map.put("3","李四");
map.put("7","王五");
map.put("9","赵六");
map.put("2","老六");
TreeMap<String, String> treeMap = new TreeMap<>(map);
treeMap.forEach((k,v)->{
System.out.printf("k -> %s | v -> %s%n", k,v);
});
2.1.在TreeMap基础上自定义排序方法
Map<String,String> map=new HashMap<>();
map.put("1","maliu");
map.put("22","张三");
map.put("4444","李四");
map.put("666666","王五");
map.put("55555","赵六");
map.put("333","老六");
//TreeMap<String, String> treeMap = new TreeMap<>((o1,o2)-> o2.length()-o1.length());
//TreeMap<String, String> treeMap2 = new TreeMap<>((o1,o2)-> o1.length()-o2.length());
TreeMap<String, String> treeMap = new TreeMap<>(Comparator.comparingInt(String::length));
treeMap.putAll(map);
treeMap.forEach((k,v)->{
System.out.printf("k -> %s | v -> %s%n", k,v);
});java中Map中根据key的大小进行排序实例
需要对键值对(key-value)的key进行排序的时候,可以利用TreeMap来操作,TreeMap默认情况下就是按照key的大小来进行排序的(升序),所以只需要使用TreeMap来存储key-value对时,就是排好序的。想要按序取数据时,利用Iterator。
- 升序Demo
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
public class Main {
public static void main(String args[]) {
System.out.println("kaishi");
Random random = new Random();
Map<Double, Integer> map = new TreeMap<Double, Integer>();
//随机产生数据,存入到map中;默认情况下时升序的。
for(int i=0;i<100;i++) {
map.put(random.nextDouble()*1000,random.nextInt(1000));
}
//使用Iterator来取key-value对;
Set<Double> keySet = map.keySet();
Iterator<Double> iter = keySet.iterator();
while (iter.hasNext()) {
Double key = iter.next();
System.out.println(key + ":" + map.get(key)); //打印结果,会发现key都是按升序输出的
}
}
}
- 降序Demo
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.Comparator;
public class Main {
public static void main(String args[]) {
System.out.println("kaishi");
Random random = new Random();
Map<Double, Integer> map = new TreeMap<Double, Integer>(
new Comparator<Double>(){
@Override
public int compare(Double o1, Double o2) {
//利用Comparator来实现降序;
return (int) (o2-o1);
}
});
for(int i=0;i<100;i++) {
map.put(random.nextDouble()*1000,random.nextInt(1000));
}
Set<Double> keySet = map.keySet();
Iterator<Double> iter = keySet.iterator();
while (iter.hasNext()) {
Double key = iter.next();
System.out.println(key + ":" + map.get(key));//打印结果,会发现key都是按降序输出的
}
}
}总结
到此这篇关于java中如何对Map的key顺序排序的文章就介绍到这了,更多相关java对Map的key排序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring事务框架之TransactionStatus源码解析
这篇文章主要为大家介绍了Spring事务框架之TransactionStatus源码示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-08-08
MyBatis使用自定义TypeHandler转换类型的实现方法
这篇文章主要介绍了MyBatis使用自定义TypeHandler转换类型的实现方法,本文介绍使用TypeHandler 实现日期类型的转换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-10-10
Java java.lang.InstantiationException异常案例详解
这篇文章主要介绍了Java java.lang.InstantiationException异常案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-08-08
SpringMVC中redirect重定向(带参数)的3种方式
Spring MVC中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,本文主要介绍了SpringMVC中redirect重定向(带参数)的3种方式,感兴趣的可以了解一下2024-07-07


最新评论