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. C# winform DataGridView 绑定数据的的几种方法

    1.用DataSet和DataTable为DataGridView提供数据源 String strConn = "Data Source=.;Initial Catalog=His;User ...

  2. mysql小白系列_06 备份与恢复

    1.使用mydumper工具全库备份. 1)源码编译安装 2)全库备份 2.误操作truncate table gyj_t1;利用mysqldump的备份和binlog日志对表gyj_t1做完全恢复. ...

  3. apache slowloris mod_antiloris for Apache httpd 2.2 / 2.4

    http://www.apachelounge.com/viewtopic.php?t=4222

  4. Spring全家桶——SpringBoot渐入佳境

    Spring全家桶系列--SpringBoot渐入佳境 萌新:小哥,我在实体类写了那么多get/set方法,看着很迷茫 小哥:那不是可以自动生成吗? 萌新:虽然可以自动生成,但是如果我要修改某个变量的 ...

  5. 读Pyqt4教程,带你入门Pyqt4 _010

    在PyQt4教程的这部分中,我们讨论拖放操作. 拖放(Drag-and-drop)指的是图形用户界面(Graphical user interface)中,在一个虚拟的对象上按着鼠标键将之拖曳到另一个 ...

  6. Java IO(十九)PrintStream 和 PrintWriter

    Java IO(十九)PrintStream 和 PrintWriter 一.介绍 (一).PrintStream PrintStream 是打印输出流,它继承于FilterOutputStream. ...

  7. upload-labs通关手册

    最近在练习文件上传,所以记录一下自己练习的过程,既能帮助自己以后复习,同时也能帮到初学者. 主要用到的工具是Burpsuite.首先我们应该明白上传文件的目的是什么,通过上传文件将web后门上传并被成 ...

  8. Shellshock漏洞复现

    漏洞分析: exp: curl -A "() { :; }; echo; /bin/cat /etc/passwd" http://172.16.20.134:8080/victi ...

  9. Matlab矩阵学习一 矩阵的创建

    Matlab矩阵创建 1.直接输入数值创建       矩阵元素要用[ ] 括起来,";"代表一行结束,以下创建方式也是合法的,矩阵的元素可以是实数,也可以是复数,复数用a+bi表 ...

  10. 05 . Python入门值循环语句

    一.Python循环语句 程序一般情况下是按照顺序执行的 编程语言提供了各种控制结构,允许更复杂的执行路径 Python中的循环语句有for和while但没有do while 循环语句允许我们执行一个 ...