# EntrySet

以 Set 集合形式返回 Map 集合中所有键值对

EntrySet: 通过指定的键删除对应的值

public class Demo_6 {
    public static void main(String[]args){
        HashMap<Integer,String> ar = new HashMap<>();
        // 向集合中添加一个键值对
        ar.put(1,"a");
        ar.put(2,"b");
        ar.put(3,"c");
        // 集合对象名调用 EntrySet 获取对象,以 Set 集合形式,返回 Map 集合中所有键值
        Set<Map.Entry<Integer,String>> s = ar.entrySet();
        // 通过 Set 集合的形式对象调用 迭代器
        Iterator<Map.Entry<Integer,String>> a = s.iterator();
        // 判断是否有元素,有则 true 没有则 false
        while(a.hasNext()){
            // 使用 Map.Entry 静态方法获取 Map 中键值对 next 来往下遍历 并赋值给变量 x
            Map.Entry<Integer,String> x = a.next();
            // 获取每次遍历的 Key / 值
            Integer x1 = x.getKey();
            // 通过 Map 集合的 get 方法通过指定的键获取指定的值的方式来判断哪个元素的值为 a
            if(ar.get(x1) == "a"){
                // 满足判断条件后执行 删除
                a.remove();
            }
            // 通过指定的键获取值的方式来打印 键值对
            System.out.println(x1+","+ar.get(x1));
        }
    }
}

# KeySet

以 Set 集合形式返回 Map 集合中所有键

通过 KeySet 指定的键删除对应的值

public class Demo_7 {
    public static void main(String[]args){
        // 创建 Map 集合
        HashMap<Integer,String> ar = new HashMap<>();
        // 向集合中添加一个键值对
        ar.put(1,"a");
        ar.put(2,"b");
        ar.put(3,"c");
        // 以 Set 集合形式,返回 Map 中所有键
        Set s = ar.keySet();
        // 通过 Set 集合形式的对象调用 迭代器
        Iterator s1 = s.iterator();
        // 遍历元素
        while(s1.hasNext()){
            // 强制类型转换 获取 Integer 类型的 键 遍历获取
            Integer key =(Integer) s1.next();
            // 通过 Map 集合的 get 方法 遍历键对应的值是否为 a
            if(ar.get(key) == "a"){
                // 删除
                s1.remove();
            }
            System.out.println(key+","+ar.get(key));
        }
    }
}

# values

以 Collection 集合形式,返回 Map 集合中所有的值

遍历元素

public class Demo_8 {
    public static void main(String[]args){
        HashMap<Integer,String> a = new HashMap<>();
        a.put(1,"a");
        a.put(2,"b");
        a.put(3,"c");
        // 以 Collection 集合形式,返回 Map 集合中所有值
        Collection s = a.values();
        // 通过 Collection 集合形式的对象调用 迭代器
        Iterator s1 = s.iterator();
        // 遍历 集合中 所有值
        while(s1.hasNext()){
            // 将每次获取的值赋值给 String 类型的变量 ar
            String ar =(String) s1.next();
            System.out.println(ar);
        }
    }
}