zoukankan      html  css  js  c++  java
  • HashMap、Hashtable、TreeMap、Properties

    HashMap、Hashtable、TreeMap、Properties

    Map接口与Collection并列存在。用于保存具有映射关系的数据:key-value

    Map接口中的key和value都可以是任何引用类型的数据

    Map对象所对应的类,需要重写hashCode和equals方法

    常用String类作为Map的键

    key和value之间存在单向一对一关系,即通过指定的key总能找到唯一的、确定的value。

    HashMap:

    hashMap是Map接口使用频率最高的实现类。

    允许使用null键和null值,与HashSet一样,不保证映射的顺序

    hashMap判断两个key相等的标准是:两个key通过equals方法的返回true,hashCode值也相等

    hashMap判断两个value相等的标准是:两个value通过equals方法返回true。

    package com.xatu.集合;
     
    import java.io.Externalizable;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
     
    import org.junit.Test;
     
    public class MapTest {
    	@Test
    	public void testHashMap() {
    		Map<Integer, String> m = new HashMap<>();
    		m.put(2, "cc");
    		m.put(3, "aa");
    		m.put(6, "dd");
    		m.put(9, "f");
    		m.put(2, "cb");//键相同覆盖之前
    		m.put(null, null);
    		
    		System.out.println("容量:"+m.size());
    	String rs =	m.remove(3);//删除词条
    		System.out.println(rs);
    		System.out.println("查找value:"+m.containsValue("ghg"));
    		System.out.println("查找key:"+m.containsKey(5));
    		System.out.println("之后的"+m.size());
    		
    		//先获取所有的键
    		Set<Integer> s = m.keySet();
    		Iterator<Integer> it = s.iterator();
    		while (it.hasNext()) {
    			Integer key =  it.next();
    			String value = m.get(key);
    			System.out.println("键:"+key+"值:"+value);
    			
    		}
    		System.out.println("-----------");
    		Set<Map.Entry<Integer, String>> ent = m.entrySet();
    		for (Entry<Integer, String> entry : ent) {
    			System.out.println("键:"+entry.getKey()+"值:"+entry.getValue());
    		}
    	}
    }
    

    LinkedHashMap:

    LinkedHashMap是hashMap的子类

    与LinkedHashSet类似,LinkedHashMap可以维护Map的迭代顺序:迭代顺序与Key-value对的插入顺序一致

    TreeMap

    TreeMap存储 Key-Value 对时,需要根据 key-value 对进行排序。TreeMap 可以保证所有的 Key-Value 对处于有序状态。

    TreeMap 的 Key 的排序:

    自然排序:TreeMap 的所有的 Key 必须实现 Comparable 接口,而且所有的 Key 应该是同一个类的对象,否则将会抛出 ClasssCastException

    定制排序:创建 TreeMap 时,传入一个 Comparator 对象,该对象负责对 TreeMap 中的所有 key 进行排序。此时不需要 Map 的 Key 实现 Comparable 接口

    TreeMap判断两个key相等的标准:两个key通过compareTo()方法或者compare()方法返回0。

    若使用自定义类作为TreeMap的key,所属类需要重写equals()和hashCode()方法,且equals()方法返回true时,compareTo()方法应返回0。

    Hashtable:

    Hashtable是个古老的 Map 实现类,线程安全。

    与HashMap不同,Hashtable 不允许使用 null 作为 key 和 value

    与HashMap一样,Hashtable 也不能保证其中 Key-Value 对的顺序

    Hashtable判断两个key相等、两个value相等的标准,与hashMap一致。

    Properties:

    Properties 类是 Hashtable 的子类,该对象用于处理属性文件

    由于属性文件里的 key、value 都是字符串类型,所以 Properties 里的 key 和 value 都是字符串类型

    存取数据时,建议使用setProperty(String key,String value)方法和getProperty(String key)方法

  • 相关阅读:
    js(5)关于this的指代值
    bootstrap(2)关于表单
    bootstrap(1)关于排版
    bootstrap基础(0)写在前面的一些话
    js(4) 继承
    js(3)面向对象的程序设计
    js(2)关于作用域和作用域链
    鼠标事件(jQuery方法)
    鼠标事件(JS原生方法)
    键盘事件(在输入框中输入内容后按回车键)
  • 原文地址:https://www.cnblogs.com/athony/p/13435170.html
Copyright © 2011-2022 走看看