我是靠谱客的博主 无心小熊猫,这篇文章主要介绍map常用方法,现在分享给大家,希望可以做个参考。

Map接口中的常用方法

Map接口中定义了很多方法,常用的如下:

  • public V put(K key, V value): 把指定的键与指定的值添加到Map集合中。

  • public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。

  • public V get(Object key) 根据指定的键,在Map集合中获取对应的值。

  • boolean containsKey(Object key) 判断集合中是否包含指定的键。

  • public Set<K> keySet(): 获取Map集合中所有的键,存储到Set集合中。

  • public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。

 

Map集合遍历键找值方式

键找值方式:即通过元素中的键,获取键所对应的值

分析步骤:

  1. 获取Map中所有的键,由于键是唯一的,所以返回一个Set集合存储所有的键。方法提示:keyset()

  2. 遍历键的Set集合,得到每一个键。

  3. 根据键,获取键所对应的值。方法提示:get(K key)

代码演示:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MapDemo01 {    public static void main(String[] args) {        //创建Map集合对象        HashMap<String, String> map = new HashMap<String,String>();        //添加元素到集合        map.put("胡歌", "霍建华");        map.put("郭德纲", "于谦");        map.put("薛之谦", "大张伟"); ​        //获取所有的键 获取键集        Set<String> keys = map.keySet();        // 遍历键集 得到 每一个键        for (String key : keys) {         //key 就是键            //获取对应值            String value = map.get(key);            System.out.println(key+"的CP是:"+value);       }     } }

遍历图解:

 

 Entry键值对对象

我们已经知道,Map中存放的是两种对象,一种称为key(键),一种称为value(值),它们在在Map中是一一对应关系,这一对对象又称做Map中的一个Entry(项)Entry将键值对的对应关系封装成了对象。即键值对对象,这样我们在遍历Map集合时,就可以从每一个键值对(Entry)对象中获取对应的键与对应的值。

既然Entry表示了一对键和值,那么也同样提供了获取对应键和对应值得方法:

  • public K getKey():获取Entry对象中的键。

  • public V getValue():获取Entry对象中的值。

在Map集合中也提供了获取所有Entry对象的方法:

  • public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。

Map集合遍历键值对方式

键值对方式:即通过集合中每个键值对(Entry)对象,获取键值对(Entry)对象中的键与值。

操作步骤与图解:

  1. 获取Map集合中,所有的键值对(Entry)对象,以Set集合形式返回。方法提示:entrySet()

  2. 遍历包含键值对(Entry)对象的Set集合,得到每一个键值对(Entry)对象。

  3. 通过键值对(Entry)对象,获取Entry对象中的键与值。 方法提示:getkey() getValue()

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MapDemo02 {    public static void main(String[] args) {        // 创建Map集合对象        HashMap<String, String> map = new HashMap<String,String>();        // 添加元素到集合        map.put("胡歌", "霍建华");        map.put("郭德纲", "于谦");        map.put("薛之谦", "大张伟"); ​        // 获取 所有的 entry对象 entrySet        Set<Entry<String,String>> entrySet = map.entrySet(); ​        // 遍历得到每一个entry对象        for (Entry<String, String> entry : entrySet) {           // 解析            String key = entry.getKey();            String value = entry.getValue();              System.out.println(key+"的CP是:"+value);       }   } }

 

 

tips:Map集合不能直接使用迭代器或者foreach进行遍历。但是转成Set之后就可以使用了。

HashMap存储自定义类型键值

练习:每位学生(姓名,年龄)都有自己的家庭住址。那么,既然有对应关系,则将学生对象和家庭住址存储到map集合中。学生作为键, 家庭住址作为值。

注意,学生姓名相同并且年龄相同视为同一名学生。

编写学生类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Student {    private String name;    private int age; ​    public Student() {   } ​    public Student(String name, int age) {        this.name = name;        this.age = age;   } ​    public String getName() {        return name;   } ​    public void setName(String name) {        this.name = name;   } ​    public int getAge() {        return age;   } ​    public void setAge(int age) {        this.age = age;   } ​    @Override    public boolean equals(Object o) {        if (this == o)            return true;        if (o == null || getClass() != o.getClass())            return false;        Student student = (Student) o;        return age == student.age && Objects.equals(name, student.name);   } ​    @Override    public int hashCode() {        return Objects.hash(name, age);   } }

编写测试类:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class HashMapTest {    public static void main(String[] args) {        //1,创建Hashmap集合对象。        Map<Student,String>map = new HashMap<Student,String>();        //2,添加元素。        map.put(newStudent("lisi",28), "上海");        map.put(newStudent("wangwu",22), "北京");        map.put(newStudent("zhaoliu",24), "成都");        map.put(newStudent("zhouqi",25), "广州");        map.put(newStudent("wangwu",22), "南京");                //3,取出元素。键找值方式        Set<Student>keySet = map.keySet();        for(Student key: keySet){            Stringvalue = map.get(key);            System.out.println(key.toString()+"....."+value);       }   } }
  • 当给HashMap中存放自定义对象时,如果自定义对象作为key存在,这时要保证对象唯一,必须复写对象的hashCode和equals方法(如果忘记,请回顾HashSet存放自定义对象)。

  • 如果要保证map中存放的key和取出的顺序一致,可以使用java.util.LinkedHashMap集合来存放。

LinkedHashMap

我们知道HashMap保证成对元素唯一,并且查询速度很快,可是成对元素存放进去是没有顺序的,那么我们要保证有序,还要速度快怎么办呢?

在HashMap下面有一个子类LinkedHashMap,它是链表和哈希表组合的一个数据存储结构。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class LinkedHashMapDemo {    public static void main(String[] args) {        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();        map.put("邓超", "孙俪");        map.put("李晨", "范冰冰");        map.put("刘德华", "朱丽倩");        Set<Entry<String, String>> entrySet = map.entrySet();        for (Entry<String, String> entry : entrySet) {            System.out.println(entry.getKey() + " " + entry.getValue());       }   } }

结果:

复制代码
1
2
3
邓超 孙俪 李晨 范冰冰 刘德华 朱丽倩

最后

以上就是无心小熊猫最近收集整理的关于map常用方法的全部内容,更多相关map常用方法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(102)

评论列表共有 0 条评论

立即
投稿
返回
顶部