Java中HashMap的用法

Java中HashMap的用法HashMap学习java基础的时候对map不熟悉,再加上图算法经常用到这个结构来存储,特此加一篇文章来介绍Mapimportjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map.Entry;publicclassHashMap{publi……


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

HashMap

学习java基础的时候对map不熟悉,再加上图算法经常用到这个结构来存储,特此加一篇文章来介绍Map

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

public class HashMapTest {

	public static void main(String[] args) {
		HashMap<String, String> map = new HashMap<>();
		map.put("zhang", "31");//存放键值对

		System.out.println(map.containsKey("zhang"));//键中是否包含这个数据
		System.out.println(map.containsKey("daniu"));
		System.out.println("=========================");

		System.out.println(map.get("zhang"));//通过键拿值
		System.out.println(map.get("daniu"));
		System.out.println("=========================");

		System.out.println(map.isEmpty());//判空
		System.out.println(map.size());
		System.out.println("=========================");

		System.out.println(map.remove("zhang"));//从键值中删除
		System.out.println(map.containsKey("zhang"));
		System.out.println(map.get("zhang"));//获取值
		System.out.println(map.isEmpty());
		System.out.println(map.size());
		System.out.println("=========================");

		map.put("zhang", "31");
		System.out.println(map.get("zhang"));
		map.put("zhang", "32");
		System.out.println(map.get("zhang"));
		System.out.println("=========================");

		map.put("zhang", "31");
		map.put("cheng", "32");
		map.put("yun", "33");

		for (String key : map.keySet()) {
			System.out.println(key);
		}
		System.out.println("=========================");

		for (String values : map.values()) {
			System.out.println(values);
		}
		System.out.println("=========================");

		map.clear();
		map.put("A", "1");
		map.put("B", "2");
		map.put("C", "3");
		map.put("D", "1");
		map.put("E", "2");
		map.put("F", "3");
		map.put("G", "1");
		map.put("H", "2");
		map.put("I", "3");
		for (Entry<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key + "," + value);
		}
		System.out.println("=========================");

		// you can not remove item in map when you use the iterator of map
		// for(Entry<String,String> entry : map.entrySet()){
		// if(!entry.getValue().equals("1")){
		// map.remove(entry.getKey());
		// }
		// }

		// if you want to remove items, collect them first, then remove them by
		// this way.
		List<String> removeKeys = new ArrayList<String>();
		for (Entry<String, String> entry : map.entrySet()) {
			if (!entry.getValue().equals("1")) {
				removeKeys.add(entry.getKey());
			}
		}
		for (String removeKey : removeKeys) {
			map.remove(removeKey);
		}
		for (Entry<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key + "," + value);
		}
		System.out.println("=========================");

	}

}


扫码关注更多技术干货

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158889.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • this指针的原理

    this指针的原理讲解this指针的原理是个很复杂的问题,如果我们从javascript里this的实现机制来说明this,很多朋友可能会越来越糊涂,因此本篇打算换一个思路从应用的角度来讲解this指针,从这个角度理解this指针更加有现实意义。  下面我们看看在java语言里是如何使用this指针的,代码如下:复制代码publicclassPerson{privateStringname;priva

    2022年5月17日
  • USB调试助手_蓝牙串口APP

    USB调试助手_蓝牙串口APP今天终于把USB调试助手做完了,程序看起来自己还算满意,虽然有些地方还需完善,但整体功能已经实现了,程序就叫“USB调试助手”吧,是一款USB设备测试工具,可用于研究学习USB协议或进行USB设备开发测试使用,程序已经上传到CSDN了(https://download.csdn.net/download/donghailin/11965800),以方便大家下载,也希望大家提宝贵意…

    2022年9月3日
  • String 字符串 转成List 集合

    String 字符串 转成List 集合第一:代码@RequestMapping(value="/addUserDoMenuPrivilege",method=RequestMethod.POST)@ResponseBodypublicStringaddUserDoMenuPrivilege(intuserID,StringuserCode,Stringmenu_codeList,StringpricodeLis…

    2022年5月15日
  • Python原始字符串无法在字符串结尾加上反斜杠的处理方式

    Python原始字符串无法在字符串结尾加上反斜杠的处理方式

    2022年3月5日
  • transactionscope mysql,如何将TransactionScope与MySql和多个数据库服务器一起使用[通俗易懂]

    transactionscope mysql,如何将TransactionScope与MySql和多个数据库服务器一起使用[通俗易懂]TransactionOptionsTransOpt=newTransactionOptions();TransOpt.IsolationLevel=System.Transactions.IsolationLevel.ReadCommitted;TransOpt.Timeout=newTimeSpan(0,2,0);using(TransactionScopescope…

    2022年7月19日
  • Java 冒泡排序

    Java 冒泡排序Java冒泡排序冒泡排序是最为出名的排序算法之一,总共有八大排序!直接插入排序希尔排序简单选择排序堆排序冒泡排序快速排序归并排序基数排序冒泡排序还是比较简单的,两次循环吗,外层冒泡轮数,里层依次比较。上代码packageArrays;importjava.util.Arrays;/***冒泡排序*1.比较数组中,两个相邻的元素,如果第一个数比第二个大,我们就让它们交换位置*2.每一次比较,都会产生一个最大的,或者最小的数字*3.下

    2022年7月7日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号