2021-03-17:java第19天:Map集合介绍及使用,JDK9 of 方法对集合优化,debug程序调试

1.Map集合

2.Map接口的常用方法

3.Map集合遍历找值

package Part2.Day_19.demo01;

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

/*
    Map集合的第一种遍历方式,通过键找值的方式
    Map集合中的方法:
        Set<K>  keySet() 返回此映射中包含的键的Set视图
    实现步骤:
    1.KeySet()
    2.遍历Set集合,获取Map集合中的每一个key
    3.通过Map集合中的方法get(key),通过key找到value
    */
public class demo02keyset {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("赵丽颖",168);
        map.put("杨颖",165);
        map.put("林志玲",178);

        Set<String> set = map.keySet();

        Iterator<String> it =set.iterator();
        while(it.hasNext()){
            String key = it.next();
            Integer value = map.get(key);
            System.out.println(key+"="+value);
        }

        for (String key : set) {
            Integer value = map.get(key);
            System.out.println(key+"="+value);
        }
    }
}

package Part2.Day_19.demo01;

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

/*
    Map集合的第二种遍历方式:
    使用Entry对象, 内部类
    Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的Set视图
    步骤:
    1. entrySet() 取Map集合中的多个Entry对象,存到set里
    2.遍历Set集合,获取每一个Entry对象
    3.使用Entry里的getKey()和getValue()
 */
public class demo03EntrySet {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("赵丽颖",168);
        map.put("杨颖",165);
        map.put("林志玲",178);

        Set<Map.Entry<String ,Integer>> set = map.entrySet();

        Iterator<Map.Entry<String, Integer>> it = set.iterator();
        while(it.hasNext()){
            Map.Entry<String, Integer> entry = it.next();
            String key = entry.getKey();
            Integer value = entry.getValue();
        }
        for (Map.Entry<String, Integer> entry : set) {
            String key = entry.getKey();
            Integer value = entry.getValue();
        }
    }
}

HashMap存储自定义类型键值

Map集合保证key唯一:

key的元素必须重写hashCode和equals方法

package Part2.Day_19.demo02;

import java.util.Objects;

public class Person {
    private String name;
    private int age;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", 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;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}




package Part2.Day_19.demo02;

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

public class demo01hashmapsavePerson {
    public static void main(String[] args) {
        show01();
        show02();
    }
    private static void show01(){
        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));

        Set<String> set = map.keySet();
        for (String key : set) {
            Person value = map.get(key);//String重写了hashcode和equals
            System.out.println(key+"="+value);
        }
    }
    private static void show02(){
        HashMap<Person,String> map= new HashMap<>();
        map.put(new Person("女王",18),"英国");
        map.put(new Person("秦始皇",18),"秦国");
        map.put(new Person("普京",30),"俄国");
        Set<Map.Entry<Person,String>> set = map.entrySet();
        for (Map.Entry<Person, String> entry : set) {
            Person key = entry.getKey();
            String value = entry.getValue();//需要Person类里重写Hashcode和equals方法
            System.out.println(key+"="+value);
        }
    }
}


LinkedHashMap:

底层:哈希表+链表(记录元素顺序)

HashTable集合:

1.键和值都不能为空 null

2.最早期的双列集合

3.线程安全,单线程,速度慢

package Part2.Day_19.demo03;

import java.util.HashMap;
import java.util.Hashtable;

/*
java.util.Hashtable<K,V> implements Map<K,V> 接口
Hashtable:底层也是一个哈希表,是一个线程安全的集合,单线程集合,速度慢
HashMap:底层是哈希表,线程不安全的集合,多线程,速度快

HashMap(之前学的所有集合),可以存储null值,null键
Hashtable集合不能存储null值null键

Hashtable和Vector集合一样,在jdk1.2版本之后被更先进的集合取代。HashMap,ArrayList

Hashtable子类Properties依然非常活跃
Properties集合是一个唯一和IO流结合的集合
 */
public class demo02Hashtable {
    public static void main(String[] args) {
        HashMap<String,String> map= new HashMap<>();
        map.put(null,"a");
        map.put(null,null);
        //允许存储空值空键

        Hashtable<String,String> table = new Hashtable<>();
        table.put("b",null);//空指针异常
    }
}

练习(略)

4. JDK9对集合添加的优化

package Part2.Day_19.demo04;

import java.util.List;
import java.util.Set;

/*
    JDK9新特性:
    list接口,set接口,Map接口,里面增加了静态方法of,可以给集合一次性添加多个元素
    static <E> List<E> of (E...elements)
    使用前提:
        当集合中存储的个数已经确定了,不再改变,才可以使用

    注意:
    1.of方法只适用于 list,set,map,不适用于接口的实现类
    2.of方法的返回值是一个不能改变的集合,不能add,put不然会抛出异常
    3.Set接口和map接口调用of方法的时候不能有重复的元素,否则抛出异常
 */
public class demo01JDK9 {
    public static void main(String[] args) {
        List<String> list = List.of("a","b","c","d");
        System.out.println(list);

        Set<String> set = Set.of("a","b","c");//不能有重复元素
    }
}


5. Debug追踪

添加断点

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值