HashMap中常用方法总结

由上图我们可以看到在Map集合中,HashMap和LinkedHashMap直接实现了Map接口。接下来总结HashMap常用方法:

1.put(K key, V value) 将键(key)/值(value)映射存放到Map集合中,向HashMap中添加元素

  • 需要注意的是,key不允许重复,否则会覆盖已有的key对应的值
package map;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		int score=map.get("Tom");
		System.out.println(score);
		map.put("Tom", 90);
		score=map.get("Tom");
		System.out.println(score);
	}
}

此时,程序的执行结果为:

2.get(Object key) 返回指定键所映射的值,没有该key对应的值则返回 null

package map;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.get("Lucy"));
		System.out.println(map.get("Tom"));
	}
}

程序的运行结果为:

3.size()  返回Map集合中数据数量

package map;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.size());
	}
}

此时的运行结果为2

4.clear() 清空Map集合

package map;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.size());
		map.clear();
		System.out.println(map.size());
	}
}

此时运行结果为:

5.isEmpty () 判断Map集合中是否有数据,如果没有则返回true,否则返回false

package map;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.size());
		map.clear();
		System.out.println(map.size());
		System.out.println(map.isEmpty());
	}
}

此时的运行结果为:

6.remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值。

package map;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.remove("Tom"));
	}
}

此时代码的运行结果为100

7.values()  返回Map集合中所有value组成的以Collection数据类型格式数据。

package map;
import java.util.Collection;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.remove("Tom"));
		Collection<Integer> con = map.values();
		for (int score : con) {
			System.out.println(score);
		}
	}
}

8.containsKey(Object key)  判断集合中是否包含指定键,包含返回 true,否则返回false

package map;
import java.util.Collection;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.containsKey("Lucy"));	
	}
}

此时的返回值为false

9.containsValue(Object value)  判断集合中是否包含指定值,包含返回 true,否则返回false

package map;
import java.util.Collection;
import java.util.HashMap;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		System.out.println(map.containsValue(90));	
	}
}

此时的返回值为true
10.keySet()  返回Map集合中所有key组成的Set集合

package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
	    Set<String> set=map.keySet();
	    for (String key: set) {
	    	System.out.println(key+" "+map.get(key));
	    }
	}
}

此时代码的运行结果为:

11.entrySet()  将Map集合每个key-value转换为一个Entry对象并返回由所有的Entry对象组成的Set集合

我们先来看下面一段代码:

package map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		Set<String> set=map.keySet();
		for (String key: set) {
			System.out.println(key+" "+map.get(key));
			
		}
		Iterator<String> iterator=set.iterator();
		while(iterator.hasNext()) {
			String key=iterator.next();
			System.out.println(key+" "+map.get(key));	
		}
	}
}

此时如果转化成Entry的形式的话,则为下面的形式:

package map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("Tom",100);
		map.put("Jim", 90);
		Set<Entry<String,Integer>> set=map.entrySet();
		for (Entry<String, Integer> entry : set) {
			System.out.println(entry.getKey()+" "+entry.getValue());
		}
		Iterator<Entry<String,Integer>> iterator=set.iterator();
		while(iterator.hasNext()) {
			Entry<String,Integer> entry=iterator.next();
			System.out.println(entry.getKey()+" "+entry.getValue());
		}
	}
}

此时代码的运行结果为:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值