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. pyhton中的深浅copy

    深浅拷贝:数据分离情况 1. =赋值:数据完全共享(指向内存中的同一个对象)被赋值的变量指向的数据和原变量的数据都是指向内存中的同一个地址: (1)如果是不可变数据类型(数字.字符串等),修改其中的一 ...

  2. php连接数据库 需要下载adodb

    <?include('adodb/ADOdb.inc.php'); # 加载ADODB$conn = &ADONewConnection('odbc_mssql'); # 建立一个连结$ ...

  3. 架构设计 | 接口幂等性原则,防重复提交Token管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.幂等性概念 1.幂等简介 编程中一个幂等操作的特点是其任意多次执行所产生的影响均与一次执行的影响相同.就是说,一次和多次请求某一个资源会产 ...

  4. F. Pathwalks动态开辟线段树

    题意:n点m边,然后要求走最多的路,走路的时候经过的边权必须是严格递增. 解法1:传统的区间更新 解法2:发现区间更新只是对两个固定的点所延长形成的区间段,所以问题可以退化成单点更新单点查询. 然后动 ...

  5. poj1780欧拉回路

    转载 #include<cstdio> #include<cstring> ; bool vis[N]; char ans[N]; int main() { int n; wh ...

  6. 蓝桥杯 试题 历届试题 填字母游戏 博弈+dfs剪枝

    问题描述 小明经常玩 LOL 游戏上瘾,一次他想挑战K大师,不料K大师说: “我们先来玩个空格填字母的游戏,要是你不能赢我,就再别玩LOL了”. K大师在纸上画了一行n个格子,要小明和他交替往其中填入 ...

  7. python+selenium实现百度关键词搜索自动化操作

    缘起 之前公司找外面网络公司做某些业务相关关键词排名,了解了一下相关的情况,网络公司只需要我们提供网站地址和需要做的关键词即可,故猜想他们采取的方式应该是通过模拟用户搜索提升网站权重进而提升排名. 不 ...

  8. 项目中 SimpleDateFormat 的正确使用

    项目中 SimpleDateFormat 的正确使用 日常开发中,我们经常需要使用时间相关类,说到时间相关类,想必大家对 SimpleDateFormat 并不陌生.主要是用它进行时间的格式化输出和解 ...

  9. docker安装和基本使用

    1.安装环境 此处在Centos7进行安装,可以使用以下命令查看CentOS版本 lsb_release -a 在 CentOS 7安装docker要求系统为64位.系统内核版本为 3.10 以上,可 ...

  10. 四使用浮动div布局

    刚开始学习的小白,如有不足之处还请各位补充,感激涕零.在html中有两种方式布局<table>表格和<div>,个人剧的使用表格布局可以避免bug产生,并且表格布局相对来说要容 ...