javaSE.Map

Map

特点

  • 无序、无下标;
  • 存储键值对<Key,Value>,键不能重复,值可以重复;
  • 一旦键值相同,值不同,则做替换操作;

方法

V put(K key,V value) //将对象存入到集合中,关联键值,key重复则覆盖;
V get(Object key) //根据键获取对应的值
Set keySet()<> //返回所有的key的set集合
Collection values() //返回包含所有值的Collection集合
Set<Map.Entry<K,V>> entrySet() //键值匹配的Set集合

HashMap【重点】

  • jdk1.2以后;
  • 线程不安全,运行效率快;
  • 允许用null作为key或value。
/**
 * HashMap集合的使用
 * 存储结构:哈希表(数组+链表+红黑树)
 */
public class HashMapDemo{
    public static void main(String[] args) {
        //创建集合
        HashMap<Student,String> students=new HashMap<>();
        //1.添加元素
        Student s1=new Student("tom",01);
        Student s2=new Student("bob",03);
        Student s3=new Student("joe",02);
        students.put(s1,"beijing");
        students.put(s2,"shanghai");
        students.put(s3,"guangzhou");
        students.put(s3,"shengzhen");
        //可以添加进去,重写hashCode和equals就可以避免重复
        students.put(new Student("tom",01),"chongqing");
        System.out.println("元素个数:"+students.size());
        System.out.println(students.toString());
        //2.删除
        //students.remove(s1);
        //3.遍历
        System.out.println("---3.1 使用keySet()---");
        for(Student key:students.keySet()){
            System.out.println(key.toString()+"= "+students.get(key));
        }
        System.out.println("---3.2 使用entrySet()---");
        for(Map.Entry<Student,String> entry:students.entrySet()){
            System.out.println(entry.getKey()+"= "+entry.getValue());
        }
        //4.判断
        System.out.println(students.containsKey(s2));//true
        System.out.println(students.containsKey(new Student("tom",01))); //false
        System.out.println(students.containsValue("shanghai"));//true
    }
}

Hashtable

  • jdk1.0后;
  • 线程安全,运行效率慢;
  • 不允许null作为key或value;
  • 现在基本不用了。

Properties

  • Hashtable的子类;
  • 要求key和value都是String类型;
  • 通常用于配置文件的读取。

TreeMap

  • 实现了SortedMap(Map的子接口);
  • 可以根据key自动排序;
/**
 * TreeMap的使用
 * 存储结构:红黑树
 * @author Lenovo
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        //创建集合
        TreeMap<Student,String> treeMap=new TreeMap<Student,String>();
        //1.添加元素
        Student s1=new Student("rose",18);
        Student s2=new Student("mary",17);
        Student s3=new Student("kate",19);
        treeMap.put(s1,"bj"); //Student必须实现Comparable接口
        treeMap.put(s2, "sh");
        treeMap.put(s3, "gz");
        treeMap.put(new Student("kate", 19), "cq");
        System.out.println("元素个数:"+treeMap.size());
        System.out.println(treeMap.toString());
        //2.删除
        treeMap.remove(new Student("kate", 19));
        System.out.println("元素个数:"+treeMap.size());
        //3.遍历
        System.out.println("---3.1 keySet()---");
        for (Student key:treeMap.keySet()){
            System.out.println(key+": "+treeMap.get(key));
        }
        System.out.println("---3.2 entrySet()---");
        for (Map.Entry<Student,String> entry:treeMap.entrySet()){
            System.out.println(entry.getKey()+": "+entry.getValue());
        }
        //4.判断
        System.out.println(treeMap.containsKey(new Student("kate", 19)));
        System.out.println(treeMap.containsValue("sh"));
    }
}
posted @ 2022-03-10 09:15  老李学Java  阅读(35)  评论(0)    收藏  举报