简介
Map
是键值对的集合接口,根据键得到值,因此不允许键重复,但允许值重复。它的实现类主要包括:HashMap
、TreeMap
、Hashtable
以及LinkedHashMap
等。
TreeMap
是基于红黑树(Red-Black tree
)的NavigableMap
实现,该映射根据其键的自然顺序进行排序或者根据创建映射时提供的Comparator
进行排序,具体取决于使用的构造方法,默认是按键值的升序排序。
HashMap
的值是没有顺序的,它是按照Key
的HashCode
值存储数据,具有很快的访问速度,遍历时取得数据的顺序是完全随机的。最多只允许一条记录的键为Null
,允许多条记录的值为Null
。不支持线程的同步,即任一时刻可以有多个线程同时写HashMap
,可能会导致数据的不一致。如果需要同步,可以用Collections
的SynchronizedMap
方法使HashMap
具有同步的能力,或者使用ConcurrentHashMap
。
Hashtable
与HashMap
类似,它继承自Dictionary
类。不同的是:它不允许记录的键或者值为空,它支持线程的同步,即任一时刻只有一个线程能写Hashtable
,因此也导致了Hashtable
在写入时会比较慢。
LinkedHashMap
是HashMap
的一个子类,保存了记录的插入顺序,在用Iterator
遍历LinkedHashMap
时先得到的记录肯定是先插入的,也可以在构造时带参数,按照应用次数排序。在遍历的时候会比HashMap
慢,不过有种情况例外,当HashMap
容量很大实际数据较少时,遍历起来可能会比LinkedHashMap
慢,因为LinkedHashMap
的遍历速度只和实际数据有关,与容量无关,而HashMap
的遍历速度和它的容量有关。
一般情况下我们用的最多的是HashMap
,在Map
中插入、删除和定位元素HashMap
是最好的选择。但如果要按自然顺序或自定义顺序遍历键,那么TreeMap
会更好。如果需要输出的顺序和输入相同,那么用LinkedHashMap
可以实现,它还可以按读取顺序来排列。
使用
Key
排序封转
/**
* 使用 Map 按 Key 进行排序
*
* @param oriMap 排序 Map 集合
* @param isRise 是否按照升序排序
*/
public static Map<String, String> sortMapByKey(Map<String, String> oriMap, final boolean isRise) {
if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, String> sortMap = new TreeMap<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (isRise) {
// 升序排序
return o1.compareTo(o2);
} else {
// 降序排序
return o2.compareTo(o1);
}
}
});
sortMap.putAll(oriMap);
return sortMap;
}
Value
排序封转
/**
* 使用 Map 按 Value 进行排序
*
* @param oriMap 排序 Map 集合
* @param isRise 是否按照升序排序
*/
public static Map<String, String> sortMapByValue(Map<String, String> oriMap, final boolean isRise) {
if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, String> sortedMap = new LinkedHashMap<>();
// 将oriMap.entrySet()转换成List
List<Map.Entry<String, String>> entryList = new ArrayList<>(oriMap.entrySet());
// 通过比较器来实现排序
Collections.sort(entryList, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
if (isRise) {
// 升序排序
return o1.getValue().compareTo(o2.getValue());
} else {
// 降序排序
return o2.getValue().compareTo(o1.getValue());
}
}
});
Iterator<Map.Entry<String, String>> iterator = entryList.iterator();
Map.Entry<String, String> tmpEntry;
while (iterator.hasNext()) {
tmpEntry = iterator.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return sortedMap;
}
Map
集合
Map<String, String> map = new TreeMap<>();
map.put("fck", "fck");
map.put("anb", "anb");
map.put("bwn", "bwn");
map.put("dba", "dba");
map.put("cba", "cba");
map.put("cab", "cab");
Key
排序
Map<String, String> keyMap = MapSorting.sortMapByKey(map, true);
for (Map.Entry<String, String> entry : keyMap.entrySet()) {
Log.e("log", "key sort-->" + entry.getKey() + " : " + entry.getValue());
}
运行结果
key sort-->anb : anb
key sort-->bwn : bwn
key sort-->cab : cab
key sort-->cba : cba
key sort-->dba : dba
key sort-->fck : fck
Value
排序
Map<String, String> valueMap = MapSorting.sortMapByValue(map, true);
for (Map.Entry<String, String> entry : valueMap.entrySet()) {
Log.e("log", "value sort-->" + entry.getKey() + " : " + entry.getValue());
}
运行结果
value sort-->anb : anb
value sort-->bwn : bwn
value sort-->cab : cab
value sort-->cba : cba
value sort-->dba : dba
value sort-->fck : fck