7

Map集合、Map集合的基本功能、Map集合的获取功能、Map集合的遍历方式(一)(二)

 3 years ago
source link: https://segmentfault.com/a/1190000039825129
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Map集合、Map集合的基本功能、Map集合的获取功能、Map集合的遍历方式(一)(二)

发布于 今天 15:04

一、Map集合

public interface Map<K,V>


创建Map接口的对象

public class HashMap<K,v> implements Map<K,V>

例:注意:不重复,若有两个,则键与后面那个值匹配。

二、Map集合的基本功能

三、Map集合的获取功能

get()注意get()中键不存在,返回nullkeySet()

values()
image.png

四、Map集合的遍历方式(一)

image.png
例:
image.png
遍历:
注意遍历的是键
image.png

五、Map集合的遍历方式(二)

《结婚证》
image.png

HashMap<K,V>中有方法entrySet(),返回Set<Map.Entry<K,V>>
(返回 键值对 它被封装在Set集合中)
public static interface Map.Entry<K,V>

此接口有方法getKey(),getValue()
例:
注意遍历的是键值对。
image.png

image.png
image.png
操作:
1、学生类(略)
2、HashMap<>初始化
image.png
3、遍历一
keySet()+增强for
image.png
注意get()得到的是学生对象,还需要getName、getAge得到实际值
4、遍历二
entrySet()+增强for
image.png
注意getvalue得到的是学生对象,还需要getName、getAge得到实际值

image.png
image.png
操作:
image.png
重写hashCode()、equals()
image.png


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK