Map集合
Map
- 特点:存储键值对映射关系,根据key可以找到value
HashMap
- 采用Hashtable哈希表存储结构
- 优点:添加速度快,查询速度快,删除速度快
- 缺点:key无序
LinkedHashMap
- 采用哈希表存储结构,同时使用链表维护次序
- key有序(添加顺序)
package com.bjsxt.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* 使用Map存储国家简称-国家名称映射
* <p>
* HashMap底层结构是哈希表
* key:无序,唯一,HashSet
* value:无序,不唯一
* LinkedHashMap:哈希表+链表
* key:有序(添加顺序) LinkedHashSet
* value:无序,不唯一 Collection
* TreeMap 红黑树
* key:有序(自然顺序) 唯一 TreeSet
* value:无序,不唯一 Collection
* <p>
* Set和Map的关系:如果Map中只存储key,不存储value,就是对应的Set
* Set就是对应Map的key的集合
*/
public class TestMap1 {
public static void main(String[] args) {
// 创建一个Map对象
Map<String, String> map = new HashMap<>();
// 使用Map对象存储键值对
map.put("cn", "China");
map.put("jp", "Japan");
map.put("us", "The United States");
map.put("uk", "America");
map.put("uk", "The United Kingdom");
map.put("en", "The United Kingdom");
System.out.println(map);
// 根据键获取值
System.out.println(map.size());
System.out.println(map.get("cn"));
System.out.println(map.get("fr"));
System.out.println("------");
// 遍历Map
// 方式1. 得到所有的key组成的Set
System.out.println("------遍历方式一------");
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key + "--->" + map.get(key));
}
System.out.println("------遍历方式二------");
// 方式2。得到所有Entry(就是一个哈希表或者红黑树中节点类)组成的集合
Set<Map.Entry<String, String>> entrySet = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
运行结果
package com.bjsxt.map;
public class Student {
private int id;
private String name;
private int age;
private Double score;
public Student() {
}
public Student(int id, String name, int age, Double score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
package com.bjsxt.map;
import java.util.HashMap;
import java.util.Map;
public class TestMap2 {
public static void main(String[] args) {
// 创建一个Map对象
Map<Integer, Student> map = new HashMap<>();
// 存入多个学号-学生映射
Student student1 = new Student(10, "zhangsan", 23, 98.0);
Student student2 = new Student(20, "lisi", 24, 100.0);
Student student3 = new Student(30, "wangwu", 20, 85.0);
Student student4 = new Student(10, "zhangsan", 23, 98.0);
map.put(student1.getId(), student1);
map.put(student2.getId(), student2);
map.put(student3.getId(), student3);
map.put(student4.getId(), student4);
System.out.println(map.size());
System.out.println(map);
}
}
运行结果
- 番外
- 哪些集合可以使用Iterator遍历?
实现Iterator接口的集合类都可以使用迭代器遍历(如Collection、List、Set可以,Map不可以) - for-each循环和Iterator的联系?
- for-each循环遍历集合时,底层使用的是Iterator
- 凡是可以使用for-each循环遍历的集合,肯定也可以使用Iterator遍历
- for-each循环和Iterator的区别?
- for-each还能遍历数组,Iterator只能遍历集合
- 使用for-each遍历集合时不能删除元素,会抛出异常ConcurrentModificationException,使用Iterator遍历集合时能删除元素
- 哪些集合可以使用Iterator遍历?