Java中的Map集合

1 Map

1.1 特点

  • 用于储存元素对(成为键值对:key-value),每个键映射到每个值
  • 键不能重复,键唯一

1.2 实现类

  • HashMap
  • TreeMap

1.3 方法

  • equals(Object o) 比较指定对象与此 Map 的等价性
  • hashCode() 返回此 Map 的哈希码
  • clear() 从 Map 中删除所有映射
  • remove(Object key) 从 Map 中删除键和关联的值
  • put(Object key, Object value) 将指定值与指定键相关联
  • clear() 从 Map 中删除所有映射
  • putAll(Map t) 将指定 Map 中的所有映射复制到此 map
  • get(Object key) 返回与指定键关联的值
  • containsKey(Object key) 如果 Map 包含指定键的映射,则返回 true
  • containsValue(Object value) 如果此 Map 将一个或多个键映射到指定值,则返回 true
  • isEmpty() 如果 Map 不包含键-值映射,则返回 true
  • size() 返回 Map 中的键-值映射的数目

1.4 HashMap

  • 基于哈希表,底层结构由数组实现
  • key-value的形式储存,key唯一
  • 允许存入null键,null值(null值只有一个,并存于数组第一个位置)
  • 无序集合
  • 随着元素的增加而动态扩容(与ArrayList原理一致)
  • 不存在重复元素(得益于hashCode算法和equals方法)
package learn;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class hmap {
    public static void main(String[] args) {
        //创建HashMap集合
        Map<String,String> map = new HashMap<String, String>();
        System.out.println("HashMap的初始容量:"+ map.size());

        //添加元素
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");
        map.put("4","d");
        map.put("5","e");
        map.put("6","f");
        map.put(null,null);

        //遍历1:获取key的Set集合
        for(String key:map.keySet()){
            System.out.println("HashMap的key为:"+key);
            System.out.println("HashMap的key对于的value值为:"+map.get(key));
        }

        删除元素
        map.remove(null);
        map.remove("2");

        //遍历2:得到set集合器
        Set<Map.Entry<String,String>> s1 = map.entrySet();
        Iterator<Map.Entry<String,String>> iterator = s1.iterator();
        while (iterator.hasNext()){
            Map.Entry<String,String> mapEntry = iterator.next();
            System.out.println("key为:"+mapEntry.getKey());
            System.out.println("对应的value值为:"+mapEntry.getValue());
        }

        //替换元素
        map.put("1","x");

        //遍历3:转换成Set集合,增强for循环
        Set<Map.Entry<String,String>> s2 = map.entrySet();
        for(Map.Entry<String,String> mapEntry : s2){
            System.out.println("map的key是:" + mapEntry.getKey());
            System.out.println("map的value是:" + mapEntry.getValue());
        }

        //返回与指定键关联的值
        System.out.println("返回与指定键关联的值:"+map.get("3"));

        //如果 Map 包含指定键的映射,则返回 true
        System.out.println(map.containsKey("3"));

        //如果此 Map 将一个或多个键映射到指定值,则返回 true
        System.out.println(map.containsValue(null));

        //判空
        System.out.println(map.isEmpty());

        //比较
        boolean x =map.get("1").equals("x");
        System.out.println(x);

        //容量大小
        System.out.println(map.size());

        //将指定 Map 中的所有映射复制到此 map
        Map<String,String> map1 = new HashMap<String, String>();
        map1.putAll(map);
        for(String S:map1.keySet()){
            System.out.println(S);
            System.out.println(map1.get(S));
        }

        //清空map
        map.clear();
        System.out.println(map.isEmpty());
    }
}

输出:

HashMap的初始容量:0
HashMap的key为:null
HashMap的key对于的value值为:null
HashMap的key为:1
HashMap的key对于的value值为:a
HashMap的key为:2
HashMap的key对于的value值为:b
HashMap的key为:3
HashMap的key对于的value值为:c
HashMap的key为:4
HashMap的key对于的value值为:d
HashMap的key为:5
HashMap的key对于的value值为:e
HashMap的key为:6
HashMap的key对于的value值为:f
key为:1
对应的value值为:a
key为:3
对应的value值为:c
key为:4
对应的value值为:d
key为:5
对应的value值为:e
key为:6
对应的value值为:f
map的key是:1
map的value是:x
map的key是:3
map的value是:c
map的key是:4
map的value是:d
map的key是:5
map的value是:e
map的key是:6
map的value是:f
返回与指定键关联的值:c
true
false
false
true
5
1
x
3
c
4
d
5
e
6
f
true

1.5 TreeMap

  • 不允许出现重复的key;
  • 可以插入null键,null值;
  • 可以对元素进行排序;
  • 无序集合(插入和遍历顺序不一致);
package learn;

import java.util.*;

public class tmap {
    public static void main(String[] args) {
        //创建TreeMap对象:
        TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
        System.out.println("初始化后,TreeMap元素个数为:" + treeMap.size());

        //新增元素:
        treeMap.put(1, "a");
        treeMap.put(2, "a");
        treeMap.put(4, "c");
        treeMap.put(7, "a");
        treeMap.put(8, "f");
        treeMap.put(6, "a");
        treeMap.put(9, "g");
        treeMap.put(3, "a");
        treeMap.put(12, "f");
        treeMap.put(10, "a");
        System.out.println("添加元素后,TreeMap元素个数为:" + treeMap.size());

        //判断集合的key中是否包含"6"
        System.out.println("集合是否包含key为5:" + treeMap.containsKey(5));


        //遍历元素:
        Set<Map.Entry<Integer, String>> entrySet = treeMap.entrySet();
        for (Map.Entry<Integer, String> entry : entrySet) {
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println("TreeMap元素的key:" + key + ",value:" + value);
        }

        //获取所有的key:
        Set<Integer> keySet = treeMap.keySet();
        for (Integer strKey : keySet) {
            System.out.println("TreeMap集合中的key:" + strKey);
        }

        //获取所有的value:
        Collection<String> valueList = treeMap.values();
        for (String intValue : valueList) {
            System.out.println("TreeMap集合中的value:" + intValue);
        }

        //获取元素:
        System.out.println("key为5对应的value:" + treeMap.get(5));

        //获取集合内第一个元素
        System.out.println("第一个元素key:" + treeMap.firstKey());

        //获取集合内最后一个元素
        System.out.println("最后一个元素key:" + treeMap.lastKey());
        System.out.println("最后一组元素:" + treeMap.lastEntry());

        //获取集合内的key小于"6"的key
        System.out.println("key小于6的元素:" + treeMap.lowerKey(6));

        //获取集合内的key大于等于"6"的key
        System.out.println("key大于等于5的元素:" + treeMap.ceilingKey(5));

        //获取集合的key从"3"到"10"的元素
        SortedMap<Integer, String> sortedMap = treeMap.subMap(3, 10);
        for(Integer integer:sortedMap.keySet()){
            System.out.println(sortedMap.get(integer));
        }

        //删除元素:
        treeMap.remove(6);

        //清空集合元素:
        treeMap.clear();

        //判断集合是否为空
        System.out.println("集合是否为空:" + treeMap.isEmpty());

        }
    }

输出:

初始化后,TreeMap元素个数为:0
添加元素后,TreeMap元素个数为:10
集合是否包含key为5:false
TreeMap元素的key:1,value:a
TreeMap元素的key:2,value:a
TreeMap元素的key:3,value:a
TreeMap元素的key:4,value:c
TreeMap元素的key:6,value:a
TreeMap元素的key:7,value:a
TreeMap元素的key:8,value:f
TreeMap元素的key:9,value:g
TreeMap元素的key:10,value:a
TreeMap元素的key:12,value:f
TreeMap集合中的key:1
TreeMap集合中的key:2
TreeMap集合中的key:3
TreeMap集合中的key:4
TreeMap集合中的key:6
TreeMap集合中的key:7
TreeMap集合中的key:8
TreeMap集合中的key:9
TreeMap集合中的key:10
TreeMap集合中的key:12
TreeMap集合中的value:a
TreeMap集合中的value:a
TreeMap集合中的value:a
TreeMap集合中的value:c
TreeMap集合中的value:a
TreeMap集合中的value:a
TreeMap集合中的value:f
TreeMap集合中的value:g
TreeMap集合中的value:a
TreeMap集合中的value:f
key为5对应的value:null
第一个元素key:1
最后一个元素key:12
最后一组元素:12=f
key小于6的元素:4
key大于等于5的元素:6
a
c
a
a
f
g
集合是否为空:true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值