Map集合
Map集合是双列集合,是一种一一对应关系,就是映射,Java提供java.util.Map接口供我们使用
Map集合常用子类
我们最常用的是HashMap和LinkedHashMap
- HashMap<k,v>:存储数据采用的哈希表结构,元素存储的顺序不能保证一致,由于要保证键的唯一、不重复,需要重写键的HashCode()方法、equals()方法。
Map接口常用方法
- public V put(K key,V value),把指定的键和值添加到集合中
- public V get(K key)根据键获取对应的值
- public V remove(K key)根据键删除对应的值,返回被删除的元素
- public boolean containsKey(Object key);判断集合中是否包含指定的键
public class Demo {
public static void main(String[] args) {
//创建map对象
HashMap<String, String> map = new HashMap<>();
map.put("吕布","貂蝉");
map.put("孙策","大乔");
map.put("周瑜","小乔");
map.put("刘备","甘夫人");
//如果put事件存在,那么会覆盖之前的值
map.put("刘备","孙尚香");
System.out.println(map);
//访问
String s = map.get("刘备");
String s2 = map.get("吕布");
System.out.println(s);
System.out.println(s2);
//删除
String remove=map.remove("孙策");
System.out.println(remove);
System.out.println(map.containsKey("刘备"));
System.out.println(map.containsKey("孙策"));
}
}
- public Set<K>keySet();获取集合中所有的键,存储到Set集合中
public class Demo2 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("吕布","貂蝉");
map.put("孙策","大乔");
map.put("周瑜","小乔");
map.put("刘备","甘夫人");
//获取所有的键
Set<String> keys = map.keySet();
//遍历所有的键
for (String key:keys) {
String val=map.get(key);
System.out.println(key +"和"+val+"是一对");
}
}
}
Entry键值对对象
Map中存放两种对象,一种是key,一种是value,这一对对象又称做Map中的一个Entry项,Entry将键值对的对应关系封装成了对象,这样遍历Map时,这样我们就可以从每一个键值对(Entry)对象中获取对应的键或者对应的值。
- public Set<Map.Entry<K,V>> entrySet();获取所有的键值对的set集合
- K getKey();获取Entry对象中的键
- V getValue();获取Entry对象中的键
public class Demo3 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("吕布","貂蝉");
map.put("孙策","大乔");
map.put("周瑜","小乔");
map.put("刘备","甘夫人");
//获取所有的entry对象
Set<Map.Entry<String, String>> entrySet = map.entrySet();
//遍历每一个entry对象
for (Map.Entry<String, String> entry :entrySet) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
HashMap存储自定义类型键值
需求:学生对象作为键,家庭住址作为值,存入map集合中
这里学生姓名相同并且年龄相同视为同一个学生。
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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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);
}
}
测试
public class TestStudent {
public static void main(String[] args) {
//创建map
HashMap<Student, String> map = new HashMap<>();
//添加元素
map.put(new Student("刘能",34),"锦州");
map.put(new Student("赵四",20),"葫芦岛");
map.put(new Student("宋小宝",30),"大连");
map.put(new Student("王天来",25),"营口");
//遍历
for (Student key:map.keySet()) {
String val = map.get(key);
System.out.println(key+"...."+val);
}
}
}
image.png
- 当HashMap中存放自定义对象,如果自定义对象作为key存在,要保证对象唯一,必须重写对象的equals方法和hashCode()方法