测试HashMapTest结果集
/*
* <p>Title: 测试HashMapTest结果集</p>
* <p>Description: 测试HashMapTest结果集</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: ***</p>
*/
package com.studysources.random;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* 类HashMapTest.java的实现描述:测试HashMapTest结果集
*
* @author wangdw wang8121@hotmail.com
* @version 1.0
* Date 2005-5-25
* @see java.lang.Class
* History:
* <author> <time> <version> <desc>
*/
public class HashMapTest
{
/**
* 特点:按照KEY的的顺序生序排列
* @return
*/
public SortedMap testSortedMap()
{
SortedMap sortedMap = new TreeMap();
sortedMap.put("20050602", "444");
sortedMap.put("20050601", "222");
sortedMap.put("20050401", "sss");
sortedMap.put("20050202", "bbb");
sortedMap.put("20041223", "ww1");
sortedMap.put("20041229", "11w");
return sortedMap;
}
/**
* 特点:按照KEY的传入顺序固定排列
* @return
*/
public HashMap testHashMap()
{
HashMap map = new HashMap();
map.put("20050602", "444");
map.put("20050601", "222");
map.put("20050401", "sss");
map.put("20050202", "333");
map.put("20041223", "ww1");
map.put("20041229", "11w");
return map;
}
/**
* 输出
* @param map
*/
private void print(Map map)
{
//测试 keySet
Iterator it = map.keySet().iterator();
System.out.print("keySet: ");
while (it.hasNext())
{
Object obj = it.next();
String str = obj.toString();
System.out.print(str + " ");
}
System.out.println("");
//测试 entrySet
Iterator ite = map.entrySet().iterator();
System.out.print("entrySet: ");
while (ite.hasNext())
{
Map.Entry obj = (Map.Entry) ite.next();
Object key = obj.getKey();
Object value = obj.getValue();
System.out.print("key:" + key.toString() + " ");
Integer.parseInt(key.toString());
String str = obj.toString();
System.out.print(str + " ");
}
System.out.println("");
//测试 values
Iterator iter = map.values().iterator();
System.out.print("values: ");
while (iter.hasNext())
{
Object obj = iter.next();
if (obj == null)
{
System.out.println("values is null");
return;
}
String str = obj.toString();
System.out.print(str + " ");
}
System.out.println("");
}
public static void main(String[] args)
{
HashMapTest hashMapTest = new HashMapTest();
HashMap map = hashMapTest.testHashMap();
SortedMap sortedMap = hashMapTest.testSortedMap();
hashMapTest.print(map);
hashMapTest.print(sortedMap);
}
}