java中如何对Map的key顺序排序
作者:一只alone
大家都知道Map排序的方式有很多种,下面这篇文章主要给大家介绍了关于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排序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
