Map的方法总结

	/**
	 * 将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 + "异常");
		}
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值