Map 接口
1.1 概述
Interface Map<K,V>
: K表示值得数据类型,V表示值得数据类型- 键不能重复,值可以重复
- 键和值一一对应得,每一个值只能找到自己对应得值
- (键 + 值)这个整体我们称之为“键值对”或者“键值对对象”,在Java中叫做“Entry对象”
public class MapDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("1","张三");
map.put("2","李四");
map.put("3","王五");
System.out.println(map);
}
}
1.2 基本方法
- V put(K key,V value):添加元素
- V remove(Objectkey):根据键删除键值对元素
- void clear():移除所有的键值对元素
- boolean containsKey(Object key)判断集合是否包含指定的键
- boolean containsValue(Object value)判断集合是否包含指定的值
- boolean isEmpty()判断集合是否为空
- int size()集合的长度,也就是集合中键值对的个数
Set<K> keySet()
获取所有键的集合- V get(Object Key) 根据键获取值
public class MapDemo2 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("1","张三");
map.put("2","李四");
map.put("3","王五");
map.put("4","刘六");
System.out.println(map);
map.remove("3");
System.out.println(map);
System.out.println(map.containsKey("1"));
System.out.println(map.containsKey("6"));
System.out.println(map.containsValue("小王"));
System.out.println(map.containsValue("小黄"));
System.out.println(map.isEmpty());
System.out.println(map.size());
map.clear();
System.out.println(map);
}
}
public class MapDemo3 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("1","张三");
map.put("2","李四");
map.put("3","王五");
map.put("4","刘六");
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(map.get(key));
}
}
}
1.3 遍历Map集合
- Set<Map.Entry<K,V> > entrySet() 获取所有键值对对象的集合
- K getKey() 获得键
- V getValue() 获得值
public class MapDemo4 {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("1","张三");
map.put("2","李四");
map.put("3","王五");
map.put("4","刘六");
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey() + "-" + entry.getValue());
}
}
}
1.4 实现类
1.4.1 HashMap
1.4.1.1 特点
- HashMap跟HashSet一样底层是哈希表结构的
- 依赖hashCode方法和equals方法保证键的唯一
- 如果键要存储的是自定义对象,需要重写hashCode的equals方法
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class MapDemo5 {
public static void main(String[] args) {
HashMap<Student,String> hm = new HashMap<>();
Student s1 = new Student("小河",20);
Student s2 = new Student("林格",22);
Student s3 = new Student("小刘",20);
hm.put(s1,"湖南");
hm.put(s2,"湖北");
hm.put(s3,"江苏");
Set<Student> students = hm.keySet();
for (Student student : students) {
System.out.println(hm.get(student));
}
Set<Map.Entry<Student, String>> entries = hm.entrySet();
for (Map.Entry<Student, String> entry : entries) {
System.out.println(entry.getKey() + "-----"+ entry.getValue());
}
hm.forEach((Student s,String str)->{
System.out.println(s+ "-----"+ str);
});
}
}
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;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
}
1.4.2 TreeMap
1.4.2.1 特点
- 底层是红黑树结构
- 依赖自然排序或者比较器排序,对键进行排序
- 如果键存储的是自定义对象,需要实现Comparable接口或者在创建TreeMap的时候给出比较器排序规则
public class Student implements Comparable<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;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
@Override
public int compareTo(Student o) {
int result = this.getAge() - o.getAge();
result = result == 0 ? this.getName().compareTo(o.getName()) : result;
return result;
}
}
public class MapDemo6 {
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<>();
Student s1 = new Student("张三",20);
Student s2 = new Student("李四",25);
Student s3 = new Student("王五",25);
tm.put(s1,"福建");
tm.put(s2,"河北");
tm.put(s3,"四川");
tm.forEach((Student s,String str)->{
System.out.println(s+"---->"+str);
});
}
}