Hashmap的存值:(map.put(key,value))

1 public static void main(String[] args) {

2 ///Integer/map.put("1", 1);//向map中添加值(返回这个key以前的值,如果没有返回null)

3 HashMap<String, Integer> map=new HashMap<>();

4 System.out.println(map.put("1", 1));//null

5 System.out.println(map.put("1", 2));//1

6 }

Hashmap的取值:(map.get(key))

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 map.put("DEMO", 1);

4 /Value的类型///得到map中key相对应的value的值

5 System.out.println(map.get("1"));//null

6 System.out.println(map.get("DEMO"));//1

7 }

Hashmap的判断为空:(map.isEmpty())

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /boolean///判断map是否为空

4 System.out.println(map.isEmpty());//true

5 map.put("DEMO", 1);

6 System.out.println(map.isEmpty());//false

7 }

Hashmap判断是否含有key:(map.containsKey(key))

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /boolean///判断map中是否存在这个key

4 System.out.println(map.containsKey("DEMO"));//false

5 map.put("DEMO", 1);

6 System.out.println(map.containsKey("DEMO"));//true

7 }

Hashmap判断是否含有value:(map.containsValue(value))

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /boolean///判断map中是否存在这个value

4 System.out.println(map.containsValue(1));//false

5 map.put("DEMO", 1);

6 System.out.println(map.containsValue(1));//true

7 }

Hashmap删除这个key值下的value:(map.remove(key))

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值:(map.values())

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /Collection///显示所有的value值

4 System.out.println(map.values());//[]

5 map.put("DEMO1", 1);

6 map.put("DEMO2", 2);

7 System.out.println(map.values());//[1, 2]

8 }

Hashmap的元素个数:(map.size())

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /int///显示map里的值得数量

4 System.out.println(map.size());//0

5 map.put("DEMO1", 1);

6 System.out.println(map.size());//1

7 map.put("DEMO2", 2);

8 System.out.println(map.size());//2

9 }

Hashmap删除这个key值下的value:

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /SET///显示map所有的key

4 System.out.println(map.keySet());//[]

5 map.put("DEMO1", 1);

6 System.out.println(map.keySet());//[DEMO1]

7 map.put("DEMO2", 2);

8 System.out.println(map.keySet());//[DEMO1, DEMO2]

9 }

Hashmap显示所有的key和value:

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /SET<map<String,Integer>>///显示所有的key和value

4 System.out.println(map.entrySet());//[]

5 map.put("DEMO1", 1);

6 System.out.println(map.entrySet());//[DEMO1=1]

7 map.put("DEMO2", 2);

8 System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]

9 }

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

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 HashMap<String, Integer> map1=new HashMap<>();

4 /void///将同一类型的map添加到另一个map中

5 map1.put("DEMO1", 1);

6 map.put("DEMO2", 2);

7 System.out.println(map);//{DEMO2=2}

8 map.putAll(map1);

9 System.out.println(map);//{DEMO1=1, DEMO2=2}

10 }

Hashmap删除这个key和value:

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /boolean///删除这个键值对

4 map.put("DEMO1", 1);

5 map.put("DEMO2", 2);

6 System.out.println(map);//{DEMO1=1, DEMO2=2}

7 System.out.println(map.remove("DEMO2", 1));//false

8 System.out.println(map.remove("DEMO2", 2));//true

9 System.out.println(map);//{DEMO1=1}

10 }

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

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /value///判断map中是否存在这个key

4 map.put("DEMO1", 1);

5 map.put("DEMO2", 2);

6 System.out.println(map);//{DEMO1=1, DEMO2=2}

7 System.out.println(map.replace("DEMO2", 1));//2

8 System.out.println(map);//{DEMO1=1, DEMO2=1}

9 }

清空这个hashmap:

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /void///清空map

4 map.put("DEMO1", 1);

5 map.put("DEMO2", 2);

6 System.out.println(map);//{DEMO1=1, DEMO2=2}

7 map.clear();//2

8 System.out.println(map);//{}

9 }

Hashmap的克隆:

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /object///克隆这个map

4 map.put("DEMO1", 1);

5 map.put("DEMO2", 2);

6 System.out.println(map.clone());//{DEMO1=1, DEMO2=2}

7 Object clone = map.clone();

8 System.out.println(clone);//{DEMO1=1, DEMO2=2}

9 }

如果当前 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 时,新值依赖于旧值的情况

1 public static void main(String[] args) {

2 HashMap<String, Integer> map=new HashMap<>();

3 /boolean///当这个value为null时为1,否则为3

4 map.put("DEMO1", 1);

5 map.put("DEMO2", 2);

6 System.out.println(map);//{DEMO1=1, DEMO2=2}

7 map.compute("DEMO2", (k,v)->v==null?1:3);

8 System.out.println(map);//{DEMO1=1, DEMO2=3}

9 }

如果当前 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}

}


StringBuffer的方法:

 public int capacity():返回当前容量,理论值

  public int length():返回长度(字符数) ,实际值

package com.stringbuffer;

public class TestStringBufffer {

public static void main(String[] args) {
//StringBuffer构造方法1
StringBuffer sb1=new StringBuffer("Hello");
System.out.println(sb1); String s1="World";
//StringBuffer构造方法2
StringBuffer sb2=new StringBuffer(s1);
System.out.println(sb2); //length()返回字符串的长度
System.out.println(sb2.length());
//toString()这个方法重写了Object中的toString()方法,返回String类型的字符串
//输出StringBuffer对象时候,会默认调用此方法
System.out.println(sb2); //append(String s)方法在原有的字符串后面添加字符串,返回的是添加后的StringBuffer对象
sb1.append(" World");
System.out.println(sb1); //public StringBuffer deleteCharAt(int index)
//该方法的作用是删除指定位置的字符,然后将剩余的内容形成新的字符串
sb1.deleteCharAt(0);
System.out.println(sb1);//ello World //public StringBuffer delete(int start,int end)
//从字符缓冲区中从start索引删除到end索引所对应的字符,其中包括start索引不包括end索引对应的值
sb1.delete(1, 3);
System.out.println(sb1); //public StringBuffer insert(int offset,String str)
//在字符串缓冲区的第offset个字符后面插入指定字符串
sb1.insert(1, "ME");
System.out.println(sb1); //public StringBuffer reverse(),将字符串反转
sb1.reverse();
System.out.println(sb1);
}

}

HashMap的方法及功能、StringBuffer的方法的更多相关文章

  1. Java—API/Obiect类的equals toString方法/String类/StringBuffer类/正则表达式

    API  Java 的API(API: Application(应用) Programming(程序) Interface(接口)) 就是JDK中提供给我们使用的类,这些类将底层的代码实现封装了起来 ...

  2. 在Excel中使用频率最高的函数的功能和使用方法

    在Excel中使用频率最高的函数的功能和使用方法,按字母排序: 1.ABS函数 函数名称:ABS 主要功能:求出相应数字的绝对值. 使用格式:ABS(number) 参数说明:number代表需要求绝 ...

  3. primace 5.0软件的Debug ware 功能的使用方法简介

    用primace 软件已经一年多了,一直不知道Debug ware 软件怎么使用,上周终于逮住FAE请教了下这个功能的使用方法.发现这个功能和signalTap ii 原理不一样,这个是非时事的波形, ...

  4. Keil的使用方法 - 常用功能(二)

    Ⅰ.概述 上一篇文章是总结关于Keil使用方法-常用功能(一),关于(文件和编译)工具栏每一个按钮的功能描述和快捷键的使用. 我将每一篇Keil使用方法的文章都汇总在一起,回顾前面的总结请点击下面的链 ...

  5. duilib中控件拖拽功能的实现方法(附源码)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...

  6. Format类及其子类功能和使用方法具体解释

    Format类及其子类功能和使用方法具体解释 1.   Format类结构: ·        java.lang.Object ·        java.text.Format ·         ...

  7. 关于String.concat()方法和StringBuffer.append()方法的学习:方法是如何追加字符到源字符串的

    问题分析: 首先,看看两段代码的运行结果,两段代码分别是: 第一段代码,关于String.concat()方法的测试: public static void main(String[] args) { ...

  8. CPU-Z五大主要功能及使用方法初步了解

    CPU-Z这款软件除了具有查看CPU温度这个功能之外,还有很多其他的功能.今天就和小编一起去看看CPU-Z的5大功能以及他们的使用方法吧! CPU信息标签页 CPU-Z介绍: CPU-Z是一款著名的免 ...

  9. Math类中round、ceil和floor方法的功能

    Java中的Math工具类用来完成除+.-.*./.%等基本运算以外的复杂运算,位于java.lang包下,Math类的构造器全是私有的(private),因此无法创建Math类的对象,Math类的方 ...

随机推荐

  1. mysql小白系列_14 线上故障分析与排错

    1.重现故障5---线上执行update报错,并处理.(表结构和UPDATE语句自己构造,请给出详细步骤) 1)update故障出现ERROR 1206 (HY000): The total numb ...

  2. 根据ip查询ip归属地

    http://www.oschina.net/code/snippet_944819_33978 http://www.jb51.net/article/54287.htm public String ...

  3. Java switch case语句

    switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. switch case 语句语法格式如下: switch(expression){ case value : ...

  4. 【常用工具】vagrant的box哪里下?镜像在哪儿找?教你在vagrant官网下载各种最新.box资源

    进入vagrant官网 : https://www.vagrantup.com/ 点击findbox[寻找box],进入有很多box的列表 : https://app.vagrantup.com/bo ...

  5. Java入门以及使用IDEA安装和使用

    目录 JAVA第一个程序 HelloWorld 可能会遇到的情况 JAVA程序运行机制 IDEA安装 什么是IDE IDEA介绍 IDEA下载 IDEA基本使用 关于一些创建项目的小tips JAVA ...

  6. 测试开发专题:spring-boot如何使用JPA进行双向一对多配置

    本片文章我们主要介绍spring-boot如何进行JPA的配置以及如何进行实体间的一对多配置. 依赖准备 要在spring-boot使用jpa需要在项目中有进入相关的依赖,pom文件里加入下面内容 & ...

  7. mysql-connector-java 6版本的jdbc连接问题

    使用新版本6的jdbc驱动,会出现下面的问题 Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The s ...

  8. JAVA定义变量和基础的数据类型和关键字

      标识符语法 1)java中定义标识符格式以字母,数字,下划线,$符合组成,不能以数字开头,且不能为  java中的关键字.         2)标识符意义要明确,不要乱起         3)标识 ...

  9. Servlet配置及生命周期

    1.设置Ecilipse快捷  file new 2.创建Servlet程序 1). 创建一个 Servlet 接口的实现类.              public class HelloServl ...

  10. python基本操作-文件、目录及路径

    目录 1 前言 2 文件夹操作 2.1 查询操作 2.2 创建操作 2.3 删除操作 2.4 修改操作 3 文件操作 3.1 查询操作 3.2 创建操作 3.3 修改操作 3.4 删除 4 路径操作 ...