【map】Java笔记——Map集合

 

Map集合接口

Map集合与Collection不是从属关系,是平级的

  1. Map集合的映射特点

    • 一个映射不能包含重复的键,由此键只能允许有一个空null
    • 每个键最多只能和一个值对应
    • 值可以重复,由此值允许多个是空null
    • Map集合与Collection的子接口Set一样是无序的
  2. Map<KEY,VALUE>:KEY和VALUE表示泛型,KEY和VALUE可以是任何数据类型【除基本数据类型之外】,实际项目中KEY一般是String类型

实现类

HashMap

特点
  • 一个映射不能包含重复的键:

    如果存在重复的KEY,则后面添加的会把前面的覆盖

  • 其实键就是Set,元素唯一,只能有一个null,

  • 元素是无序的(与添加顺序无关)

  • HashMap不是同步的,即线程不安全

如何将HashMap变成线程安全?

 
 
 
 
 
 
 
 
1
Collections.synchronizedMap(<HashMap集合>);
 
 
常用方法
 
 
 
x
 
 
 
 
1
public static void main(String[] args) {
2
        Map<String, String> maps = new HashMap<>();
3
        /*1.添加*/
4
        //加入单条键值对
5
        maps.put("智多星","吴用");
6
        maps.put("豹子头","林冲");
7
        maps.put("玉麒麟","卢俊义");
8
        Map<String, String> map1 = new HashMap<>();
9
        map1.put("小诸葛","富安");
10
        map1.put("及时雨","宋江");
11
        //加入一个集合:将一个集合复制到另一个集合中
12
        maps.putAll(map1);
13
        System.out.println("map集合:"+maps);
14
        System.out.println("map1集合:"+map1);
15
        /*2.清空*/
16
        map1.clear();
17
        System.out.println("map1是否为空:"+map1.isEmpty());  //isEmpty()检查集合是否为空  返回布尔值
18
        /*3.获取*/
19
        //获取VALUE,通过KEY获取VALUE
20
        System.out.println("获取智多星的值:"+maps.get("智多星"));
21
        //获取全部的KEY,返回一个Set集合
22
        Set<String> KEYSET = maps.keySet();
23
        System.out.println("获取KEY的Set集合:"+KEYSET); //输出Map集合中的KEY
24
    /*4.遍历集合*/
25
        //遍历集合:通过遍历Set集合的KEYSET,来获取集合的值VALUE
26
        System.out.println("===============\n通过for()增强遍历HashMap集合");
27
        for (String str : KEYSET) {
28
            String value = maps.get(str);
29
            System.out.println(str+"——>"+value);
30
        }
31
        //遍历集合:通过entrySet(),返回 Set<Map.Entry<K,V>>,一个Set集合
32
        System.out.println("===============\n通过entrySet()遍历HashMap集合");
33
        Set<Map.Entry<String, String>> entries = maps.entrySet();
34
        for (Map.Entry<String, String> entry : entries) {
35
            //获取KEY
36
            String key = entry.getKey();
37
            //获取VALUE
38
            String value = entry.getValue();
39
            System.out.println(key+"——>"+value);
40
        }
41
    /*5.判断功能*/
42
        //是否包含指定的键KEY,返回布尔值
43
        System.out.println("maps集合中是否包含”玉麒麟“这个KEY:"+(maps.containsKey("玉麒麟")?"是":"否"));
44
    
45
        //是否包含指定的值VALUE,返回布尔值
46
        System.out.println("maps集合中是否包含”宋江“这个VALUE:"+(maps.containsValue("宋江")?"是":"否"));
47
    
48
        //检查集合是否为空null
49
        System.out.println("maps集合是否为空:"+(maps.isEmpty()?"是":"否"));
50
        //HashMap不能同步,即线程不安全
51
        /*6.如何线程安全?*/
52
        Collections.synchronizedMap(maps);
53
        Collections.synchronizedMap(map1);
54
55
56
    }
 
 

 

 

LinkedHashMap

  • 特点

    • 允许多个null 值 和 一个 null 键
    • LinkedHashMap有顺序(添加顺序有关)与HashMap的区别
    • LinkedHashMap线程不安全
  • 代码

 
 
 
xxxxxxxxxx
23
 
 
 
 
1
 public static void main(String[] args) {
2
        /*LinkedHashMap*/
3
        Map<String, String> maps = new LinkedHashMap<>();
4
        maps.put("智多星","吴用");
5
        maps.put("豹子头","林冲");
6
        maps.put("玉麒麟","卢俊义");
7
        //LinkedHashMap根据元素添加顺序进行排序
8
        System.out.println(maps);
9
        
10
        /*HashMap*/
11
        Map<String, String> hashmap = new HashMap<String, String>();
12
        hashmap.put("智多星","吴用");
13
        hashmap.put("豹子头","林冲");
14
        hashmap.put("玉麒麟","卢俊义");
15
        //排序与添加顺序无关
16
        System.out.println(hashmap);
17
        //综上比较LinkedHashMap和HashMap之间的区别即元素是否有序
18
    }
19
/*
20
LinkedHashMap-->{智多星=吴用, 豹子头=林冲, 玉麒麟=卢俊义}
21
HashMap-->{玉麒麟=卢俊义, 智多星=吴用, 豹子头=林冲}
22
23
*/
 
 

 

TreeMap

  • 很少用到,需要自定义比较器

  • TreeMap可以参考TreeSet,TreeMap可以支持Map的排序

  • TreeMap根据键KEY的自然顺序进行排序,或者根据创建映射【键和值相对应】时提供的Comparator进行排序,具体取决于使用的构造方法。

  • 特点

    • 可以按照KEY来排序
    • KEY不能为null,KEY不能重复,VALUE值可以有多个null
    • 线程不安全
代码
封装类
 
 
 
xxxxxxxxxx
50
 
 
 
 
1
/*
2
Student实现Comparable接口,则TreeMap可以根据KEY来排序
3
*/
4
public class Student implements Comparable<Student> {
5
    private String name ;
6
    private int age;
7
8
    public Student() {
9
    }
10
11
    public Student(String name, int age) {
12
        this.name = name;
13
        this.age = age;
14
    }
15
16
    public String getName() {
17
        return name;
18
    }
19
20
    public void setName(String name) {
21
        this.name = name;
22
    }
23
24
    public int getAge() {
25
        return age;
26
    }
27
28
    public void setAge(int age) {
29
        this.age = age;
30
    }
31
32
    /**
33
     *
34
     * @param o
35
     * @return
36
     * 自定义比较器:根据年龄来比较
37
     */
38
    @Override
39
    public int compareTo(Student o) {
40
        return this.age - o.age;
41
    }
42
43
    @Override
44
    public String toString() {
45
        return "Student{" +
46
                "name='" + name + '\'' +
47
                ", age=" + age +
48
                '}';
49
    }
50
}
 
 
测试类
 
 
 
xxxxxxxxxx
22
 
 
 
 
1
public class HashMapDemo02  {
2
    public static void main(String[] args) {
3
        Map<Student, String> maps = new TreeMap<>();
4
        /*1.添加*/
5
        //加入单条键值对
6
        maps.put(new Student("智多星",30),"吴用");
7
        maps.put(new Student("豹子头",30),"林冲");
8
        maps.put(new Student("玉麒麟",30),"卢俊义");
9
        maps.put(new Student("小诸葛",25),"富安");
10
        
11
        System.out.println(maps);
12
    }
13
}
14
/*答案
15
16
//因为以年龄age为排序依据,所以系统误认为第一条到第二条是同一条数据
17
//在Student中是设置根据年龄进行排序,TreeMap集合是根据KEY进行排序
18
{Student{name='小诸葛', age=25}=富安, 
19
Student{name='智多星', age=30}=林冲, 
20
Student{name='玉麒麟', age=33}=卢俊义}
21
22
*/
 
 

 

Hahstable

  • 不常用

  • 特点

    1. HashTable是map的实现类
    2. 不允许键 和 值为null,会报“空指针异常”
    3. HashTable中的元素没有顺序(与添加的顺序无关)
    4. HashTable是线程安全的:查看源码,用了
posted @ 2019-10-15 09:22  叶荒  阅读(203)  评论(0编辑  收藏  举报