HashMap排序
HashMap进行有序排序
相信大家都知道hashMap是无序的 即使可以把key-value拿出来 当再次放进去的时候还是乱序的 那么如何进行排序呢?这就涉及到它的一个子类了LinkedHashMap。
LinkedHashMap
LInkedHashMap是一个有序的链表结构是HashMap的一个子类:
public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
{
........
}
- 创建一个LInkedHashMap对象
LinkedHashMap<Integer, User> newHashMap = new LinkedHashMap<>();
Collections
凡是涉及到集合排序的问题 应该首先想到的是系统自带的集合排序工具类 Collectioins 别自己写快速排序 冒泡排序 选择排序
Collection中需要两个参数 LIst集合 和 Comparator比较器
public class Collections {
// Suppresses default constructor, ensuring non-instantiability.
private Collections() {
}
.......
public static <T> void sort(List<T> list, Comparator<? super T> c) {
// BEGIN Android-changed: Compat behavior for apps targeting APIs <= 25.
// list.sort(c);
int targetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion();
if (targetSdkVersion > 25) {
list.sort(c);
} else {
// Compatibility behavior for API <= 25. http://b/33482884
if (list.getClass() == ArrayList.class) {
Arrays.sort((T[]) ((ArrayList) list).elementData, 0, list.size(), c);
return;
}
Object[] a = list.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<T> i = list.listIterator();
for (int j = 0; j < a.length; j++) {
i.next();
i.set((T) a[j]);
}
}
// END Android-changed: Compat behavior for apps targeting APIs <= 25.
}
.....
}
List
Map结构转换成List结构
在不改变key-value结构前提下 map并不能直接转换成List结构 所以需要把map先转换成set集合
entrySet
如果你对api特别熟悉的话肯定知道这个entrySet方法
entrySet
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
Map结构转换成set结构* 把hashMap的键值对全部绑定
Set<Map.Entry<Integer, User>> entrieSet = hashMap.entrySet();
然后再把set集合转成list集合
ArrayList<Map.Entry<Integer, User>> list = new ArrayList<>(entrieSet);
将list做为参数传到Collections中,匿名内部类创建比较器
Collections.sort(list, new Comparator<Map.Entry<Integer, User>>() {
@Override
public int compare(Map.Entry<Integer, User> o1, Map.Entry<Integer, User> o2) {
//默认排序o1 - o2;
//反序
return o2.getValue().getAge() - o1.getValue().getAge();
}
});
排好序的list 不能直接转换成LinkedHashMap
因为set->list转换不可逆
所以循环遍历取出来key-value再放进linkedHashMMap中
for (int i = 0; i < list.size(); i++) {
Map.Entry<Integer, User> entry = list.get(i);
newHashMap.put(entry.getKey(), entry.getValue());
}
测试代码如下
public class HashMapSort {
public static void main(String args[]) {
HashMap<Integer, User> hashMap = new HashMap<>();
User user = new User();
user.setName("张三");
user.setAge(21);
hashMap.put(1, user);
User user2 = new User();
user2.setName("李四");
user2.setAge(24);
hashMap.put(2, user2);
User user3 = new User();
user3.setName("王五");
user3.setAge(23);
hashMap.put(3, user3);
System.out.println("排序前" + hashMap);
HashMap<Integer, User> sortHashMap = sortHashMap(hashMap);
System.out.println("排序后" + sortHashMap);
}
//根据hashmap中user的age属性的倒序排序 并保持 key_value 结构
private static HashMap<Integer, User> sortHashMap(HashMap<Integer, User> hashMap) {
//通过hashmap的有序子类 来进行排序 linkedHashMap 是链表的有序结构
//创建一个有序的hashmap的数据结构
LinkedHashMap<Integer, User> newHashMap = new LinkedHashMap<>();
//凡是要对集合的排序首先想到的是集合的工具类进行排序
//将map的数据结构转换成list结构
// 把hashmap的键值对拿出来 转到集合里面
Set<Map.Entry<Integer, User>> entries = hashMap.entrySet();
//把set集合转成list集合
ArrayList<Map.Entry<Integer, User>> list = new ArrayList<>(entries);
//参2 比较器
Collections.sort(list, new Comparator<Map.Entry<Integer, User>>() {
@Override
public int compare(Map.Entry<Integer, User> o1, Map.Entry<Integer, User> o2) {
//默认排序o1 -o2;
//反序
return o2.getValue().getAge() - o1.getValue().getAge();
}
});
//将排好序的list转换成linkHashMap set->list 转换不可逆
//遍历取出来 放到linkHashMap中
for (int i = 0; i < list.size(); i++) {
Map.Entry<Integer, User> entry = list.get(i);
newHashMap.put(entry.getKey(), entry.getValue());
}
return newHashMap;
}
}
输出结果:
排序前{1=User{name=‘张三’, age=21}, 2=User{name=‘李四’, age=24}, 3=User{name=‘王五’, age=23}}
排序后{2=User{name=‘李四’, age=24}, 3=User{name=‘王五’, age=23}, 1=User{name=‘张三’, age=21}}
转载请注明出处 https://blog.youkuaiyun.com/LyueP/article/details/88644719