JAVA学习线路:day08集合[3]
所有的文档和源代码都开源在GitHub: https://github.com/kun213/JavaNotes上了。希望我们可以一起加油,一起学习,一起交流。
day08【集合】
今日学习内容-2020.10.5
@
目录
Map集合
1、说出Map集合特点
发现Map
接口下的集合与Collection
接口下的集合,它们存储数据的形式不同,如下图。
Collection
中的集合,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储。Map
中的集合,元素是成对存在的(理解为夫妻)。每个元素由键与值两部分组成,通过键可以找对所对应的值。Collection
中的集合称为单列集合,Map
中的集合称为双列集合。- 需要注意的是,
Map
中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。
2、使用Map集合添加方法保存数据
示例代码:
/**
* V put(K,V)将键值对,存储在集合中
* 返回作为值的对象
* put方法的返回值,一般情况下都返回null
* 当向集合中存储了重复键的时候,将原有的值覆盖
* put方法,返回被覆盖之前的值
*/
public static void method(){
//创建Map集合,使用实现类HashMap集合
//键是字符串类型,值是整数类型
Map<String,Integer> map = new HashMap<String, Integer>();
//Map集合方法put,存储键值对
map.put("a",1);
map.put("b",2);
Integer i = map.put("b",5);
map.put("c",3);
System.out.println(map);
System.out.println(i);
}
}
3、使用”键找值”的方式遍历Map集合
通过元素中的键,获取键所对应的值
分析步骤:
- 获取Map中所有的键,由于键是唯一的,所以返回一个Set集合存储所有的键。方法提示:
keyset()
- 遍历键的Set集合,得到每一个键。
- 根据键,获取键所对应的值。方法提示:
get(K key)
遍历图解:
示例代码:
public class MapDemo02 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("java",10000);
map.put("c",12000);
map.put("python",8000);
//1: Map集合方法keySet(),获取到存储所有键的Set集合
Set<String> set = map.keySet();
// 2: 遍历Set集合,取出了集合中的所有的键
Iterator<String> it = set.iterator();
while (it.hasNext()){
String key = it.next();
// 3: Map集合方法get()获取值
Integer value = map.get(key);
System.out.println(key+"::"+value);
}
System.out.println("增强for循环--------------------");
for (String key: set){
Integer value = map.get(key);
System.out.println(key+"::"+value);
}
}
4、使用”键值对”的方式遍历Map集合
即通过集合中每个键值对(Entry)对象,获取键值对(Entry)对象中的键与值。
Entry键值对对象:
我们已经知道,Map
中存放的是两种对象,一种称为key(键),一种称为value(值),它们在在Map
中是一一对应关系,这一对对象又称做Map
中的一个Entry(项)
。Entry
将键值对的对应关系封装成了对象。即键值对对象,这样我们在遍历Map
集合时,就可以从每一个键值对(Entry
)对象中获取对应的键与对应的值。
在Map集合中也提供了获取所有Entry对象的方法:
public Set<Map.Entry<K,V>> entrySet()
: 获取到Map集合中所有的键值对对象的集合(Set集合)。
获取了Entry对象 , 表示获取了一对键和值,那么同样Entry中 , 分别提供了获取键和获取值的方法:
public K getKey()
:获取Entry对象中的键。public V getValue()
:获取Entry对象中的值。
操作步骤与图解:
- 获取Map集合中,所有的键值对(Entry)对象,以Set集合形式返回。方法提示:
entrySet()
。 - 遍历包含键值对(Entry)对象的Set集合,得到每一个键值对(Entry)对象。
- 通过键值对(Entry)对象,获取Entry对象中的键与值。 方法提示:
getkey() getValue()
遍历图解:
示例代码:
public class MapDemo03 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String, Integer>();
map.put("java",10000);
map.put("c",11000);
map.put("php",8000);
map.put("python",9000);
// 1: Map集合的方法 entrySet()
Set<Map.Entry<String,Integer>> set = map.entrySet();
System.out.println("迭代器---------------");
//2: 遍历Set集合
Iterator<Map.Entry<String,Integer>> it = set.iterator();
while (it.hasNext()){
// 3: 对应关系对象,获取键和值
// set集合中,存储的元素是Entry对象, it.next()取出的也是Entry对象
Map.Entry<String,Integer> entry = it.next();
//Entry对象的方法 getKey() getValue()
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"::"+value);
}
System.out.println("增强for循环----------------");
for (Map.Entry<String,Integer> entry : set){
String key = entry.getKey();
int value = entry.getValue();
System.out.println(key+"::"+value);
}
}
5、能够使用HashMap存储自定义键值对的数据
Map接口实现类HashMap类特点:
- 底层哈希表结构。
- 不允许重复键。
- 用作键的对象,应该重写hashCode()方法和equals()方法。
- 此集合可以使用null值和null键。
- 线程不安全,运行速度快。
练习:每位学生(姓名,年龄)都有自己的家庭住址。那么,既然有对应关系,则将学生对象和家庭住址存储到map集合中。学生作为键, 家庭住址作为值。
注意,学生姓名相同并且年龄相同视为同一名学生。
编写学生类:
public class Student {
private String name;
private int age;
//构造方法
//get/set
@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 static void main(String[] args) {
//1,创建Hashmap集合对象。
Map<Student,String> map = new HashMap<Student,String>();
//2,添加元素。
map.put(new Student("lisi",28), "上海");
map.put(new Student("wangwu",22), "北京");
map.put(new Student("wangwu",22), "南京");
//3,取出元素。键找值方式
Set<Student> keySet = map.keySet();
for(Student key: keySet){
String value = map.get(key);
System.out.println(key.toString()+"....."+value);
}
}
- 当给HashMap中存放自定义对象时,如果自定义对象作为key存在,这时要保证对象唯一,必须复写对象的hashCode和equals方法(如果忘记,请回顾HashSet存放自定义对象)。
- 如果要保证map中存放的key和取出的顺序一致,可以使用
java.util.LinkedHashMap
集合来存放。