一、Map的实现:Java中Map的实现有很多种,常用的有HashMap、TreeMap和LinkedHashMap。
- HashMap是一种基于哈希表的实现方式,其中键值对的顺序是无序的。
- TreeMap是一种基于红黑树的实现方式,其中键值对是按照键的自然顺序进行排序的。
- LinkedHashMap是一种基于链表的实现方式,在HashMap的基础上,增加了一个双向链表维护插入顺序。
二、Map 的简单使用
需用具体的实现类去初始化:
Map<String, String> map = new HashMap<String, String>(); // 初始化
Map有如下方法:
- map.put("key1","value1"); //插入元素
- map.get("key1"); //获取元素
- map.size()
- map.remove("key1"); //移除元素
- map.clear(); //清空map
- boolean containsKey(Object key) // 若包含指定key的映射关系,则返回 true
- boolean containsValue(Object value) //
- void putAll(map2) // 将一个已有Map中的数据压入另一个Map中,且去重
- map.values(); //获取集合所有value值
- boolean isEmpty() // 判断Map是否有内容(即new分配空间后是否put键值对),没有返回true。和==null有区别,后者是判断是否new分配了空间,和其中的键值对没有关系
- keySet() //返回值是Map中key值的集合
- entrySet() //返回值也是返回一个Set集合,此集合的类型为Map.Entry
三、Map 遍历
1、方法一 在for-each循环中使用entries来遍历(最常见)(推荐)
这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
注意:如果你遍历的是一个空的map对象,for-each循环将抛出NullPointerException,因此在遍历前你总是应该检查空引用。
2、方法二 在for-each循环中遍历keys或values。
如果只需要map中的键或者值,你可以通过keySet或values来实现遍历,而不是用entrySet。该方法比entrySet遍历在性能上稍好(快了10%),而且代码更加干净。
for (Integer key : map.keySet()) { //遍历map中的键
System.out.println("Key = " + key);
}
for (Integer value : map.values()) { //遍历map中的值
System.out.println("Value = " + value);
}
3、方法三使用Iterator遍历
Map<String, String> map = new HashMap<String, String>();
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
这里的Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry<K,V>。它表示Map中的一个实体(一个key-value对)。接口中有getKey(),getValue方法
四、Map的常见操作
- 添加键值对:可以使用put()方法向Map中添加一个键值对。
示例代码:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
- 获取值:可以使用get()方法获取指定键对应的值。
示例代码:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
int value = map.get("banana");
System.out.println("value: " + value); // 输出 value: 2
- 移除键值对:可以使用remove()方法移除指定键对应的键值对。
示例代码:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
map.remove("banana");
System.out.println("map: " + map); // 输出 map: {apple=1, cherry=3}
- 判断键是否存在:可以使用containsKey()方法判断指定的键是否存在。
示例代码:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
boolean containsKey = map.containsKey("banana");
System.out.println("containsKey: " + containsKey); // 输出 containsKey: true
- Map的遍历
Map的遍历可以使用entrySet()方法获取键值对的集合,然后遍历集合中的每个键值对。
示例代码:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
/* 输出
apple: 1
banana: 2
cherry: 3
*/
此外,Map还提供了keySet()和values()方法,可以分别获取所有的键和值。
示例代码:
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
Set<String> keySet = map.keySet();
System.out.println("keySet: " + keySet); // 输出 keySet: [apple, banana, cherry]
Collection<Integer> values = map.values();
System.out.println("values: " + values); // 输出 values: [1, 2, 3]
总结:Map是一种非常常用的数据结构,在Java中提供了很多种实现方式。可以使用put()、get()、remove()等方法对Map进行操作,也可以使用entrySet()、keySet()、values()等方法进行遍历。
五、案例Map集合储存学生对象并遍历Map基础强化
需求:创建一个Map集合,键是学生对象(Student),值是籍贯(String)。
要求:存储三个键值对元素(Entry),并遍历。
Student类:
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 +
'}';
}
}
Map遍历输出:
public static void main(String[] args){
//若要使用Map,我们需要导入import java.util.HashMap;与import java.util.Map;
//我们使用泛型来约束Map的数据类型,Key为Student数据类型,value为String数据类型
//其具体实现类为HashMap
Map<Student,String>map=new HashMap<>();
//创建Student对象元素
Student student1=new Student("zhangsan",18);
Student student2=new Student("lisi",29);
Student student3=new Student("wangwu",33);
//使用put()方法来添加数据
map.put(student1,"北京");
map .put(student2,"上海");
map .put(student3,"广州");
//调用keySet方法遍历
//在Map遍历中Key占据着主导地位,可以通过Key值找到对应的Value值
//调用keySet()方法,Set<>泛型约束应与Key的数据类型一致
//例如在本代码中,Map<student, string>,Key的数据类型为Student,因此Set<>泛型约束也应当为Student
//Set<Student> set11=map.keySet();代码的意思为将Map中所有Key值存入Set集合(student1, student2, student3
//那么set11即为Key值集合
Set<Student> set11 = map.keySet();
//使用forEach()语句遍历,Student为set11的数据类型,i为set11的复用名(相当于set11)
//那么i就成为了Key值
for(Student i:set11){
//在Map遍历中Key占据着主导地位,可以通过Key值找到对应的Value值
//接下来我们要根据Key值来查找各个Key值对应的Value值
//Value数据类型为String,设置一个string变量来存储Value
//map.get(i);代码意思为根据i(Key值)找到相对应的Value值
String str=map.get(i);
//打印输出
System.out.println("Key的值为:"+i+" "+"Value的值为:"+str);
}
System.out.println("====华丽的分割线====");
//调用entrySet方法遍历
//调用entrySet()方法,Set<>泛型约束应与Map.Entry的数据类型一致,即<student, string>
//<Key,Value>键值对,在Java语言中又被称之为Entry/entry,Map.Entry就相当于Student.name,若name的数据类型为St
//Set<Map.Entry<Student, String>> set22=map.entrySet();代码的意思为将Map中所有(Key,Value)值存入Set集合[
//那么set22即为(Key,value)值集合
Set<Map.Entry<Student,String>> set22 = map.entrySet();
//使用forEach()语句遍历,Integer为set11的数据类型,i为set22的复用名(相当于set22)
//那么i就成为了(Key,Value)值
for(Map.Entry<student,String> i:set22){
//打印输出,直接调用getKey()方法得到Key值,直接调用getvalue()得到Value值
System.out.println("Key的值为:"+i.getKey()+" "+"Value的值为:"+i.getvalue());
}
}