hashmaptest.java

来自「一些JavaAPI测试的源代码(包括一些Java中常用的数据结构类」· Java 代码 · 共 63 行

JAVA
63
字号
/**
 *Author Miracle
 *Time	2005.11.24 14:04
 *
 */
 
 import java.util.*;
 
 /**
  *
  *Attention : Vector and HashMap ,
  * 当线程同步时,用Vector ,或用synchronizetion能返回Set ,List,等		 
  */
 
 public class HashMapTest
 {
 	public static void printElements(Collection c)
 	{
 		Iterator iterator = c.iterator();
 		while(iterator.hasNext())
 		{
 			System.out.println(iterator.next());
 		}
 	}
 	
 	public static void main(String [] args)
 	{
 		HashMap hm = new HashMap();
 		hm.put("one","miracle");
 		hm.put("two","xiaob");
 		hm.put("three","zsd");
 		
 		System.out.println(hm.get("two"));
 		System.out.println(hm.get("three"));
 		System.out.println(hm.get("one"));
		System.out.println(); 		
 		/**** Test keySet() & values() & entrySet()  ****/
 		Set keys = hm.keySet();
		System.out.println("Key:");
		printElements(keys);
		
		System.out.println();
		Collection values = hm.values();
		System.out.println("Values:");
		printElements(values);
 		
 		System.out.println();
 		Set entry = hm.entrySet();
 		printElements(entry);
 		Iterator iter = entry.iterator();

		System.out.println();
 		
 		System.out.println("Key : Values");
 		while(iter.hasNext())
 		{
 			Map.Entry me = (Map.Entry)iter.next();
 			System.out.println(me.getKey()+":"+me.getValue());
 		}
 	}
 	
 }
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?