Java中map根据值进行排序
来源:互联网 发布:淘宝买家退货率 编辑:程序博客网 时间:2024/05/16 05:19
此接口取代 Dictionary 类,后者完全是一个抽象类,而不是一个接口。某些映射实现可明确保证其顺序,如 TreeMap 类;另一些映射实现则不保证顺序,如HashMap 类。 虽然可以用TreeMap保证排序,但是在HashMap中自己实现根据值的排序也是一种学习。所以找来了如下代码:
public class TestMap {public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("lisi", 5);
map.put("lisi1", 1);
map.put("lisi2", 3);
map.put("lisi3", 9);
List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
System.out.println("--------------排序前--------------");
for (int i = 0; i < infoIds.size(); i++) {
String id = infoIds.get(i).toString();
System.out.println(id);
}
// 排序
Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return ( o1.getValue()-o2.getValue());
}
});
System.out.println("--------------排序后--------------");
for (int i = 0; i < infoIds.size(); i++) {
Entry<String,Integer> ent=infoIds.get(i);
System.out.println(ent.getKey()+"="+ent.getValue());
}
}
}
之后又发现api文档中存在一个map.entry<K,V>接口,Map.entrySet 方法返回映射的 collection 视图,其中的元素属于此类。获得映射项引用的唯一 方法是通过此 collection 视图的迭代器来实现。这些Map.Entry 对象仅 在迭代期间有效;更确切地讲,如果在迭代器返回项之后修改了底层映射,则某些映射项的行为是不确定的,除了通过setValue 在映射项上执行操作之外。 为此查找了一些有用的资料,http://q.cnblogs.com/q/66942/Map是个key:value对的集合,是散列表类。里面包含key value两个部分,如果要访问它的内部,这时候就需要一个更直接的部分,来方便访问,就有了-->Map.Entry。就像一个装着两样东西的袋子,给它一根拉开的拉链?map能通过key能找到对应的value;map.entry是map内的key-value对。
学习到的技术点:
1.map的排序问题,代码如上
2.在字符串里找出出现次数最多的字母和出现的次数:
public class MostString {
public static void main(String[] args) {
String s ="adadfdfseffserfefsefsseetsdg";
Map map = new HashMap();
for(int i=0; i< s.length();i++){
String tmpStr = s.charAt(i)+"";
Object tmpObj = map.get(tmpStr);
if(tmpObj == null){
map.put(tmpStr, new Integer(1));
}else {
map.put(tmpStr, new Integer(((Integer) tmpObj).intValue()+1));
}
}
for(Iterator itor =map.keySet().iterator();itor.hasNext();){
String word = itor.next().toString();
Integer integer =(Integer) map.get(word);
System.out.println("字母["+word+"]出现次数:"+integer.intValue());
}
List<Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
System.out.println("--------------排序后--------------");
infoIds = sort(map);
for (int i = 0; i < infoIds.size(); i++) {
Entry<String,Integer> ent=infoIds.get(i);
String word = ent.getKey();
Integer integer = ent.getValue();
System.out.println("字母["+word+"]出现次数:"+integer.intValue());
}
}
public static List<Entry<String, Integer>> sort(Map<String, Integer> map) {
List<Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
// 排序
Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return ( o1.getValue()-o2.getValue());
}
});
return infoIds;
}
}
- Java中map根据值进行排序
- (java)根据map中value进行排序
- JAVA中对list map根据map某个key值进行排序
- JAVA中对list map根据map某个key值进行排序
- Java中Map根据键(Key)或者值(Value)进行排序
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java 中 Map 根据键值 (key) 或者值 (value) 进行排序实现
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java中Map根据键值(key)或者值(value)进行排序实现
- Map中根据value值的大小进行排序
- java中list里面存放map,根据map中的某一个字段进行排序
- java中list里面存放map,根据map中的某一个字段进行排序
- 根据value值对map进行排序
- map根据其value值进行排序
- 根据Map的value值进行排序
- Map根据value进行排序
- Map根据value进行排序
- Map根据value进行排序
- 矩阵快速幂应用于概率DP POJ 3744
- Objective-C基本数据类型操作
- Android LayoutInflater原理分析
- Sql server 2012 安装
- 2015/3/12
- Java中map根据值进行排序
- wpf DataGrid 不显示垂直滚动条
- Bag of Features (BOF)图像检索算法
- 获取屏幕的宽度和高度的方法
- 引用android-support-v7-appcompat库文件出错的问题
- 图解Windows下开发Objective-C程序之一 - 搭建Objective-C开发环境
- 告别BIOS EFI时代来临
- ppt转换成pdf的在线转换方法
- C++实现快速排序算法