java知识总结——Map接口具体使用案例

这是我参与更文挑战的第1天,活动详情查看: 更文挑战

Map的实现类的结构

Map:双列数据,存储key-value对的数据

  • HashMap:作为Map的主要实现类;线程不安全,效率高;存储null的key和value
  • LinkedHashMap:保证在遍历map元素时,可以按照添加的顺序实现遍历。
    • 原因:在原有的HashMap底层结构基础上,添加了一对指针,指向前一个和后一个元素。对于频繁的遍历操作,此类执行效率高于HashMap。
  • TreeMap:保证按照添加的key-value对进行排序,实现排序遍历。此时使用key的自然排序或定制排序。底层使用红黑树
  • Hashtable:作为古老的实现类;线程安全,效率低;不能存储null的key和value
    • Properties:常用来处理配置文件。key-value都是String类型

Map结构的理解

Map中的key:无序的、不可重复的,使用Set存储所有的key

  ->key所在类要重写equals()和hashCode()
复制代码

Map中的value:无序的、可重复的,使用Collection存储所有value

      ->value所在的类要重写equals()
复制代码

一个键值对:key-value构成了一个Entry对象。

Map中的entry:无序的、不可重复的,使用Set存储所有的entry

HashMap的底层实现原理?

与HashSet一致

Map中定义的方法

常用方法:

  • 添加:put
  • 删除:remove
  • 修改:put
  • 查询:get
  • 长度:size
  • 遍历:keyset/values/entrySet

所有方法

增,删,改

put、putAll、remove、clear

        map.put("AA", 123);//体现添加
        map.put(45, 123);
        map.put("BB", 56);
        map.put("AA", 87);//体现修改

        System.out.println(map);
        System.out.println("**********");

        Map map1 = new HashMap<>();
        map1.put("CC", 123);
        map1.put("DD", 123);

        map.putAll(map1);
        System.out.println(map);
        System.out.println("**********");

        //remove
        Object value = map.remove("CC");
        System.out.println(value);
        System.out.println(map);
        System.out.println("**********");

        //clear
//        map.clear();
//        System.out.println(map.size());
//        System.out.println(map);

复制代码

查询

get、containsKey、containsValue、size、isEmpty、equals

//查询
        //get
        System.out.println(map.get(45));
        //containsKey
        System.out.println(map.containsKey(45));
        //containsValue
        System.out.println(map.containsValue(123));
        //size
        System.out.println(map.size());
        //isEmpty()
        System.out.println(map.isEmpty());
        //equals
        System.out.println(map.equals(map1));
        System.out.println("**********");
复制代码

遍历操作

 //遍历操作
        //遍历所有的key集
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("**********");

        //遍历所有的value
        Collection values = map.values();
        for (Object obj : values) {
            System.out.println(obj);
        }
        System.out.println("**********");

        //遍历所有的key-value
        //方式一:
        Set entrySet = map.entrySet();
        Iterator iterator1 = entrySet.iterator();
        while (iterator1.hasNext()) {
            Object obj = iterator1.next();

            Map.Entry entry = (Map.Entry) obj;
            System.out.println(entry.getKey()+"---->"+entry.getValue());
        }
        System.out.println("**********");

        //方式二:
        Set keySet = map.keySet();
        iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            Object value1 = map.get(key);
            System.out.println(key+"---->"+value1);
        }

复制代码

TreeMap

向TreeSet中添加键值对,要求key必须是同一类创建的对象

要实现两种排序方式:自然排序和定制排序

自然排序

class Student implements Comparable{
     
     ....
     
    @Override
    public int compareTo(Object o) {
        if (o instanceof Student) {
            Student s = (Student) o;
            int com = -this.name.compareTo(s.name);
            if (com != 0) {
                return com;
            } else {
                return Integer.compare(this.age, s.age);
            }
        }else
            throw new RuntimeException();
    }
}
复制代码

定制排序

TreeMap map1 = new TreeMap(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Student && o2 instanceof Student) {
                    Student s1 = (Student) o1;
                    Student s2 = (Student) o2;
                    return Integer.compare(s1.getAge(), s2.getAge());
                } else {
                    throw new RuntimeException("类型不匹配");
                }
            }
        });
复制代码

Properties

package Map接口;

import java.io.FileInputStream;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args)throws Exception {
        Properties pros = new Properties();

        FileInputStream fis = new FileInputStream("jdbc.properties");

        pros.load(fis);
        String name = pros.getProperty("name");
        String password = pros.getProperty("password");

        System.out.println("name= " + name + ",password= " + password);

    }
}

复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享