java进阶 04

Map

1.Java提供了专门的集合类用来存放对象关系的对象(映射),即 java.util.Map 接 口。比如:IP地址与主机名,身份证号与个人,系统用户名与系统用户对象等。
在这里插入图片描述
2.注:
(1)Collection 中的集合,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储。
(2)Map 中的集合,元素是成对存在的(理解为夫妻)。每个元素由键与值两部分组成,通过键可以找对所对应的值。
(3)Collection 中的集合称为单列集合, Map 中的集合称为双列集合
(4) Map 中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。

Map常用子类

1.HashMap:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。由于要保证键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。
2.LinkedHashMap:HashMap下有个子类LinkedHashMap,存储数据采用的哈希表结构+链表结构。通过链表结构可以保证元素的存取顺序一致;通过哈希表结构可以保证的键的唯一、不重复,需要重写键的 hashCode()方法、equals()方法。

Map接口中的常用方法

public V put(K key, V value) : 把指定的键与指定的值添加到Map集合中。
public V remove(Object key) : 把指定的键所对应的键值对元素 在Map集合中删除,返回被删除元素的
public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
public Set<K> keySet() : 获取Map集合中所有的键,存储到Set集合中。
public Set<Map.Entry<K,V>> entrySet() : 获取到Map集合中所有的键值对对象的集合(Set集合)。
代码演示;

import java.util.HashMap;

public class MapDemo {
    public static void main(String[] args) {
//创建 map对象
        HashMap<String, String> map = new HashMap<String, String>();
//添加元素到集合
        map.put("黄晓明", "杨颖");
        map.put("文章", "马伊琍");
        map.put("邓超", "孙俪");
        System.out.println(map);
//String remove(String key)
        System.out.println(map.remove("邓超"));
        System.out.println(map);
// 想要查看 黄晓明的媳妇 是谁
        System.out.println(map.get("黄晓明"));
        System.out.println(map.get("邓超"));
    }
}

输出结果:
在这里插入图片描述

Map集合遍历键找值方式

1.键找值方式:通过元素中的键,获取键所对应的值。
(1)获取Map中所有的键,由于键是唯一的,所以返回一个Set集合存储所有的键。方法提示: keyset()
(2)遍历键的Set集合,得到每一个键。
(3)根据键,获取键所对应的值。方法提示: get(K key)
代码演示:

import java.util.HashMap;
import java.util.Set;

public class MapDemo01 {
    public static void main(String[] args) {
//创建Map集合对象
        HashMap<String, String> map = new HashMap<String,String>();
//添加元素
        map.put("我", "你");
        map.put("你", "他");
        map.put("他", "我");
//获取所有的键 
        Set<String> keys = map.keySet();
// 遍历键集得到每一个键
        for (String key : keys) {
//key 就是键
//获取对应值
            String value = map.get(key);
            System.out.println(key+"的CP是:"+value);
        }
    }
}

输出结果:
在这里插入图片描述

Entry键值对对象

1.Map 中存放的是两种对象,一种称为key(键),一种称为value(值),它们在在 Map 中是一一对应关 系,这一对对象又称做 Map 中的一个 Entry(项)
2. 方法:
public K getKey() :获取Entry对象中的键。
public V getValue() :获取Entry对象中的值。
3.在Map集合中也提供了获取所有Entry对象的方法:
public Set<Map.Entry<K,V>> entrySet() : 获取到Map集合中所有的键值对对象的集合(Set集合)。
代码演示:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo02 {
    public static void main(String[] args) {
// 创建Map集合对象
        HashMap<String, String> map = new HashMap<String,String>();
// 添加元素到集合
        map.put("我", "我们");
        map.put("你", "你们");
        map.put("他", "他们");
// 获取 所有的 entry对象 entrySet
        Set<Map.Entry<String,String>> entrySet = map.entrySet();
// 遍历得到每一个entry对象
        for (Map.Entry<String, String> entry : entrySet) {
// 解析
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"的CP是:"+value);
        }
    }
}

输出结果:
在这里插入图片描述

HashMap存储自定义类型键值

/*
    HashMap存储自定义类型键值
    Map集合保证key是唯一的:
        作为key的元素,必须重写hashCode方法和equals方法,以保证key唯一
 */
public class Demo01HashMapSavePerson {
    public static void main(String[] args) {
        show02();
    }

    /*
        HashMap存储自定义类型键值
        key:Person类型
            Person类就必须重写hashCode方法和equals方法,以保证key唯一
        value:String类型
            可以重复
     */
    private static void show02() {
        //创建HashMap集合
        HashMap<Person,String> map = new HashMap<>();
        //往集合中添加元素
        map.put(new Person("女王",18),"英国");
        map.put(new Person("秦始皇",18),"秦国");
        map.put(new Person("普京",30),"俄罗斯");
        map.put(new Person("女王",18),"毛里求斯");
        //使用entrySet和增强for遍历Map集合
        Set<Map.Entry<Person, String>> set = map.entrySet();
        for (Map.Entry<Person, String> entry : set) {
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"-->"+value);
        }
    }
/*
        HashMap存储自定义类型键值
        key:String类型
            String类重写hashCode方法和equals方法,可以保证key唯一
        value:Person类型
            value可以重复(同名同年龄的人视为同一个)
     */
    private static void show01() {
        //创建HashMap集合
        HashMap<String,Person> map = new HashMap<>();
        //往集合中添加元素
        map.put("北京",new Person("张三",18));
        map.put("上海",new Person("李四",19));
        map.put("广州",new Person("王五",20));
        map.put("北京",new Person("赵六",18));
        //使用keySet加增强for遍历Map集合
        Set<String> set = map.keySet();
        for (String key : set) {
            Person value = map.get(key);
            System.out.println(key+"-->"+value);
        }
    }
}

LinkedHashMap

1.在HashMap下面有一个子类LinkedHashMap,它是链表和哈希表组合的一个数据存储结构。保证成对元素存放进去有序的。

public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("邓超", "孙俪");
map.put("李晨", "范冰冰");
map.put("刘德华", "朱丽倩");
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}

输出结果:
邓超 孙俪
李晨 范冰冰
刘德华 朱丽倩

JDK9对集合添加的优化

1.Java 9,添加了几种集合工厂方法,更方便创建少量元素的集合、map实例。新的List、Set、Map的静态工厂方法可 以更方便地创建集合的不可变实例。
例子;

public class HelloJDK9 {
public static void main(String[] args) {
Set<String> str1=Set.of("a","b","c");
//str1.add("c");这里编译的时候不会错,但是执行的时候会报错,因为是不可变的集合
System.out.println(str1);
Map<String,Integer> str2=Map.of("a",1,"b",2);
System.out.println(str2);
List<String> str3=List.of("a","b");
System.out.println(str3);
}
}

注:
(1):of()方法只是Map,List,Set这三个接口的静态方法,其父类接口和子类实现并没有这类方法,比如 HashSet,ArrayList等;
(2):返回的集合是不可变的;

Debug追踪

1.使用IDEA的断点调试功能,查看程序的运行过程。

猜你喜欢

转载自blog.csdn.net/Ulrica_Amaris/article/details/107756122