Java集合进阶——Map

本文介绍了Java中的Map接口及其实现类(HashMap、LinkedHashMap和TreeMap),包括Map的基本操作如添加、删除、查找、遍历,以及不同实现类的特点,如哈希表性质的HashMap、有序的LinkedHashMap和排序的TreeMap。
摘要由CSDN通过智能技术生成

一、Map 

package com.JinJie;

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

public class myMap {

    public static void main(String[] args) {

        Map<String, Integer> map = new HashMap<>();

        //添加元素
        /*
        * put方法细节:添加和覆盖
        * 当添加的元素集合中不存在时,put方法返回的String是null
        * 当添加的元素集合中存在时,put方法会将原来的元素覆盖,并返回覆盖元素的值

        * */
        map.put("冰红茶",3);
        map.put("脉动",4);
        map.put("王老吉",4);
        map.put("维他命水",5);
        System.out.println(map.put("茶π", 5));
        System.out.println(map.put("脉动", 7));


        //删除对应键值元素
        //map.remove("冰红茶",3);
        map.remove("冰红茶");

        //集合是否包含该键、值。
        System.out.println(map.containsKey("茶π"));
        System.out.println(map.containsValue(4));

        //集合是否为空
        System.out.println(map.isEmpty());

        //集合大小
        System.out.println(map.size());

        //输出集合
        System.out.println(map);

        //清空集合
         map.clear();
        System.out.println(map);
    }
}

package com.JinJie.myMap;

import java.util.*;
import java.util.function.BiConsumer;

public class demo02 {
    //map集合的三种遍历方式

    public static void main(String[] args) {

        Map<String,Integer> map = new HashMap<>();

        map.put("冰红茶",3);
        map.put("脉动",4);
        map.put("王老吉",4);
        map.put("维他命水",5);

        //1,键找值
        //(1)通过keySet()方法将map集合中的键获取并存放到一个单列数组

        Set<String> key = map.keySet();
        //(2)遍历单列数组
        for (String s : key) {
            //(3) 通过get()方法获取键对应的值
            Integer integer = map.get(s);
            System.out.println(s+"--->"+ integer);
        }

        /*key.forEach(s -> {
            System.out.println(map.get(s));
        });

        Iterator<String> iterator = key.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(map.get(next));
        }*/

        //2,键值对
        //(1)通过map的entrySet方法将所有键值对存放到一个Set集合
        //(2)遍历这个Set集合
        //(3)通过getKey()和getValue()方法获取键和值

        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            String key1 = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key1+"---->"+value);
        }

        //3,,lambda表达式
        map.forEach((String s, Integer integer) -> {
                System.out.println(s+"====>"+integer);
        });
    }
}
 

二、HashMap

 

 

练习一: 

package com.JinJie.myMap.myHashMap;

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

public class demo03 {

    public static void main(String[] args) {

        Student s1 = new Student("zhangsan",18);
        Student s2 = new Student("lisi",20);
        Student s3 = new Student("zhaoliu",21);
        Student s4 = new Student("zhaoliu",21);

        HashMap<Student, String> stringHashMap = new HashMap<>();
        stringHashMap.put(s1,"湖南");
        stringHashMap.put(s2,"广东");
        stringHashMap.put(s3,"江苏");
        stringHashMap.put(s4,"海南");

        //遍历,lambda方式
        Set<Map.Entry<Student, String>> entrySet = stringHashMap.entrySet();
        entrySet.forEach((e)->{
            System.out.println(e.getKey()+"===>"+e.getValue());
        });

        //键找值
        System.out.println("-----------------------");
        Set<Student> studentSet = stringHashMap.keySet();
        for (Student student : studentSet) {
            String s = stringHashMap.get(student);
            System.out.println(student+"="+s);
        }

        //键值对
        System.out.println("-----------------------");
        Set<Map.Entry<Student, String>> entries = stringHashMap.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            System.out.println(entry.getKey()+"="+entry.getValue());
        }

    }
}
 

练习二:

 

 

package com.JinJie.myMap.myHashMap;

import java.util.*;

public class demo04 {

    public static void main(String[] args) {

        //定义一个数组,存放景点
        String[] arr = {"A","B","C","D"};

        //模拟学生投票
        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 80; i++) {
            int index = r.nextInt(arr.length);
            list.add(arr[index]);
        }
//        System.out.println(list);

        HashMap<String, Integer> hashMap = new HashMap<>();
        //判断集合中是否已经存在景点,不存在则添加,值为1
        //存在则值加1
        for (String s : list) {
            if (hashMap.containsKey(s)) {
                Integer integer = hashMap.get(s);
                integer++;
                hashMap.put(s,integer);
            }else hashMap.put(s,1);
        }
        System.out.println(hashMap);

        //判断集合中最大值
        int max = 0;
        Set<Map.Entry<String, Integer>> entrySet = hashMap.entrySet();
        for (Map.Entry<String, Integer> stringIntegerEntry : entrySet) {
            int value = stringIntegerEntry.getValue();
            if (value > max){
                max = value;
            }
        }

        //输出最大值的键名,也就是景点
        for (Map.Entry<String, Integer> resultEntry : entrySet) {
            String key = resultEntry.getKey();
            Integer value = hashMap.get(key);
            if (value == max){
                System.out.println(value+"--->"+key);
            }
        }

    }
}
 

三、LinkedHashMap

四、TreeMap 

练习:

 

需求1:

package com.JinJie.myMap.myTreeMap;

import java.util.TreeMap;

public class demo05 {

    public static void main(String[] args) {

        TreeMap<Integer, String> treeMap = new TreeMap<>((o1, o2) -> {
            int i = o1.compareTo(o2) > 0 ? -1 : (o1.compareTo(o2) == 0 ? 0 : 1);
            return i;
        });
        treeMap.put(1,"王老吉");
        treeMap.put(2,"冰红茶");
        treeMap.put(3,"脉动");
        treeMap.put(1,"王老吉");

        System.out.println(treeMap);
    }
}
 

需求2:

package com.JinJie.myMap.myTreeMap;

import com.JinJie.MySet.hashset.Student;
import com.JinJie.myMap.myHashMap.Student3;

import java.util.TreeMap;

public class demo06 {

    public static void main(String[] args) {

        Student3 s1 = new Student3("zhangsan",20);
        Student3 s4 = new Student3("zhangsan",20);
        Student3 s2 = new Student3("lisi",21);
        Student3 s3 = new Student3("wangwu",22);
        Student3 s5 = new Student3("bge",22);

        TreeMap<Student3, String> treeMap = new TreeMap<>((o1,o2)->{
            int i = o1.getAge() == o2.getAge() ? o1.getName().compareTo(o2.getName()) : o1.compareTo(o2);
            return i;
        });
        treeMap.put(s1,"江苏");
        treeMap.put(s2,"上海");
        treeMap.put(s3,"深圳");
        treeMap.put(s4,"南京");
        treeMap.put(s5,"北京");

        System.out.println(treeMap);

    }
}
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值