Java集合进阶--双列集合
双列集合的特点:
1 双列集合一次需要存一对数据,分别为键和值
2 键不能重复,值能重复
3 键和值是一一对应的,每一个键只能找到自己对应的值
4 键+值这个整体 我们称之为 “键值对” 或者 “键值对对象” ,在Java中叫做 “Entry对象”
双列集合的结构图:
红色代表接口
蓝色代表实现类
Map
Map中常用的API
- 方法介绍
| 方法名 | 说明 |
| ----------------------------------- | ------------------ |
| V put(K key,V value) | 添加元素 |
| V remove(Object key) | 根据键删除键值对元素 |
| void clear() | 移除所有的键值对元素 |
| boolean containsKey(Object key) | 判断集合是否包含指定的键 |
| boolean containsValue(Object value) | 判断集合是否包含指定的值 |
| boolean isEmpty() | 判断集合是否为空 |
| int size() | 集合的长度,也就是集合中键值对的个数 |
public class MapDemo02 {
public static void main(String[] args) {
//创建集合对象
Map<String,String> map = new HashMap<String,String>();
//V put(K key,V value):添加元素
map.put("张无忌","赵敏");
map.put("郭靖","黄蓉");
map.put("杨过","小龙女");
//V remove(Object key):根据键删除键值对元素
// System.out.println(map.remove("郭靖"));
// System.out.println(map.remove("郭襄"));
//void clear():移除所有的键值对元素
// map.clear();
//boolean containsKey(Object key):判断集合是否包含指定的键
// System.out.println(map.containsKey("郭靖"));
// System.out.println(map.containsKey("郭襄"));
//boolean isEmpty():判断集合是否为空
// System.out.println(map.isEmpty());
//int size():集合的长度,也就是集合中键值对的个数
System.out.println(map.size());
//输出集合对象
System.out.println(map);
}
}
Map集合的基本功能
- 方法介绍
| 方法名 | 说明 |
| -------------------------------- | ------------ |
| V get(Object key) | 根据键获取值 |
| Set<K> keySet() | 获取所有键的集合 |
| Collection<V> values() | 获取所有值的集合 |
| Set<Map.Entry<K,V>> entrySet() | 获取所有键值对对象的集合 |
Map的遍历方式
Map的第一种遍历方式(键找值)
package com.itheima.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class test5 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("杨过","小龙女");
map.put("郭靖","黄蓉");
map.put("杨康","穆念慈");
//通过键找值
//1、获取所有的键,把这些数据放到一个单列集合中
Set<String> keys = map.keySet();
//遍历单列集合,得到每一个键
for (String key : keys) {
//通过键找值
String value = map.get(key);
System.out.println(key + ":" + value);
}
System.out.println("====================================");
keys.forEach( key ->{
String value = map.get(key);
System.out.println(key + ":" + value);
}
);
System.out.println("====================================");
Iterator<String> it = keys.iterator();
while (it.hasNext()){
String key = it.next();
String value = map.get(key);
System.out.println(key + ":" + value);
}
}
}
Map的第二种遍历方式(根据键值对对象)
package com.itheima.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Consumer;
public class test5 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("西凉锦","马超");
map.put("麒麟弓","赵云");
map.put("青釭剑","关羽");
map.put("丈八蛇矛","张飞");
//通过键值对对象进行遍历
Set<Entry<String, String>> entries = map.entrySet();
for (Entry<String, String> entry : entries) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
System.out.println("-----------------");
entries.forEach(s -> System.out.println(s.getKey()+"="+s.getValue()));
System.out.println("-----------------");
Iterator<Entry<String, String>> it = entries.iterator();
while(it.hasNext()){
Entry<String, String> next = it.next();
System.out.println(next.getKey()+"="+next.getValue());
}
}
}
Map的第三种遍历方式(lambda表达式)
package com.itheima.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class test5 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("诸葛亮", "卧龙");
map.put("张飞", "燕人张飞");
map.put("赵云", "赵子龙");
map.put("关羽", "关云长");
map.put("曹操", "曹孟德");
map.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String key, String value) {
System.out.println(key + " " + value);
}
});
System.out.println("-----------");
map.forEach((key, value) -> System.out.println(key + " " + value));
}
}
HashMap
HashMap集合概述和特点
+ HashMap底层是哈希表结构的
+ 依赖hashCode方法和equals方法保证键的唯一
+ 如果键要存储的是自定义对象,需要重写hashCode和equals方法
案例1:存储学生对象并遍历
package com.itheima.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class test5 {
public static void main(String[] args) {
HashMap<Student,String> map = new HashMap();
Student s1 = new Student("张三",18);
Student s2 = new Student("李四",19);
Student s3 = new Student("王五",20);
Student s4 = new Student("王五",20);
map.put(s1, "北京");
map.put(s2, "上海");
map.put(s3, "广州");
map.put(s4, "山东");
Set<Student> keys = map.keySet();
for (Student key : keys) {
System.out.println(key + "..." + map.get(key));
}
System.out.println("=======================================");
Set<Entry<Student, String>> entries = map.entrySet();
for (Entry<Student, String> entry : entries) {
System.out.println(entry.getKey() + "..." + entry.getValue());
}
System.out.println("=======================================");
map.forEach((Student t, String u)->
// TODO Auto-generated method stub
System.out.println(t + "..." + u)
);
}
}
package com.itheima.test;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
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;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}