Map集合

Map集合概述

Map集合_第1张图片
Map集合概述

Map集合常用实现类

Map集合_第2张图片
Map集合常用实现类

Map接口中常用方法

HashMap map=new HashMap<>();
        map.put("张三","zhansgan");
        map.put("李四","lisi");
        map.put("王五","wnagwu");
        map.put("李留","liliu");
        System.out.println(map);
        String kk = map.remove("张三");
        System.out.println(kk);
        System.out.println(map);
        String put = map.put("王五", "新增");
        System.out.println(put);
        System.out.println(map);
        String oo = map.get("王五");
        System.out.println(oo);
        boolean ll = map.containsKey("王五");
        System.out.println(ll);
Map集合_第3张图片
Map接口中常用方法1

Map集合遍历键找值

  HashMap map=new HashMap<>();
        map.put("张三","zhansgan");
        map.put("李四","lisi");
        map.put("王五","wnagwu");
        map.put("李留","liliu");
        Set strings = map.keySet();
        for (String keys:strings
             ) {
            System.out.println(keys+"---"+map.get(keys));
        }
        Iterator iterator = strings.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next+"----"+map.get(next));
        }
Map集合_第4张图片
Map集合遍历键找值
Map集合_第5张图片
Map集合遍历键找值

entey键值对对象

      HashMap map=new HashMap<>();
        map.put("张三","zhansgan");
        map.put("李四","lisi");
        map.put("王五","wnagwu");
        map.put("李留","liliu");
        Set> entries = map.entrySet();
        for (Map.Entry entry:entries
             ) {
            System.out.println(entry.getKey()+"----"+entry.getValue());
        };
Map集合_第6张图片
entey键值对对象

hashmap存储自定义类型键值

package com.mujiachao;

import java.util.Objects;

public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

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

    @Override
    public int hashCode() {

        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
     HashMap map=new HashMap<>();
        map.put(new Student("张三",10),1);
        map.put(new Student("李四",10),2);
        map.put(new Student("王五",10),3);
        map.put(new Student("李六",10),4);
        map.put(new Student("张三",10),5);
        System.out.println(map);
hashmap存储自定义类型键值

LinkedHashMap集合

Map集合_第7张图片
LinkedHashMap集合

HashTable集合

        HashMap map=new HashMap<>();
         map.put(null,"1111");
         map.put("pppp",null);
        System.out.println(map);

        //会报错   java.lang.NullPointerException 键和值都不允许放null值
        Hashtable table=new Hashtable<>();
        table.put(null,"222");
        table.put("221112",null);
Map集合_第8张图片
HashTable集合

练习题

Map集合_第9张图片
图片.png
        Scanner scanner = new Scanner(System.in);
        HashMap objectObjectHashMap = new HashMap<>();
        System.out.println("请输入字符串:");
        String next = scanner.next();
        for (Character c : next.toCharArray()
                ) {
           if (objectObjectHashMap.containsKey(c)){
               Integer integer = objectObjectHashMap.get(c);
               integer++;
               objectObjectHashMap.put(c,integer);
           }
           else {
               objectObjectHashMap.put(c,1);
           }
        }
        for (Character cc:objectObjectHashMap.keySet()
             ) {
            System.out.println(cc+"--->"+objectObjectHashMap.get(cc));
        }

你可能感兴趣的:(Map集合)