JavaSE8基础 HashMap<Integer,String> containsKey/Value 判断集合是否包含指定的键或值

os :windows7 x64
    jdk:jdk-8u131-windows-x64
    ide:Eclipse Oxygen Release (4.7.0)

code:

package jizuiku0;

import java.util.HashMap;

/*
 * @version V17.09
 */
public class MapDemo_100 {
	public static void main(String[] args) {
		HashMap<Integer, String> hm = init();

		System.out.println(hm);

		System.out.println("包含键1吗? "+hm.containsKey(1));
		System.out.println("包含值hello world吗? "+hm.containsValue("hello world"));
	}

	public static HashMap<Integer, String> init() {
		HashMap<Integer, String> hm = new HashMap<Integer, String>();

		// 这里的键 在添加时是乱序的,然而在输出时 会有一个很有趣的现象
		// 要想知道这个现象背后的原因,就必须了解底层的代码实现
		// 所谓 玄之又玄,众妙之门
		hm.put(1, "北斗第一阳明贪狼太星君");
		hm.put(2, "北斗第二阴精巨门元星君");
		hm.put(5, "北斗第五丹元廉贞罡星君");
		hm.put(6, "北斗第六北极武曲纪星君");
		hm.put(7, "北斗第七天卫破军关星君");
		hm.put(3, "北斗第三福善禄存真星君");
		hm.put(4, "北斗第四玄冥文曲纽星君");
		hm.put(8, "北斗第八左辅洞明星君");
		hm.put(9, "北斗第九右弼隐光星君");

		return hm;
	}
}

result:



Java优秀,值得学习。Java优秀,值得学习。
学习资源:API手册+Java源码+清净的心地。

时间: 2024-08-25 15:50:18

JavaSE8基础 HashMap<Integer,String> containsKey/Value 判断集合是否包含指定的键或值的相关文章

JavaSE8基础 HashMap&lt;Integer,String&gt; keySet遍历 根据键找值

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.util.HashMap; import java.util.Set; /* * @version V17.09 */ public class MapDemo_1110 { public static void main(String[] args) { H

JavaSE8基础 HashMap&lt;Integer,String&gt; keySet values 获取所有键 所有值的集合

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.util.Collection; import java.util.HashMap; import java.util.Set; /* * @version V17.09 */ public class MapDemo_110 { public static

JavaSE8基础 HashMap&lt;Integer,String&gt; get 根据键得到值

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.util.HashMap; /* * @version V17.09 */ public class MapDemo_101 { public static void main(String[] args) { HashMap<Integer, String>

JavaSE8基础 HashMap&lt;Integer,String&gt; entrySet遍历 键值对的集合

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; /* * @version V17.09 */ public class MapDemo_1111 { public static

Map.containsKey方法——判断Map集合对象中是否包含指定的键名

该方法判断Map集合对象中是否包含指定的键名.如果Map集合中包含指定的键名,则返回true,否则返回false. public static void main(String[] args) { Map map = new HashMap(); //定义Map对象 map.put("apple", "新鲜的苹果"); //向集合中添加对象 map.put("computer", "配置优良的计算机"); map.put(&q

containsKey方法——判断是否包含指定的键名

containsKey方法用来判断Map集合对象中是否包含指定的键名. 语法  boolean containsKey(Object key) 返回值:如果Map集合中包含指定的键名,则返回true:否则返回false. 参数:key是要查询的Map集合的键名对象. 示例  本示例首先使用HashMap类创建Map集合对象,并向集合中添加几个元素,然后调用containsKey方法查询是否包含指定的键名. public static void main(String[] args) { Map

JavaSE8基础 HashMap isEmpty clear 判断该映射空不空与删除所有键值对

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.util.HashMap; /* * @version V17.09 */ public class MapDemo_001 { public static void main(String[] args) { HashMap<Integer, String>

JavaSE8基础 HashMap remove 根据键来删除键值对

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.util.HashMap; /* * @version V17.09 */ public class MapDemo_010 { public static void main(String[] args) { HashMap<Integer, String>

JavaSE8基础 HashMap remove 删除指定的键值对

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; import java.util.HashMap; /* * @version V17.09 */ public class MapDemo_011 { public static void main(String[] args) { HashMap<Integer, String>