Map集合
- java.util.Map双列集合的顶层接口,用来存储具备映射关系对象的集合接口定义
单列集合: 以单个单个元素进行存储数据
双列集合: 以键值对的形式进行存储数据
- 特点:
- Map<K,V>,K用来限制键的类型,V用来限制值的类型
- Map集合以键值对的形式来存储数据
- Map集合的键是唯一的,值可以重复,但键如果重复,值就会覆盖
- Map集合是根据键来找值
实现类_
- 实现类都有的特点:
键是唯一的,值可以重复,但键如果重复,值就会覆盖
HashMap: 键值对存取顺序不一致底层哈希表结构,由哈希表保证键唯一(键值唯一,存取无序)
**LinkedHashMap:**键值对存取顺序一致底层哈希表+链表结构,由哈希表保证键唯一,由链表保证存取顺序一致(键值唯一,存取一致)
TreeMap: 可以对键进行排序,从而实现键值对排序底层红黑树结构,由红黑树保证键唯一,由比较器对象对元素进行排序 (键值唯一,可以排序)
Map的常用方法
public V put(K key, V value)
: 把指定的键与指定的值添加到Map集合中public V remove(Object key)
: 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值public V get(Object key)
:根据指定的键,在Map集合中获取对应的值public boolean containsKey(Object key)
:判断该集合中是否有此键public boolean containsValue(Object value):
判断该集合中是否有此值public Set<K> keySet()
: 获取Map集合中所有的键,存储到Set集合中public Set<Map.Entry<K,V>> entrySet()
: 获取到Map集合中所有的 键值对对象 的集合(Set集合)
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Text8 {
public static void main(String[] args) {
// 创建Map集合,限制类型为String ,Integer
Map<String,Integer> hMap = new HashMap<>();
//hMap.put(k key,v value) 把指定的键与指定的值添加到Map集合中
hMap.put("张三",18);
hMap.put("李四",28);
hMap.put("老王",38);
hMap.put("王五",48);
hMap.put("张三",18);
// 输出 hMap = {李四=28, 张三=18, 老王=38, 王五=48}
System.out.println("hMap = " + hMap);
// hMap.remove(Object key) 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值
hMap.remove("老王");
System.out.println("hMap = " + hMap); // 输出 hMap = {李四=28, 张三=18, 王五=48}
// hMap.get(Object key)` 根据指定的键,在Map集合中获取对应的值
System.out.println(hMap.get("王五")); // 输出 48
// hMap.containsKey(Object key)判断该集合中是否有此键
System.out.println(hMap.containsKey("张三"));// 输出 true
// hMap.containsKey(Object value)判断该集合中是否有此值
System.out.println(hMap.containsValue("50"));// 输出 false
// hMap.keySet()获取Map集合中所有的键,存储到Set集合中
Set set = hMap.keySet();
System.out.println("set = " + set); // 输出 set = [李四, 张三, 王五]
// hMap.entrySet()获取到Map集合中所有的 键值对对象 的集合(Set集合)
Set set2 = hMap.entrySet();
System.out.println("set2 = " + set2);// 输出 set2 = [李四=28, 张三=18, 王五=48]
}
}
Map的遍历
- 获取Map集合的所有键—>keySet()方法
- 循环遍历所有的键
- 根据键找值—>get(K k)方法
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Text9 {
public static void main(String[] args) {
Map<String,Integer> hMap = new HashMap<>();
//添加键值对
hMap.put("张三",18);
hMap.put("李四",28);
hMap.put("老王",38);
hMap.put("王五",48);
hMap.put("老六",18);
Set<String> setMap = hMap.keySet();
for (String key : setMap) {
// 通过键值得到对应的 value值
System.out.println(key +"="+ hMap.get(key));
// 输出:
//李四=28
//张三=18
//老六=18
//老王=38
//王五=48
}
}
}
- 方式二:
通过键值对对象方式
- 获取所有的键值对对象---->entrySet()方法
- 循环遍历所有的键值对对象
- 使用键值对对象获取键和值—>使用Entry接口的方法
Entry<K,V>接口:
- Entry接口是Map接口的成员内部接口,使用的方式是Map.Entry<K,V>
- Entry表示键值对对象,也就是说Entry是用来封装键值对的
Entry接口里面的常用方法有:
- K getKey(); 获取键值对对象封装的键
- V getValue(); 获取键值对对象封装的值
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Text9 {
public static void main(String[] args) {
Map<String, Integer> hMap = new HashMap<>();
//添加键值对
hMap.put("张三", 18);
hMap.put("李四", 28);
hMap.put("老王", 38);
hMap.put("王五", 48);
hMap.put("老六", 18);
// 获取所有的键值对对象
Set<Map.Entry<String, Integer>> entrySet = hMap.entrySet();
// 遍历所有的键值对对象
for (Map.Entry<String, Integer> entry : entrySet) {
// 使用键值对 对象获取键和值 -- 使用Entry接口的方法
System.out.println(entry.getKey() + "=" + entry.getValue());
}
// 输出:
//李四=28
//张三=18
//老六=18
//老王=38
//王五=48
}
}
HashMap存储自定义类型_
- 如果键是自定义类型的元素,要保证键唯一,那么该键所属的类需要重写hashCode和equals方法
import java.util.HashMap;
import java.util.Objects;
import java.util.Set;
class Student5 {
String name;
int age;
public Student5() {
}
public Student5(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student5 student5 = (Student5) o;
return age == student5.age &&
Objects.equals(name, student5.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student5{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Text10 {
public static void main(String[] args) {
HashMap<Student5, String> hMap = new HashMap<>();
// 添加对象到集合中
hMap.put(new Student5("张三", 18), "上海");
hMap.put(new Student5("李四", 28), "北京");
hMap.put(new Student5("赵六", 38), "广州");
hMap.put(new Student5("老六", 48), "武汉");
hMap.put(new Student5("张三", 18), "深圳");
// 获取hMap中的键值并放到Set集合中
Set<Student5> keys = hMap.keySet();
for (Student5 key : keys) {
// 用获取到的键值调用get方法,获取值
System.out.println(key + "=" + hMap.get(key));
//输出:
//Student5{name='赵六', age=38}=广州
//Student5{name='张三', age=18}=深圳
//Student5{name='李四', age=28}=北京
//Student5{name='老六', age=48}=武汉
}
}
}
LinkedHashMap
class Student5{...}
public class Text10 {
public static void main(String[] args) {
LinkedHashMap<Student5, String> hMap = new LinkedHashMap<>();
// 添加对象到集合中
hMap.put(new Student5("张三", 18), "上海");
hMap.put(new Student5("李四", 28), "北京");
hMap.put(new Student5("赵六", 38), "广州");
hMap.put(new Student5("老六", 48), "武汉");
hMap.put(new Student5("张三", 18), "深圳");
// 获取hMap中的键值并放到Set集合中
Set<Student5> keys = hMap.keySet();
for (Student5 key : keys) {
// 用获取到的键值调用get方法,获取值
System.out.println(key + "=" + hMap.get(key));
//输出:LinkedHashMap 存取顺序一致,键唯一
//Student5{name='张三', age=18}=深圳
//Student5{name='李四', age=28}=北京
//Student5{name='赵六', age=38}=广州
//Student5{name='老六', age=48}=武汉
}
}
}
TreeMap
class Student5{...}
public class Text10 {
public static void main(String[] args) {
TreeMap<Student5, String> hMap = new TreeMap<>(new Comparator<Student5>() {
@Override
public int compare(Student5 o1, Student5 o2) {
// 使用Student5 对象里的age值做为降序排序的条件
return o2.age - o1.age;
}
});
// 添加对象到集合中
hMap.put(new Student5("张三", 18), "上海");
hMap.put(new Student5("李四", 28), "北京");
hMap.put(new Student5("赵六", 38), "广州");
hMap.put(new Student5("老六", 48), "武汉");
hMap.put(new Student5("张三", 18), "深圳");
// 获取hMap中的键值并放到Set集合中
Set<Student5> keys = hMap.keySet();
for (Student5 key : keys) {
// 用获取到的键值调用get方法,获取值
System.out.println(key + "=" + hMap.get(key));
//输出:TreeMap 可以排序,键唯一
//Student5{name='老六', age=48}=武汉
//Student5{name='赵六', age=38}=广州
//Student5{name='李四', age=28}=北京
//Student5{name='张三', age=18}=深圳
}
}
}