其他分享
首页 > 其他分享> > 集合:map接口及其实现类(HashMap、TreeMap)

集合:map接口及其实现类(HashMap、TreeMap)

作者:互联网

1、map接口

(1)特点

不能包含重复的键,但是值可以有重复

一个key只能映射一个值

用于保存一组键值对的映射

(2)方法

 

2、Map接口的常用方法

(1)添加

put:

@Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        map.put("7","sun");
        map.put("123",null);
        map.put(null,"qqq");
        System.out.println(map);
    }

(2)删除

remove:

    @Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        System.out.println(map);
        map.remove("4");
        System.out.println(map);
    }
{1=zhao, 2=liu, 4=zhai, 6=ma, 9=zhang}
{1=zhao, 2=liu, 6=ma, 9=zhang}

(3)获取

get:

 @Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        System.out.println(map.get("6"));
    }
ma

containsKey:键是否存在

@Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        System.out.println(map.containsKey("6"));
        System.out.println(map.containsKey("123"));
    }
true
false

containsValue:值是否存在

@Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        System.out.println(map.containsValue("6"));
        System.out.println(map.containsValue("ma"));
    }
false
true

集合大小:

@Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        System.out.println(map.size());
    }
5

是否为空:

@Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        System.out.println(map.isEmpty());
    }
false

通过size方法和isEmpty方法判断集合是否为空的效率是一样的

 

3、Map接口的遍历方式

(1)entrySet方式

@Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        //获取所有的关系
        Set entrys=map.entrySet();
        Iterator iterator=entrys.iterator();//遍历
        while (iterator.hasNext()){//获取每一对关系
            Map.Entry entry= (Map.Entry) iterator.next();
            System.out.println(entry);
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }
1=zhao
1:zhao
2=liu
2:liu
4=zhai
4:zhai
6=ma
6:ma
9=zhang
9:zhang

通过迭代器和增强for是一样的

(2)keySet方式

@Test
    public void test1() {
        Map map=new HashMap();
        map.put("4","zhai");
        map.put("2","liu");
        map.put("9","zhang");
        map.put("1","zhao");
        map.put("6","ma");
        //获取所有的键
        Set keys=map.entrySet();
        for (Object key:keys){
            System.out.println(key);
        }
    }
1=zhao
2=liu
4=zhai
6=ma
9=zhang

以上两种方式不管是获取到所有的关系还是获取到所有的键,都只能用set来存储,因为只有set是不允许有重复的

 

4、HashMap

(1)底层原理:https://www.cnblogs.com/zhai1997/p/12677798.html

(2)源码分析:https://www.cnblogs.com/zhai1997/p/13028001.html

 

5、HashMap与HashTable

底层结构

HashMap:哈希表

HashTable:哈希表

版本

HashMap:1.2

HashTable:1.0

线程安全

HashMap:不安全

HashTable:安全

允许null键null值

HashMap:允许

HashTable:不允许

 

6、TreeMap

(1)特点

基于红黑树

根据键排序(如果键是自定义的类型则要像TreeSet那样手动添加比较器)

 

标签:map,ma,HashMap,zhang,TreeMap,zhao,put
来源: https://www.cnblogs.com/zhai1997/p/13449690.html