java -- Map映射接口

Map
是一个映射接口,Map没有继承Collection接口,Map提供key到value的映射。一个Map中不能包含相同的key,每个key只能映射一个value。

Map中常用方法

contianskey
contiansvalue
isEmpty
remove
size
clear

public static void fun2() {
    Map<String, Integer> map  = new HashMap<>();
    Integer num1 = map.put("张三", 18);
    Integer num2 = map.put("李四", 19);
    Integer num3 = map.put("王五", 16);
    Integer num4 = map.put("赵六", 15);

    //  containsKey
    System.out.println(map.containsKey("张三"));
    //  containsvalue
    System.out.println(map.containsValue(19));
    //  isEmpty
    System.out.println(map.isEmpty());
    //  remove
    System.out.println(map.remove("张三"));
    //  size
    System.out.println(map.size());
    //  clear
    map.clear();
    System.out.println(map);
}

/**
 * map 特点1.数据的保存 是以键值对形式保存   2.键是唯一的(不能重复)
 * HashSet 和 HashMap 之间的关系
 *  HashSet 的底层  实际上是 HashMap实现的(换句话说:HashSet 依赖于 HashMap)
 * 在向 HashSet中添加的 值 相当于 往 Map中添加到key的位置
 */
public static void fun1() {
    Map<String, Integer> map = new HashMap<>();
    Integer put1 = map.put("张三", 18);
    Integer put2 = map.put("李四", 14);
    Integer put3 = map.put("王五", 45);
    Integer put4 = map.put("赵六", 12);
    Integer put5 = map.put("张三", 18);
    System.out.println(put1);
    System.out.println(put2);
    System.out.println(put3);
    System.out.println(put4);
    System.out.println(put5);

    System.out.println(map);
}

Map中的遍历方法

/**
 * foreach方法遍历
 * 使用entrySet方法
 */
public static void fun7() {
    HashMap<String, Integer> hMap = new HashMap<>();
    hMap.put("松松", 11);
    hMap.put("凉凉", 23);
    hMap.put("涵涵", 33);
    hMap.put("宁宁", 24);

    Set<Entry<String,Integer>> entrySet = hMap.entrySet();
    for (Entry<String, Integer> entry : entrySet) {
        Integer value = entry.getValue();
        String key = entry.getKey();
        System.out.println(key + " = " + value);
    }
}

/**
 * 使用entrySet()方法
 * 该方法返回的是一个集合 集合中存储的是一个一个键值对对象
 * entry对象中 保存 key 和 value
 * entry对象的  key 和 value  可以用getKey/getValue取出
 * 
 */
public static void fun6() {
    HashMap<String, Integer> hMap = new HashMap<>();
    hMap.put("张三", 18);
    hMap.put("李四", 12);
    hMap.put("王五", 16);
    hMap.put("赵六", 15);

    Set<Entry<String,Integer>> entrySet = hMap.entrySet();
    Iterator<Entry<String, Integer>> iterator = entrySet.iterator();
    while (iterator.hasNext()) {
        Entry<String, Integer> next = iterator.next();
        String key = next.getKey();
        Integer value = next.getValue();
        System.out.println(key + " = " + value);

    }
}

/**
 * 使用foreach 方法遍历
 */
public static void fun5() {
    HashMap<String, Integer> hMap = new HashMap<>();
    hMap.put("张三", 18);
    hMap.put("李四", 12);
    hMap.put("王五", 16);
    hMap.put("赵六", 15);

    Set<String> keySet = hMap.keySet();
    for (String key : keySet) {
        System.out.println(key + " = " + hMap.get(key));
    }
}

/**
 * Map中是没有迭代器的  不能直接迭代
 * 使用 keySet 取出Map中的键 -- 返回的是Set 集合 用Set集合的迭代器间接迭代
 *     get(key) 将所有value取出  
 */
public static void fun4() {
    HashMap<String, Integer> hMap = new HashMap<>();
    hMap.put("张三", 18);
    hMap.put("李四", 12);
    hMap.put("王五", 16);
    hMap.put("赵六", 15);

    Set<String> keySet = hMap.keySet();
    Iterator<String> iterator = keySet.iterator();
    while (iterator.hasNext()) {
        String key = iterator.next();
        Integer integer = hMap.get(key);
        System.out.println(key + " = " + integer);
    }
}

HashMap去重类对象

/**
 * 去重类对象的时候 要重写hashCode() 和 equals()
 * 在最后打印的时候,只打印了舟山的张三
 * 这是因为舟山的张三覆盖了北京的张三
 */
public static void fun3() {
    HashMap<Stu, String> hMap = new HashMap<>();
    hMap.put(new Stu("张三", 18), "北京");
    hMap.put(new Stu("李四", 18), "上海");
    hMap.put(new Stu("王五", 18), "广州");
    hMap.put(new Stu("赵六", 18), "深圳");
    hMap.put(new Stu("张三", 18), "舟山");

    System.out.println(hMap);
}

ListHashMap 的特点

/**
 * LinkedHashMap 的特点 : 怎么存的怎么取出来
 */
public static void fun8() {
    LinkedHashMap<String, Integer> lMap = new LinkedHashMap<>();
    lMap.put("张三", 18);
    lMap.put("李四", 19);
    lMap.put("王五", 16);
    lMap.put("赵六", 15);

    System.out.println(lMap);
}

TreeMap

/**
 * TreeMap 可以对Map中的 键 进行排序(TreeSet 底层实现是由 TreeMap实现的)
 * 要排序,  也要实现comparable接口 或者使用比较器
 */
public static void fun9() {
    TreeMap<Students, String> tMap = new TreeMap<>();
    tMap.put(new Students("张三", 18), "北京");
    tMap.put(new Students("李四", 20), "上海");
    tMap.put(new Students("王五", 19), "广州");

    System.out.println(tMap);
}

Map的嵌套

    public static void fun11() {
    HashMap<Stu, String> classes1 = new HashMap<>();
    classes1.put(new Stu("李四", 18), "上海");
    classes1.put(new Stu("张三", 20), "北京");

    HashMap<Stu, String> classes2 = new HashMap<>();
    classes1.put(new Stu("王五", 25), "广州");
    classes1.put(new Stu("赵六", 22), "深圳");

    HashMap<HashMap<Stu, String>, String> java = new HashMap<>();

    java.put(classes1, "一班");
    java.put(classes2, "二班");

    for (HashMap<Stu, String> classes : java.keySet()) {
        //  java学科中的  对应value
        String javaValue = java.get(classes);
        System.out.println(javaValue);
        //  继续遍历
        for (Stu stu : classes.keySet()) {
            String hj = classes.get(stu);
            System.out.println(stu + " -- " + hj);
        }

    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值