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. 2019_8_1python

    #函数 #函数是用来重复使用哒 #定义函数套路 ''' 1.首先要会写出裸代码,然后看看哪里是重复需要使用的 2.接下来将需要重复使用的代码转换成参数,带入到函数中 函数格式 def funcName ...

  2. Ubuntu Server 19配置静态IP

    这个/etc/netplan下默认有个文件50-cloud-init.yaml,直接修改它就行了 sudo vim /etc/netplan/50-cloud-init.yaml 网口名字ens33可 ...

  3. ElementUI的Loading组件 —— 想实现在请求后台数据之前开启Loading组件,请求成功或失败之后,关闭Loading组件

    我在实际项目开发中,遇到了这个需求,记录一下~~~~~~ 在ElementUI官网上有几种实现Loading的方法,但官网上是在一个方法里写了开启与关闭组件,所以可以根据官网的实现方法进行一个封装,便 ...

  4. 第二天:PowerShell别名

    1.查询别名: Get-Alias -name ls Get-Alias -name dir Get-Alias -name fl Get-Alias -name ft 2.查看可用的别名 查看可用的 ...

  5. vue 中使用 lazyload 插件 数据更图片不更新 的原因 及解决方案

    在使用lazyload插件的img标签上,加上:key标识即可

  6. VC++6.0环境中输出特殊字符

    该方法最靠谱:https://blog.csdn.net/xiaofeilong321/article/details/8713002 输出特殊字符需使用扩展的ASCII码. 修改控制台显示设置: ( ...

  7. cocos2D-X 常见49种Action

    bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init( ...

  8. java 接受带有中文的get请求文件下载时的问题

    参数是接受到了 , debug的时候也能看的到 , 但是奇怪的是就是找不到文件 @ApiOperation(value = "文件下载/图片预览") @GetMapping(val ...

  9. 在Panel上绘图的实现

    近期制作了FDS的一个建模工具,由于知识有限,做出的效果是2D的.昨天上课的时候看老师画一个长方体,突然想到,为什么不给普通的2D图形加画上几条直线,就能实现2D图形的3D视觉效果呢?于是回来马上做了 ...

  10. NX二次开发-UFUN创建工程图注释UF_DRF_create_note

    NX9+VS2012 #include <uf.h> #include <uf_drf.h> #include <NXOpen/Annotations_Note.hxx& ...