Map

本文详细介绍了 Java 中四种不同的 Map 实现:HashMap、TreeMap、LinkedHashMap 以及使用 Collections.synchronizedMap 方法创建线程安全的 HashMap。每种实现都有其特点和适用场景,通过实例演示了如何使用这些 Map 类型。
package ds.collections.maps;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.LinkedHashMap;

/**
 * Map 
 * @author Hust
 * @Time 2011-10-23
 */
public class MapTest {

    public static void main(String[] args) {
        //testHashMap();
        //hashMapTest();
        //treeMapTest();
        linkedHashMapTest();
    }
    
    /**
     * 测试HashMap
     */
    public static void testHashMap(){
        //无序,hashMap本身的实现不是同步的即不是线程安全的
        //如想设为线程安全,初始化时加上:Collections.synchronizedMap
        Map<Integer,String> thMap = Collections.synchronizedMap(new HashMap<Integer, String>(32));
        thMap.put(1232, "12323321");
        thMap.put(1, "1233421");
        thMap.put(4, "12323321");
        thMap.put(123, "23455");
        //System.out.println(thMap.hashCode()+"_"+thMap.size());
        //thMap.remove(123);
        thMap.put(123, "234");
        thMap.put(null, "234");
        //System.out.println(thMap.size());
        //System.out.println(thMap.isEmpty());
        //System.out.println(thMap.containsKey(123));
        //System.out.println(thMap.containsValue("123321"));
        //System.out.println(thMap.get(123));
        //System.out.println(thMap.values());    
        for(Entry<Integer,String> b : thMap.entrySet()) {
               System.out.print(b.getKey()+"_");//获取键  
               System.out.println(b.getValue());//获取值  
        } 
        //thMap.clear();
        System.out.println("=thMap.size() "+thMap.size());
        //Map.Entry--Map的内部类,描述Map中的按键/数值对。   
        Map<Integer,String> linkMap = new LinkedHashMap<Integer, String>(thMap);
        
         for(Entry<Integer,String> b : linkMap.entrySet()) {
               System.out.print(b.getKey()+"_");//获取键  
               System.out.println(b.getValue());//获取值  
        } 
        
    }
    
    /**
     * 无序  适合 插入、删除和定位元素容量小时具有很快的访问速度
     * 容量很大,实际数据较少时,遍历起来可能会比LinkedHashMap慢
     */
    public static void hashMapTest() {  
        System.out.println("------hashMapTest------");  
        Map<String,String> map = new HashMap<String,String>();  
        map.put("1", "Value 1");  
        map.put("2", "Value 2");  
        map.put("3", "Value 3");  
        map.put("4", "Value 4");  
        map.put("F", "Value F");  
        map.put("Q", "Value Q");  
        
        //必要时初始化时控制Map大小
        //其负载因子默认为0.75,容量(大小)默认为16, 如果(负载因子)*(容量)〉map大小,则调整Map大小
        //8 * 0.75 = 6
        Map<String,String> map2 = new HashMap<String,String>(8);  
        map2.putAll(map);
        
        //可对之进行迭代,Entry,Map的内部类,描述Map中的按键/数值对。
        //Map.entrySet返回map的collection视图
        Iterator<Entry<String, String>> it = map2.entrySet().iterator();  
        while (it.hasNext()) {  
            Map.Entry<String,String> e = (Map.Entry<String,String>) it.next();  
            System.out.println(e.getKey() + " _ " + e.getValue());  
        }  
    }  
    
    /**
     * TreeMap取出来的是排序后的键值对。
     * 它是sortedMap的唯一实现
     */
    public static void treeMapTest() {  
        System.out.println("------treeMapTest------");  
        Map<String,String> map = new TreeMap<String,String>();  
        map.put("1", "Value 1");  
        map.put("F", "Value F");  
        map.put("Q", "Value Q"); 
        map.put("3", "Value 2");  
        map.put("5", "Value 5");  
        map.put("4", "Value 4");   
        
        Iterator<Entry<String, String>> it = map.entrySet().iterator();   
        while (it.hasNext()) {  
            Map.Entry<String,String> e = (Map.Entry<String,String>) it.next();  
            System.out.println(e.getKey() + " _ " + e.getValue());  
        }  

        System.out.println("------treeMapTest2------");  
        //TreeMap取出来的是排序后的键值对。它是sortedMap的唯一实现。
        //默认按键对象升序排列,如想降序排列,实现方式为将Collections.reverseOrder()作为TreeMap的构造方法。
        Map<String,String> tmap = new TreeMap<String,String>(Collections.reverseOrder());
        tmap.putAll(map);
        Iterator<Entry<String, String>> tmapit = tmap.entrySet().iterator();   
        while (tmapit.hasNext()) {  
            Map.Entry<String,String> e = (Map.Entry<String,String>) tmapit.next();  
            System.out.println( e.getKey() + " _ " + e.getValue());  
        }  
        
    } 
    
    /**
     * 保存了记录的插入顺序,即先插入的先遍历到
     * 也可以在构造时用带参数,按照应用次数排序。
     */
    public static void linkedHashMapTest() {  
        System.out.println("------linkedHashMapTest------");  
        Map<String,String> map = new LinkedHashMap<String,String>();  
        map.put("1", "Value 1");  
        map.put("2", "Value 2");  
        map.put("5", "Value 3");  
        map.put("4", "Value 4");  
        map.put("F", "Value F");  
        map.put("Q", "Value Q");  
        Iterator<Entry<String, String>> it = map.entrySet().iterator();  
        while (it.hasNext()) {  
            Map.Entry<String,String> e = (Map.Entry<String,String>) it.next();  
            System.out.println(e.getKey() + " _ " + e.getValue());  
        }  
    } 
    
}

 

转载于:https://www.cnblogs.com/znsongshu/p/6282589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值