一、Map集合
1.1、认识Map集合
Collection系列集合是单列集合;Map系列集合为双列集合。
使用场景:需要存储一一对应的数据时,就可以考虑使用Map集合来做。
package com.itheima.d4_map;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* 目标:掌握Map集合的特点。
*/
public class MapTest1 {
public static void main(String[] args) {
// Map<String, Integer> map = new HashMap<>(); // 一行经典代码。 按照键 无序,不重复,无索引。
Map<String, Integer> map = new LinkedHashMap<>(); // 有序,不重复,无索引。
map.put("手表", 100);
map.put("手表", 220); // 后面重复的数据会覆盖前面的数据(键)
map.put("手机", 2);
map.put("Java", 2);
map.put(null, null);
System.out.println(map);//HashMap:{null=null, 手表=220, Java=2, 手机=2};LinkedHashMap:{手表=220, 手机=2, Java=2, null=null}
Map<Integer, String> map1 = new TreeMap<>(); // 可排序,不重复,无索引
map1.put(23, "Java");
map1.put(23, "MySQL");
map1.put(19, "李四");
map1.put(20, "王五");
System.out.println(map1);//{19=李四, 20=王五, 23=MySQL}
}
}
1.2、Map集合的常用方法
package com.itheima.d4_map;
/**
* 目标:掌握Map集合的常用方法
*/
import java.util.*;
/**
* 目标:掌握Map集合的常用方法(重点)
*/
public class MapTest2 {
public static void main(String[] args) {
// 1.添加元素: 无序,不重复,无索引。
Map<String, Integer> map = new HashMap<>();
map.put("手表", 100);
map.put("手表", 220);
map.put("手机", 2);
map.put("Java", 2);
map.put(null, null);
System.out.println(map);
// map = {null=null, 手表=220, Java=2, 手机=2}
// 2.public int size():获取集合的大小
System.out.println(map.size());
// 3、public void clear():清空集合
//map.clear();
//System.out.println(map);
// 4.public boolean isEmpty(): 判断集合是否为空,为空返回true ,反之!
System.out.println(map.isEmpty());
// 5.public V get(Object key):根据键获取对应值
int v1 = map.get("手表");
System.out.println(v1);
System.out.println(map.get("手机")); // 2
System.out.println(map.get("张三")); // null
// 6. public V remove(Object key):根据键删除整个元素(删除键会返回键的值)
System.out.println(map.remove("手表"));
System.out.println(map);
// 7.public boolean containsKey(Object key): 判断是否包含某个键 ,包含返回true ,反之
System.out.println(map.containsKey("手表")); // false
System.out.println(map.containsKey("手机")); // true
System.out.println(map.containsKey("java")); // false
System.out.println(map.containsKey("Java")); // true
// 8.public boolean containsValue(Object value): 判断是否包含某个值。
System.out.println(map.containsValue(2)); // true
System.out.println(map.containsValue("2")); // false
// 9.public Set<K> keySet(): 获取Map集合的全部键。
Set<String> keys = map.keySet();
System.out.println(keys);
// 10.public Collection<V> values(); 获取Map集合的全部值。
Collection<Integer> values = map.values();
System.out.println(values);
// 11.把其他Map集合的数据倒入到自己集合中来。(拓展)
Map<String, Integer> map1 = new HashMap<>();
map1.put("java1", 10);
map1.put("java2", 20);
Map<String, Integer> map2 = new HashMap<>();
map2.put("java3", 10);
map2.put("java2", 222);
map1.putAll(map2); // putAll:把map2集合中的元素全部倒入一份到map1集合中去。
System.out.println(map1);
System.out.println(map2);
}
}
1.3、Map集合的遍历方式
1.3.1、健找值
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 目标:掌握Map集合的遍历方式1:键找值
*/
public class MapTest1 {
public static void main(String[] args) {
// 准备一个Map集合。
Map<String, Double> map = new HashMap<>();
map.put("蜘蛛精", 162.5);
map.put("蜘蛛精", 169.8);
map.put("紫霞", 165.8);
map.put("至尊宝", 169.5);
map.put("牛魔王", 183.6);
System.out.println(map);
// map = {蜘蛛精=169.8, 牛魔王=183.6, 至尊宝=169.5, 紫霞=165.8}
// 1、获取Map集合的全部键
Set<String> keys = map.keySet();
// System.out.println(keys);
// [蜘蛛精, 牛魔王, 至尊宝, 紫霞]
// key
// 2、遍历全部的键,根据键获取其对应的值
for (String key : keys) {
// 根据键获取对应的值
double value = map.get(key);
System.out.println(key + "=====>" + value);
}
}
}
1.3.2、键值对
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 目标:掌握Map集合的第二种遍历方式:键值对。
*/
public class MapTest2 {
public static void main(String[] args) {
Map<String, Double> map = new HashMap<>();
map.put("蜘蛛精", 169.8);
map.put("紫霞", 165.8);
map.put("至尊宝", 169.5);
map.put("牛魔王", 183.6);
System.out.println(map);// {蜘蛛精=169.8, 牛魔王=183.6, 至尊宝=169.5, 紫霞=165.8}
// 1、调用Map集合提供entrySet方法,把Map集合转换成键值对类型的Set集合
// entries = [(蜘蛛精=169.8), (牛魔王=183.6), (至尊宝=169.5), (紫霞=165.8)]
// entry
Set<Map.Entry<String, Double>> entries = map.entrySet();
for (Map.Entry<String, Double> entry : entries) {
String key = entry.getKey();
double value = entry.getValue();
System.out.println(key + "---->" + value);
}
}
}
1.3.3、Lambda表达式
package com.itheima.d5_map_traverse;
import java.util.HashMap;
import java.util.Map;
public class MapTest3 {
public static void main(String[] args) {
Map<String, Double> map = new HashMap<>();
map.put("蜘蛛精", 169.8);
map.put("紫霞", 165.8);
map.put("至尊宝", 169.5);
map.put("牛魔王", 183.6);
System.out.println(map);
map.forEach((k,v)->
System.out.println(k + "---->" + v)
);
}
}
二、HashMap集合
2.1、HashMap集合的底层原理
(利用键计算哈希值,跟值无关)
import java.util.HashMap;
import java.util.Map;
/**
* 目标:掌握Map集合下的实现类:HashMap集合的底层原理。
*/
public class Test1HashMap {
public static void main(String[] args) {
Map<Student, String> map = new HashMap<>();
map.put(new Student("蜘蛛精", 25, 168.5), "盘丝洞");
map.put(new Student("蜘蛛精", 25, 168.5), "水帘洞");
map.put(new Student("至尊宝", 23, 163.5), "水帘洞");
map.put(new Student("牛魔王", 28, 183.5), "牛头山");
System.out.println(map);//Student类中没有重写hashCode()和equals()方法,所以集合中有4个键值对
//{Student{name='至尊宝', age=23, height=163.5}=水帘洞, Student{name='蜘蛛精', age=25, height=168.5}=水帘洞, Student{name='蜘蛛精', age=25, height=168.5}=盘丝洞, Student{name='牛魔王', age=28, height=183.5}=牛头山}
}
}
三、LinkedHashMap集合
3.1、LinkedHashMap集合的原理
import java.util.LinkedHashMap;
public class Test2LinkedHashMap {
public static void main(String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>(); // 按照键 有序,不重复,无索引。
map.put("手表", 100);
map.put("手表", 220);
map.put("手机", 2);
map.put("Java", 2);
map.put(null, null);
System.out.println(map);//{手表=220, 手机=2, Java=2, null=null}
}
}
四、TreeMap集合
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class Test3TreeMap {
public static void main(String[] args) {
Map<Student, String> map = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return Double.compare(o2.getHeight(), o1.getHeight());//体重降序
}
});
//上面的方法可以简化为Lambda表达式如下
//Map<Student,String> map =new TreeMap<>(( o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()));
//Map<Student,String> map =new TreeMap<>(); //要在Student类中重写compareTo()方法
map.put(new Student("蜘蛛精", 25, 168.5), "盘丝洞");
map.put(new Student("蜘蛛精", 25, 168.5), "水帘洞");
map.put(new Student("至尊宝", 23, 163.5), "水帘洞");
map.put(new Student("牛魔王", 28, 183.5), "牛头山");
System.out.println(map);
}
}
五、集合的嵌套
是指集合中的元素又是一个集合
import java.util.*;
/**
* 目标:理解集合的嵌套。
* 江苏省 = "南京市","扬州市","苏州市“,"无锡市","常州市"
* 湖北省 = "武汉市","孝感市","十堰市","宜昌市","鄂州市"
* 河北省 = "石家庄市","唐山市", "邢台市", "保定市", "张家口市"
*/
public class Test4QianTao {
public static void main(String[] args) {
Map<String , List<String>> map=new HashMap<>();
List<String> cities1=new ArrayList<>();
Collections.addAll(cities1,"南京市","扬州市","苏州市" ,"无锡市","常州市");
map.put("江苏省",cities1);
List<String> cities2 = new ArrayList<>();
Collections.addAll(cities2, "武汉市","孝感市","十堰市","宜昌市","鄂州市");
map.put("湖北省", cities2);
List<String> cities3 = new ArrayList<>();
Collections.addAll(cities3, "石家庄市","唐山市", "邢台市", "保定市", "张家口市");
map.put("河北省", cities3);
System.out.println(map);
List<String> cities=map.get("湖北省");
for (String city : cities) {
System.out.println(city);
}
map.forEach((p,c)->{
System.out.println(p+"--------->"+c);
});
}
}