【Java】Map之HashMap

1.构造函数

HashMap()

        Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

HashMap(int initialCapacity)
        Constructs an empty HashMap with the specified initial capacity and the default load factor (0.75).
HashMap(int initialCapacity, float loadFactor)
        Constructs an empty HashMap with the specified initial capacity and load factor.
HashMap(Map<? extends K,? extends V> m)
        Constructs a new HashMap with the same mappings as the specified Map.

2.方法摘要


voidclear()
Removes all of the mappings from this map.
Objectclone()
Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
booleancontainsKey(Object key)
Returns true if this map contains a mapping for the specified key.
booleancontainsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
Set<Map.Entry<K,V>>entrySet()
Returns a Set view of the mappings contained in this map.
Vget(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
booleanisEmpty()
Returns true if this map contains no key-value mappings.
Set<K>keySet()
Returns a Set view of the keys contained in this map.
Vput(K key, V value)
Associates the specified value with the specified key in this map.
voidputAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.
Vremove(Object key)
Removes the mapping for the specified key from this map if present.
intsize()
Returns the number of key-value mappings in this map.
Collection<V>values()
Returns a Collection view of the values contained in this map.

3 程序实例

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

public class HashMapInstance {

	public static void main(String[] args) {

		// 创建一个HashMap
		Map<String, Integer> map = new HashMap<String, Integer>();
		// 判断一个Map是否是空
		System.out.println("--Is the map empty? --" + map.isEmpty());

		// 向Map中添加几个键值对
		map.put("A", 1);
		map.put("B", 2);
		map.put("C", 3);
		map.put("D", 4);
		map.put("E", 5);

		// 获取Map的大小并打印Map
		System.out.println("The size of Map is " + map.size());
		System.out.println(map);

		// 查看Map中是否存在某个键
		System.out.println("Contains Key A :" + map.containsKey("A"));
		System.out.println("Contains Key Z :" + map.containsKey("Z"));

		// 查看Map中是否存在某个值
		System.out.println("Contains Value 1 :" + map.containsValue(1));
		System.out.println("Contains Value 10 :" + map.containsValue(10));

		// 打印Map中的所有键及所有值
		System.out.println(map.keySet());
		System.out.println(map.values());

		// 获取Map某个键对应的值,如果没有返回null
		System.out.println(map.get("A"));
		System.out.println(map.get("X"));

		// 从Map中移除键为指定内容的键值对
		System.out.println(map.remove("B"));
		System.out.println(map);

		// 将Map清空
		map.clear();
		System.out.println(map);

		// 遍历hashMap的方法一(相对而言更高效)
		Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, Integer> entry = iterator.next();

			String key = entry.getKey();
			Integer value = entry.getValue();
			System.out.println("Key=" + key + "\t Value" + value);
		}

		// 遍历hashMap的方法二(相对而言效率比前者低一些)
		Iterator<String> iterator2 = map.keySet().iterator();
		while (iterator2.hasNext()) {
			String key = iterator2.next();
			Integer value = map.get(key);
			System.out.println("Key=" + key + "\t Value" + value);
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值