【Java HashMap】常用函数的使用
生活随笔
收集整理的這篇文章主要介紹了
【Java HashMap】常用函数的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
常用函數(shù)
代碼示例
package cn.hanquan.test;import java.util.HashMap; import java.util.Map;//HashMap的使用 public class HashMapTest {public static void main(String[] args){/*-------------------m1------------------*/Map<Integer, String> m1 = new HashMap<>();m1.put(1, "one");// 存放鍵值對:將1和one對應(yīng)m1.put(2, "two");m1.put(3, "three");System.out.println(m1);System.out.println(m1.get(1));// 通過鍵對象查找得到值對象System.out.println(m1.size());System.out.println(m1.isEmpty());System.out.println(m1.containsKey(2));System.out.println(m1.containsValue("five"));/*-------------------m2-------------------*/Map<Integer, String> m2 = new HashMap<>();m2.put(4, "四");// 存放鍵值對:將1和one對應(yīng)m2.put(5, "五");m2.put(6, "六");System.out.println(m1);m1.putAll(m2);// 把m2合并到m1上System.out.println(m1);} }運行結(jié)果
{1=one, 2=two, 3=three}
one
3
false
true
false
{1=one, 2=two, 3=three}
{1=one, 2=two, 3=three, 4=四, 5=五, 6=六}
總結(jié)
以上是生活随笔為你收集整理的【Java HashMap】常用函数的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java数据结构】链表的增、删、返回指
- 下一篇: 【Java版】将汉字转化为gb2312编