Java Map总结

Map是一个由键值对组成的数据结构,且在每个集合中键是唯一的。

 

为测试以下功能,开始一个具有JUnit测试功能的类

 

import org.junit.Test;

public class MapTest {

	private Map<String, Object> map = new HashMap<String, Object>();

	public MapTest() {
		map.put("userName", "admin");
		map.put("password", "123456");
		map.put("age", 11);
	}
}

 

 

 0、将Map转化为List类型

 

在Java中Map提供了三种集合的获取方式:key set , value set , key-value set.它们都可以通过构造方法或者addAll(Collection<? extends E> c)方法来转化为List类型。

 

@Test
	public void map2list() {

		List<String> keyList = new ArrayList<String>(map.keySet());
		for (String key : keyList) {
			System.out.print("[" + key + "]");
		}

		System.out.println();
		List<Object> valueList = new ArrayList<Object>(map.values());
		for (Object value : valueList) {
			System.out.print("[" + value + "]");
		}

		System.out.println();
		List<Entry<String, Object>> entryList = new ArrayList<Entry<String, Object>>(
				map.entrySet());
		for (Entry<String, Object> entry : entryList) {
			System.out.print("[" + entry + "]");
		}
	}

 

 运行结果:

 

[age][userName][password]
[11][admin][123456]
[age=11][userName=admin][password=123456]

 

1、遍历Map集合

 

通过几种最常用的方法进行遍历

 

@Test
	public void circleMap() {
		for (Entry<String, Object> entry : map.entrySet()) {
			System.out.print("[" + entry.getKey() + "," + entry.getValue()
					+ "]");
		}

		System.out.println();
		Iterator<String> iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			System.out.print("[" + key + "," + map.get(key) + "]");
		}

		System.out.println();
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			System.out.print("[" + key + "," + map.get(key) + "]");
		}
	}

 

运行结果:

 

[age,11][userName,admin][password,123456]
[age,11][userName,admin][password,123456]
[age,11][userName,admin][password,123456]

 

 2、根据Key来对Map排序

 

@Test
	public void compareByKey() {
		List<Entry<String, Object>> list = new ArrayList<Entry<String, Object>>(
				map.entrySet());
		Collections.sort(list, new Comparator<Entry<String, Object>>() {
			@Override
			public int compare(Entry<String, Object> e1,
					Entry<String, Object> e2) {
				return e1.getKey().compareTo(e2.getKey());
			}

		});
		for (Entry<String, Object> entry : list) {
			System.out.print("[" + entry.getKey() + "," + entry.getValue()
					+ "]");
		}

		System.out.println();
		SortedMap<String, Object> sortedMap = new TreeMap<String, Object>(
				new Comparator<String>() {

					@Override
					public int compare(String k1, String k2) {
						return k1.compareTo(k2);
					}
				});
		sortedMap.putAll(map);
		Iterator<String> iterator = sortedMap.keySet().iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			System.out.print("[" + key + "," + sortedMap.get(key) + "]");
		}
	}

 

运行结果:

 

[age,11][password,123456][userName,admin]
[age,11][password,123456][userName,admin]

 

3、根据Value来对Map进行排序

 

由于初始化Map对象中的Value值有不同的类型,所以在比较过程中做了一些处理,全部转为String类型比较。

 

@Test
	public void compareByValue() {
		List<Entry<String, Object>> list = new ArrayList<Entry<String, Object>>(
				map.entrySet());
		Collections.sort(list, new Comparator<Entry<String, Object>>() {
			@Override
			public int compare(Entry<String, Object> e1,
					Entry<String, Object> e2) {
				String value1 = "", value2 = "";
				if (e1.getValue() instanceof Integer) {
					value1 = Integer.toString((Integer) e1.getValue());
				} else {
					value1 = (String) e1.getValue();
				}
				if (e2.getValue() instanceof Integer) {
					value2 = Integer.toString((Integer) e2.getValue());
				} else {
					value2 = (String) e2.getValue();
				}
				return value1.compareTo(value2);
			}

		});
		for (Entry<String, Object> entry : list) {
			System.out.print("[" + entry.getKey() + "," + entry.getValue()
					+ "]");
		}
	}

 

运行结果:

 

[age,11][password,123456][userName,admin]

 

4、初始化一个static的常量map

 

有两种方式声明一个全局静态的Map,且都是线程安全的。

 

在MapStaticFinalstst.java中,第一种方式声明的可以在任何地方修改map里面的值

 

 

package com.code.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MapStaticFinalstst {

	private static final Map<Object, Object> map;
	static {
		map = new HashMap<Object, Object>();
		map.put("1", "st");
		map.put("2", "nd");
	}

	public static void main(String[] args) {
		map.put("3", "rd");
		for (Entry<Object, Object> entry : map.entrySet()) {
			System.out.print("[" + entry.getKey() + "," + entry.getValue()
					+ "]");
		}
	}
}

 

运行结果:

 

 

[3,rd][2,nd][1,st]

 

在MapStaticFinalstnd.java中,unmodifiableMap返回指定映射的不可修改视图,当我们尝试修改map数据时,会发生异常。

 

 

package com.code.map;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MapStaticFinalstnd {

	private static final Map<Object, Object> map;
	static {
		Map<Object, Object> aMap = new HashMap<Object, Object>();
		aMap.put("1", "st");
		aMap.put("2", "nd");
		map = Collections.unmodifiableMap(aMap);
	}

	public static void main(String[] args) {
		map.put("3", "rd");
		for (Entry<Object, Object> entry : map.entrySet()) {
			System.out.print("[" + entry.getKey() + "," + entry.getValue()
					+ "]");
		}
	}
}

 

运行结果:

 

 

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.Collections$UnmodifiableMap.put(Unknown Source)
	at com.code.map.MapStaticFinalstnd.main(MapStaticFinalstnd.java:19)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值