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> hm = init();

		System.out.println(hm);

		// 移出指定的键值对  ,这个键值对在集合中是存在的,所以删除成功 返回true
		System.out.println(hm.remove(1, "北斗第一阳明贪狼太星君"));
		System.out.println(hm);

		// 2=hello world ,这个键值对在集合是不存在的,所以删除失败 返回false
		System.out.println(hm.remove(2, "hello world"));

		System.out.println(hm);
	}

	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优秀,值得学习。
学习资源:API手册 + Java源码 + 清净的心地。

时间: 2025-01-03 21:31:07

JavaSE8基础 HashMap remove 删除指定的键值对的相关文章

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

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&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基础 StringBuffer delete 删除指定索引值的一个字符

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku1; public class Demo010 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append("cnblog_").append(

对一个给定的二维数组按照指定的键值进行排序

public function set_s(){ $arr = [ ['one' => 6,'two' => 19], ['one' => 36,'two' => 3], ['one' => 26,'two' => 3], ['one' => 2,'two' => 84], ['one' => 5,'two' => 35], ['one' => 6,'two' => 56], ['one' => 7,'two' => 7]

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&lt;Integer,String&gt; 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>

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