HashMap
HashMap对key进行散列。
keySet()、values()、entrySet()。
TreeMap
TreeMap按照key进行排序。
HashMap和TreeMap的比较
import
java.util.
*
;
class HashMapTest
... {
public static void printElements(Collection c)
...{
Iterator it=c.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
public static void main(String[] args)
...{
HashMap hm=new HashMap();
hm.put("one","zhangsan");
hm.put("two","lisi");
hm.put("three","wangwu");
System.out.println(hm.get("one"));
System.out.println(hm.get("two"));
System.out.println(hm.get("three"));
Set keys=hm.keySet();
System.out.println("Key:");
printElements(keys);
Collection values=hm.values();
System.out.println("Value:");
printElements(values);
Set entry=hm.entrySet();
//printElements(entry);
Iterator it=entry.iterator();
while(it.hasNext())
...{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+":"+me.getValue());
}
}
}
class HashMapTest
... {
public static void printElements(Collection c)
...{
Iterator it=c.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
public static void main(String[] args)
...{
HashMap hm=new HashMap();
hm.put("one","zhangsan");
hm.put("two","lisi");
hm.put("three","wangwu");
System.out.println(hm.get("one"));
System.out.println(hm.get("two"));
System.out.println(hm.get("three"));
Set keys=hm.keySet();
System.out.println("Key:");
printElements(keys);
Collection values=hm.values();
System.out.println("Value:");
printElements(values);
Set entry=hm.entrySet();
//printElements(entry);
Iterator it=entry.iterator();
while(it.hasNext())
...{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+":"+me.getValue());
}
}
}
和Set类似,HashMap的速度通常都比TreeMap快,只有在需要排序的功能的时候,才使用TreeMap。
Java1.0/1.1的集合类
Vector:用ArrayList代替Vector。
Hashtable:用HashMap代替Hashtable。
Satck:用LinkedList代替Stack。
Properties
import
java.util.
*
;
import java.io. * ;
class PropTest
... {
public static void main(String[] args)
...{
/**//*Properties pps=System.getProperties();
pps.list(System.out);*/
Properties pps=new Properties();
try
...{
pps.load(new FileInputStream("winsun.ini"));
Enumeration enum=pps.propertyNames();
while(enum.hasMoreElements())
...{
String strKey=(String)enum.nextElement();
String strValue=pps.getProperty(strKey);
System.out.println(strKey+"="+strValue);
}
}
catch(Exception e)
...{
e.printStackTrace();
}
}
}
import java.io. * ;
class PropTest
... {
public static void main(String[] args)
...{
/**//*Properties pps=System.getProperties();
pps.list(System.out);*/
Properties pps=new Properties();
try
...{
pps.load(new FileInputStream("winsun.ini"));
Enumeration enum=pps.propertyNames();
while(enum.hasMoreElements())
...{
String strKey=(String)enum.nextElement();
String strValue=pps.getProperty(strKey);
System.out.println(strKey+"="+strValue);
}
}
catch(Exception e)
...{
e.printStackTrace();
}
}
}