上一篇文章我们了解了单列集合List、Set集合,今天就让我们了解双列集合Map集合。
Map集合
1.Map集合的特点
- Map是一个双列集合
- Map内部采用“键值对”的形式存储
- 在一个Map内,“键”是唯一的;
- 键和值可以是任何对象;
- 可以通过“键”获取对应的“值”
2.Map集合的常用方法
添加/修改:
Public V put (K key,V value):把指定的键与指定的值添加到Map集合中。
返回值:当存储一个已经存在的键时,用新值替换旧值,并将旧值返回
删除:
Public V remove(Object key):把指定的键所对应的键值对元素在Map集合中删除,返回删除的元素的值
查询:
Public V get(Object key)根据指定的键,在Map集合中获取对应的值
Public int Size():获取键值对的长度
遍历:(Map本身没有遍历的方法,都是间接遍历)
Public Set keySet():获取Map集合中所有的键,存储到Set集合中
Public SetMap.Entry << K,V>> entrySet():获取到Map集合中所有的键值对对象的集合(Set集合)
例:
public class demo {
public static void main(String[] args) {
Map< String, String > map = new HashMap<>();
System.out.println(map.put(“haha”, “哈哈”));
System.out.println(map.put(“hehe”, “呵呵”));
System.out.println(map.put(“heihei”, “嘿嘿”));
System.out.println(map.put(“oo”, “哦哦”));
System.out.println(map.put("oo", "换啦"));
System.out.println(map);
System.out.println("==========");
System.out.println("删除oo:" + map.remove("oo"));
System.out.println(map);
System.out.println("==========");
System.out.println("删除不存在:" + map.remove("不存在"));
System.out.println(map);
System.out.println("==========");
System.out.println("获取hehe:" + map.get("hehe"));
System.out.println("获取不存在:" + map.get("不存在"));
}
注意:使用put方法时,若指定的键(key)在集合中没有,则没有这个键对应的值,返回null,并把指定的键值添加到 集合中;
若指定的键(key)在集合中存在,则返回值为集合中键对应的值(该值为替换前的值),并把指定键所对应的 值,替换成指定的新值。
**
3.“键找值”方式遍历
- 获取Map中所有的键,由于键的唯一,所以返回一个Set集合存储所有的键。(keySet():获取Entry对象中的键)
- 遍历键的Set集合,得到每个键
- 根据键,获取键所对应的值。(get(K key))
例:
public static void main(String[] args) {
HashMap< String,String > map = new HashMap<>();
//添加元素到集合中
map.put(“老大”,”111”);
map.put(“老二”,”222”);
map.put(“老三”,”333”);
//获取所有的键 获取键集
Set<String> keys = map.keySet();
//遍历
for (String key : keys){
//key 键
//获取对应 的 值
String value = map.get(key);
System.out.println(key + "的cp是:" + value);
}
}
4.“键值对”的方式遍历
- 获取Map集合中,所有的键值对(Entry)对象,以Set集合形式返回。(entry Set()获取到Map集合中所有的键值对对象的集合(Set集合)
- 遍历包含键值对(Entry)对象的Set集合,得到每个键值对(Entry) 对象
通过键值对(Entry)对象,获取Entry对象中的键与值。(getkey() getValue())
例:
public static void main(String[] args) {
HashMap< String,String> map = new HashMap<>();
//添加元素到集合中
map.put(“老大”,”111”);
map.put(“老二”,”222”);
map.put(“老三”,”333”);//获取所有 entry对象 entrySet
Set< Map.Entry< String , String>> entrySet = map.entrySet();
//遍历
for (Map.Entry< String,String> entry : entrySet){String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + "的cp是:" + value);
}
}
**
能够使用HashMap存储自定义键值对的数据
- 当给HashMap中存放自定义对象时,如果自定义对象作为key存在,这时要保证对象唯一,必须复写对象的 hashCode和equals方法
- 如果要保证map中存放的key和取出的顺序一致,可以使用 java.util.LinkedHashMap 集合来存放。
例:
public class Student {
private String name;
private int age;
…….//省略get/set 和构造方法
@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);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
测试类:
public class demo {
public static void main(String[] args) {
Map< Student,String> map = new HashMap<>();
map.put(new Student(“gu”,22),”哈哈”);
map.put(new Student(“hu”,23),”呵呵”);
map.put(new Student(“hu”,23),”替换”);
System.out.println(map.size());
System.out.println(map);
}
}