package com.test3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry; public class demo3 { 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 (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("========================="); }
}

  

Java基础系列 - HashMap的更多相关文章

  1. Java基础系列--HashMap(JDK1.8)

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/10022092.html Java基础系列-HashMap 1.8 概述 HashMap是 ...

  2. Java基础系列-Collector和Collectors

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/10748925.html 一.概述 Collector是专门用来作为Stream的coll ...

  3. Java基础系列-二进制操作

    原创文章,转载请标注出处:<Java基础系列-二进制操作> 概述 Java源码中涉及到大量的二进制操作,非常的复杂,但非常的快速. Java二进制表示法 首先了解下二进制,二进制是相对十进 ...

  4. Java基础系列-equals方法和hashCode方法

    原创文章,转载请标注出处:<Java基础系列-equals方法和hashCode方法> 概述         equals方法和hashCode方法都是有Object类定义的. publi ...

  5. 夯实Java基础系列1:Java面向对象三大特性(基础篇)

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 [https://github.com/h2pl/Java-Tutorial](https: ...

  6. 2015年12月28日 Java基础系列(六)流

    2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流

  7. Java基础系列--static关键字

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8477914.html 一.概述 static关键字是Java诸多关键字中较常使用的一个,从 ...

  8. Java基础系列-ArrayList

    原创文章,转载请标注出处:<Java基础系列-ArrayList> 一.概述 ArrayList底层使用的是数组.是List的可变数组实现,这里的可变是针对List而言,而不是底层数组. ...

  9. Java基础系列-Comparable和Comparator

    原创文章,转载请标注出处:<Java基础系列-Comparable和Comparator> 一.概述         Java中的排序是由Comparable和Comparator这两个接 ...

随机推荐

  1. axios 发 post 请求,后端接收不到参数的解决方案(转载)

    原文地址:https://www.cnblogs.com/yiyi17/p/9409249.html 问题场景 场景很简单,就是一个正常 axios post 请求: axios({ headers: ...

  2. iOS - 回顾总结Runtime原理及使用

    runtime简介 因为Objc是一门动态语言,所以它总是想办法把一些决定工作从编译连接推迟到运行时.也就是说只有编译器是不够的,还需要一个运行时系统 (runtime system) 来执行编译后的 ...

  3. UI5-技术篇-SAPUI5创建自定义控件

    转载:https://www.nabisoft.com/tutorials/sapui5/creating-custom-controls-in-sapui5     https://sapui5.h ...

  4. laravel管理模型插入

    post控制器public function comment(Post $post,Request $request){ try{ if(empty($request->content)){ E ...

  5. 实现对MySQL数据库进行分库/分表备份(shell脚本)

    工作中,往往数据库备份是件非常重要的事情,毕竟数据就是金钱,就是生命!废话不多,下面介绍一下:如何实现对MySQL数据库进行分库备份(shell脚本) Mysq数据库dump备份/还原语法: mysq ...

  6. 七年开发经验详解JVM的GC 算法

    概述 GC 是 JVM 自带的功能,它能够自动回收对象,清理内存,这是 Java 语言的一大优势,但是GC绝不仅伴随着Java,相反,GC历史比Java更悠久.关于GC,我认为有四个问题需要解决: 为 ...

  7. prometheus监控(小试牛刀)

    prometheus监控(小试牛刀) 环境:全部服务都是基于docker运行 本文略微草率,好文章在这里,特别好如下: https://www.cnblogs.com/tchua/p/11120228 ...

  8. 在openwrt 17.01上编译最新nginx 1.14.2的笔记

    openwrt 17.01源码对应的nginx版本是1.10.2,有些新功能没有,所以需要升级到nginx 1.14.2最新的稳定版 https://github.com/macports/macpo ...

  9. 基于fastai的分类网络

    classify.fastai Classifier based on fastai 欢迎访问:https://github.com/pprp/classify.fastai/ 字符识别分类器-基于f ...

  10. 【新品发布】智能驾驶实车测试系统-VDAS

    智能驾驶技术的迭代研发,需要多种传感器.海量数据.海量场景的支撑.而目前多种传感器Gbit/s级别的数据同步采集.海量数据的快速分析和评估.关键场景的切片和提取,是业界公认的棘手问题. 为了解决上述的 ...