Hashmap的存值:
  public static void main(String[] args) {
///*Integer*/map.put("1", 1);//向map中添加值(返回这个key以前的值,如果没有返回null)
HashMap<String, Integer> map=new HashMap<>();
System.out.println(map.put("1", 1));//null
System.out.println(map.put("1", 2));//
}
Hashmap的取值:
 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
map.put("DEMO", 1);
/*Value的类型*///得到map中key相对应的value的值
System.out.println(map.get("1"));//null
System.out.println(map.get("DEMO"));//
}

Hashmap的判断为空:

     public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map是否为空
System.out.println(map.isEmpty());//true
map.put("DEMO", 1);
System.out.println(map.isEmpty());//false
7 }

Hashmap判断是否含有key:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map中是否存在这个key
System.out.println(map.containsKey("DEMO"));//false
map.put("DEMO", 1);
System.out.println(map.containsKey("DEMO"));//true
}

Hashmap判断是否含有value:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map中是否存在这个value
System.out.println(map.containsValue(1));//false
map.put("DEMO", 1);
System.out.println(map.containsValue(1));//true
}

Hashmap删除这个key值下的value:

public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*Integer*///删除key值下的value
System.out.println(map.remove("1"));//null
map.put("DEMO", 2);
System.out.println(map.remove("DEMO"));//2(删除的值)
}

Hashmap显示所有的value值:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*Collection<Integer>*///显示所有的value值
System.out.println(map.values());//[]
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map.values());//[1, 2]
}

Hashmap的元素个数:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*int*///显示map里的值得数量
System.out.println(map.size());//
map.put("DEMO1", 1);
System.out.println(map.size());//
map.put("DEMO2", 2);
System.out.println(map.size());//
}

Hashmap删除这个key值下的value:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*SET<String>*///显示map所有的key
System.out.println(map.keySet());//[]
map.put("DEMO1", 1);
System.out.println(map.keySet());//[DEMO1]
map.put("DEMO2", 2);
System.out.println(map.keySet());//[DEMO1, DEMO2]
}

Hashmap显示所有的key和value:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*SET<map<String,Integer>>*///显示所有的key和value
System.out.println(map.entrySet());//[]
map.put("DEMO1", 1);
System.out.println(map.entrySet());//[DEMO1=1]
map.put("DEMO2", 2);
System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
}

Hashmap添加另一个同一类型的map下的所有制:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
HashMap<String, Integer> map1=new HashMap<>();
/*void*///将同一类型的map添加到另一个map中
map1.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO2=2}
map.putAll(map1);
System.out.println(map);//{DEMO1=1, DEMO2=2}
}

Hashmap删除这个key和value:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///删除这个键值对
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
System.out.println(map.remove("DEMO2", 1));//false
System.out.println(map.remove("DEMO2", 2));//true
System.out.println(map);//{DEMO1=1}
}

Hashmap替换这个key的value:(java8)

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*value*///判断map中是否存在这个key
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
System.out.println(map.replace("DEMO2", 1));//
System.out.println(map);//{DEMO1=1, DEMO2=1}
}

清空这个hashmap:

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*void*///清空map
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
map.clear();//
System.out.println(map);//{}
}

Hashmap的克隆:

     public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*object*///克隆这个map
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map.clone());//{DEMO1=1, DEMO2=2}
Object clone = map.clone();
System.out.println(clone);//{DEMO1=1, DEMO2=2}
}

如果当前 Map 不存在键 key 或者该 key 关联的值为 null,那么就执行 put(key, value);否则,便不执行 put 操作:(java8新增方法)

public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map中是否存在这个key
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
System.out.println(map.putIfAbsent("DEMO1", 12222));//1
System.out.println(map.putIfAbsent("DEMO3", 12222));//null
System.out.println(map);//{DEMO1=1, DEMO2=2,DEMO3=12222}

}

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)compute 方法更适用于更新 key 关联的 value 时,新值依赖于旧值的情况

 public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///当这个value为null时为1,否则为3
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
map.compute("DEMO2", (k,v)->v==null?1:3);
System.out.println(map);//{DEMO1=1, DEMO2=3}
}

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

java8新增方法

方法详解

/**/map.computeIfAbsent(key, mappingFunction);
/**/map.computeIfPresent(key, remappingFunction);
/**/map.forEach());
/**/map.merge(key, value, remappingFunction);
/**/map.getOrDefault(key, defaultValue);

public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*boolean*///判断map中是否存在这个key
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);//{DEMO1=1, DEMO2=2}
System.out.println(map.putIfAbsent("DEMO1", 12222));//1
System.out.println(map.putIfAbsent("DEMO3", 12222));//null
System.out.println(map);//{DEMO1=1, DEMO2=2} 此处应该是 {DEMO1=1, DEMO2=2, DEMO3=12222}
}

HashMap(常用)方法个人理解的更多相关文章

  1. HashMap resize方法的理解(一)

    对于oldTable中存储的为15.7.4.5.8.1,长度为8的一个数组中,存储位置如下 0 1 2 3 4 5 6 7 8 1 4 5 15 7 当扩容到一倍后,对于新的位置的选择通过e.hash ...

  2. Catalina.createDigester方法详细理解

    这个方法主要设置(这个方法很重要,贵在理解,虽然还没学过设计模式..) 1.遇到<server>标签时创建StandardServer实例   设置StandardServer类内部的相关 ...

  3. 遍历HashMap常用的的三种方式

    遍历HashMap常用的的三种方式 HashMap是我们使用非常多的集合之一,下面就来介绍几种常用的HashMap的遍历方式. 1.首先定义一个新的HashMap,并往里面添加一些数据. HashMa ...

  4. 转载:JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  5. [转]Android View.onMeasure方法的理解

    转自:http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html Android View.onMeasure方法的理解 View在屏幕上显示出来要先经过 ...

  6. 五种I/O 模式,select、epoll方法的理解,BIO、NIO、AIO理解 相关文章

    一.io方式 Linux网络编程 五种I/O 模式及select.epoll方法的理解 web优化必须了解的原理之I/o的五种模型和web的三种工作模式 五种I/O 模式——阻塞(默认IO模式),非阻 ...

  7. initWithFrame方法的理解

    initWithFrame方法的理解   有时候,知道initWithFrame方法如何用,但是么有弄明白initWithFrame方法到底是什么? 那就通过查资料弄明白.     1. initWi ...

  8. java中set和get方法的理解

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  9. Java 之HashMap.values()方法误用

    1.出错 今天在测试代码的时候发现程序报错,看代码才知道是使用HashMap.values()方法的时候出错.因为项目中需要获取Map的值的集合然后进行遍历,所以就很自然的调用了HashMap.val ...

随机推荐

  1. Tk1上搭建turtlebot环境

    sudo apt-get install ros-indigo-turtlebot ros-indigo-turtlebot-apps ros-indigo-turtlebot-interaction ...

  2. vue 外卖app(3) 引入阿里图标

    1.登陆阿里图标官网 2.点击搜索按钮,输入你想要搜索的图标,加入购物车,保存到项目中 3.在项目中使用

  3. windows 计算器

    calc sin 弧度与角度

  4. 通过js渲染高层级DOM实现网页加水印

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Kotlin Download

    { https://github.com/JetBrains/kotlin/releases/tag/v1.3.50 }

  6. JAVA大数——lightoj1024

    要用 System.gc() 清理内存 类必须命名成Main,一些大整数的操作 import java.math.BigInteger; import java.util.Scanner; publi ...

  7. Delphi中绘制圆角矩形的窗体

    制作圆角矩形的窗体: 01.procedure TPortForm.FormCreate(Sender: Tobject); 02.var hr :thandle; 03.begin 04.hr:=c ...

  8. sqoop导出数据|Hive|HDFS和脚本编写

    导出数据 在Sqoop中,“导出”概念指:从大数据集群(HDFS,HIVE,HBASE)向非大数据集群(RDBMS)中传输数据,叫做:导出,即使用export关键字. HIVE/HDFS到RDBMS ...

  9. PyQt5显示日期选择框,获取日期保存文件

    一.UI显示选择日期,保存到文件 import sys,os from PyQt5 import QtCore from PyQt5.QtGui import * from PyQt5.QtWidge ...

  10. python解决迅雷下载限制的方法

    一.转换迅雷下载链接 import base64 url='thunder://QUFodHRwOi8veHVubGVpLnhpYXphaS16dWlkYS5jb20vMTkwOC/JqLa+Mi5I ...