Map集合排序
来源:互联网 发布:unity3d 人物移动跳跃 编辑:程序博客网 时间:2024/06/10 16:09
package cn.com.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class MapUtils {
public static List<Map.Entry<String, Integer>> sortMapValue(
Map<String, Integer> data) {
// 将Map转化为List集合,List采用ArrayList
List<Map.Entry<String, Integer>> list_Data = new ArrayList<Map.Entry<String, Integer>>(
data.entrySet());
// 通过Collections.sort(List I,Comparator c)方法进行排序
Collections.sort(list_Data,
new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
return list_Data;
}
public static List<Map.Entry<String, Integer>> sortMapKey(
Map<String, Integer> data) {
// 将Map转化为List集合,List采用ArrayList
List<Map.Entry<String, Integer>> list_Data = new ArrayList<Map.Entry<String, Integer>>(
data.entrySet());
// 通过Collections.sort(List I,Comparator c)方法进行排序
Collections.sort(list_Data,
new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return (o1.getKey().compareTo(o2.getKey()));
}
});
return list_Data;
}
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("0003", 10);
map.put("0002", 1);
map.put("0004", 6);
map.put("0001", 18);
System.out.println("原数据:" + map);
//{0004=6, 0002=1, 0003=10, 0001=18}
System.out.println("按照key排序后: " + sortMapKey(map));
//[0001=18, 0002=1, 0003=10, 0004=6]
System.out.println("按照value排序后: " + sortMapValue(map));
//[0001=18, 0003=10, 0004=6, 0002=1]
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class MapUtils {
public static List<Map.Entry<String, Integer>> sortMapValue(
Map<String, Integer> data) {
// 将Map转化为List集合,List采用ArrayList
List<Map.Entry<String, Integer>> list_Data = new ArrayList<Map.Entry<String, Integer>>(
data.entrySet());
// 通过Collections.sort(List I,Comparator c)方法进行排序
Collections.sort(list_Data,
new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
return list_Data;
}
public static List<Map.Entry<String, Integer>> sortMapKey(
Map<String, Integer> data) {
// 将Map转化为List集合,List采用ArrayList
List<Map.Entry<String, Integer>> list_Data = new ArrayList<Map.Entry<String, Integer>>(
data.entrySet());
// 通过Collections.sort(List I,Comparator c)方法进行排序
Collections.sort(list_Data,
new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return (o1.getKey().compareTo(o2.getKey()));
}
});
return list_Data;
}
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("0003", 10);
map.put("0002", 1);
map.put("0004", 6);
map.put("0001", 18);
System.out.println("原数据:" + map);
//{0004=6, 0002=1, 0003=10, 0001=18}
System.out.println("按照key排序后: " + sortMapKey(map));
//[0001=18, 0002=1, 0003=10, 0004=6]
System.out.println("按照value排序后: " + sortMapValue(map));
//[0001=18, 0003=10, 0004=6, 0002=1]
}
}
阅读全文
0 0
- map集合的排序
- MAP集合排序
- Map集合排序
- Map集合排序
- map集合排序
- Map集合排序
- 对map集合进行排序
- 对map集合进行排序
- 对map集合进行排序
- 对map集合进行排序
- 对map集合进行排序
- map集合针对key排序
- 给map集合字母排序
- Map集合根据key排序
- 对map集合进行排序
- 对map集合进行排序
- 对map集合进行排序
- List集合、Map集合、数组的排序
- ubuntu14.04命令大全
- offer
- linux下打乱文本行序、文本分割与文本合并
- Google benchmark:一个简单易用的C++ benchmark库
- js—列表的使用
- Map集合排序
- 教你如何迅速秒杀掉:99%的海量数据处理面试题 本文经过大量细致的优化后,收录于我的新书《编程之法》第六章中,新书目前已上架京东/当当/亚马逊 作者:July 出处:结构之法算法之道b
- java.lang.NoClassDefFoundError
- List,Set,Map遍历时删除元素
- springboot jar包 无法访问jsp
- TensorFlow下网络模型的存储与加载
- CSDN去广告小脚本
- 【第三周项目1】 顺序表的基本运算
- Codeforces846D(二分+树状数组)