Map的方法总结
2016-10-14 10:09
190 查看
/** * 将map转换为list */ @Test public void testKeyValueEntrySet(){ Map<String, String> map = new HashMap<String, String>(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); //keySet,a,b,c List list1 = new ArrayList<>(map.keySet()); //values:1,2,3 List list2 = new ArrayList<>(map.values()); //entrySet:a=1,b=2,c=3 //注意在list3中map.entrySet()返回的是一个<Map.Entry<K,V>>的泛型, List list3 = new ArrayList<>(map.entrySet()); } /** * 遍历map */ @Test public void testTraversalMap(){ Map<String, String> map = new HashMap<String, String>(); map.put("a", "1"); map.put("b", "2"); map.put("c", "3"); //使用Entry遍历Map //a:1 //b:2 //c:3 for(Entry<String,String> entry : map.entrySet()){ System.out.print(entry.getKey() + ":"); System.out.println(entry.getValue()); } //使用Iterator和Entry //a:1 //b:2 //c:3 Iterator itr = map.entrySet().iterator(); while(itr.hasNext()){ Entry e = (Entry) itr.next(); System.out.print(e.getKey() + ":"); System.out.println(e.getValue()); } } /** * 通过key来对Map进行排序 */ @Test public void sortMapByKey(){ Map<String, String> map = new HashMap<String, String>(); map.put("2", "b"); map.put("1", "a"); map.put("4", "d"); map.put("3", "c"); //1:a //2:b //3:c //4:d List<Map.Entry<String, String>> list = new ArrayList<Entry<String,String>>(map.entrySet()); Collections.sort(list, new Comparator<Entry<String,String>>() { @Override public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o1.getKey().compareTo(o2.getKey()); } }); for(Entry<String,String> entry: list){ System.out.println(entry.getKey() + ":" + entry.getValue()); } //另外如果是treeMap类型,因为TreeMap默认是按Key升序的 Map<String,String> map1 = new TreeMap<String,String>(); map1.put("2", "b"); map1.put("1", "a"); map1.put("4", "d"); map1.put("3", "c"); //1:a //2:b //3:c //4:d for(Entry<String,String> entry : map1.entrySet()){ System.out.println(entry.getKey() + ":" + entry.getValue()); } //现在我们按key的降序来将treeMap排序 Map<String,String> map2 = new TreeMap<>(new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); map2.put("2", "b"); map2.put("1", "a"); map2.put("4", "d"); map2.put("3", "c"); //4:d //3:c //2:b //1:a for(Entry<String,String> entry : map2.entrySet()){ System.out.println(entry.getKey() + ":" + entry.getValue()); } } /** * 通过value进行排序 */ @Test public void sortMapByValue(){ Map<String,String> map = new HashMap<String,String>(); map.put("2", "b"); map.put("1", "a"); map.put("4", "d"); map.put("3", "c"); List<Entry<String, String>> list = new ArrayList<Entry<String,String>>(map.entrySet()); Collections.sort(list, new Comparator<Entry<String, String>>() { @Override public int compare(Entry<String, String> o1, Entry<String, String> o2) { return o1.getValue().compareTo(o2.getValue()); } }); //1:a //2:b //3:c //4:d for(Entry<String,String> entry : list){ System.out.println(entry.getKey() + ":" + entry.getValue()); } } /** * 对Map进行复制 */ @Test public void copyMap(){ Map<String, String> map = new HashMap<String,String>(); map.put("1", "a"); map.put("2", "b"); Map<String,String> map1 = Collections.synchronizedMap(map); //1:a //2:b for(Entry<String,String> entry : map1.entrySet()){ System.out.println(entry.getKey() + ":" + entry.getValue()); } } /** * 设置Map为不可用 */ @Test public void emptyMap(){ Map<String,String> map = new HashMap<String,String>(); map = Collections.emptyMap(); //java.lang.UnsupportedOperationException map.put("1", "a"); }
/**
* 创建一个今天不可变的常量map
* @author liwenbin
*
*/
public class MapTestTwo {
private static final Map<String,String> map;
static{
Map<String,String> map1 = new HashMap<>();
map1.put("a", "1");
map1.put("b", "2");
map = Collections.unmodifiableMap(map1);
}
public static void main(String[] args) {
try {
//当尝试put时会报异常
//java.lang.UnsupportedOperationException异常
MapTestTwo.map.put("c", "3");
} catch (Exception e) {
System.out.println(e + "异常");
}
}
}
相关文章推荐
- map containsKey 与get方法 区别 经典 总结
- vector,map各种容器类型的使用方法总结
- map用迭代器进行遍历的方法总结
- Map获取键值,Map的几种遍历方法总结(推荐)
- JS数组方法总结shift()、unshift()、pop()、push()、concat()、splice()、filter()、map()、some()、every()、forEach()等方法
- STL-MAP使用方法总结
- map containsKey与get方法区别经典总结
- GUAVA常用方法总结整理(list map string concurrent file)
- java中List、Set、Map、Properties集合的遍历方法总结
- java中List、Set、Map集合的遍历方法总结
- java map遍历的四种方法总结
- java中遍历map的各种方法总结
- map方法总结
- List,Set,Map集合使用方法总结
- 总结:各种加锁方法对于Map .(转)
- 这里总结了在Java中遍历Map的value值的三种方法,思路各不相同。
- java中List、Set、Map集合的遍历方法总结
- Map遍历的方法总结
- map用迭代器进行遍历的方法总结
- 自我对Map的使用方法总结与归纳!