zoukankan      html  css  js  c++  java
  • HashMap的方法及功能、StringBuffer的方法

    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);
    }
    

    }

  • 相关阅读:
    BZOJ 4245: [ONTAK2015]OR-XOR
    BZOJ 2535: [Noi2010]Plane 航空管制2
    COGS 2551. 新型武器
    cogs2550. 冰桥,升起来了!
    大数模板
    uva 1513(线段树)
    uva 11525(线段树)
    poj 3368(RMQ模板)
    hdu 4686 Arc of Dream(矩阵快速幂)
    poj 3321 Apple Tree(树状数组)
  • 原文地址:https://www.cnblogs.com/miniSimple/p/12274564.html
Copyright © 2011-2022 走看看