`
jiauwu
  • 浏览: 82322 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论
阅读更多

Map 键值对,数据结构测试代码如下

MapTest

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());  
    	}  
    } 
    
}
 


分享到:
评论
1 楼 jiauwu 2011-11-07  
补充:
/**
 * 对Map的值集与键集的遍历
 */
public static void keyValueSet(){
	Map<Integer,String> thMap = new HashMap<Integer, String>(32);
	thMap.put(1232, "12323321");
	thMap.put(1, "1233421");
	thMap.put(4, "12323321");
	//value
	Iterator<String> it = (Iterator<String>) thMap.values().iterator();
	while (it.hasNext()) {
		System.out.println(it.next());
	}
	//key
	Iterator<Integer> itk = (Iterator<Integer>) thMap.keySet().iterator();
	while (itk.hasNext()) {
		System.out.println(itk.next());
	}
	
}

相关推荐

Global site tag (gtag.js) - Google Analytics