# HashMap用法 示例

/*

*/

import java.util.*;
class HaspMapDemo{
public static void main(String[] args)
{
//Create a hasp map
HashMap   hm=new   HashMap();
//Put elements to the map
hm.put("Evan",new Double(12345.77));
hm.put("Rose",new Double(78777));
hm.put("Magic",new Double(-99.10));
hm.put("Mike",new Double(100.00));
hm.put("Sue",new Double(17.15));
//Get a set of the entries
Set set = hm.entrySet();
//Get an iterator
Iterator itr = set.iterator();
//Display elements
while (itr.hasNext()){
Map.Entry me = (Map.Entry)itr.next();
System.out.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
//Deposit 1000 into Evan's account
double balance = ((Double)hm.get("Evan")).doubleValue();
hm.put("Evan",new Double(balance + 1000));
System.out.println("Evan's new balance : " + hm.get("Evan"));
}
}

TreeMap用法 示例

/*

TreeMap类通过使用树来实现Map接口.TreeMap提供了按排序顺序存储关键字/值对的有效手段，同时允许快速检索。不像散列映射，树映射保证它的元素按照关键字升序排序。

*/

import java.util.*;
class TreeMapDemo{
public static void main(String[] args)
{
//Creat a tree map
TreeMap tm = new TreeMap();

//Put elements to the map
tm.put("Evan",new Double(12345.77));
tm.put("Rose",new Double(78777));
tm.put("Magic",new Double(-99.10));
tm.put("Mike",new Double(100.00));
tm.put("Sue",new Double(17.15));

//Get a set of entries
Set set = tm.entrySet();

//Get an iterator
Iterator i = set.iterator();

//Display elements
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();

//Deposit 1000 into Evan's account
double balance = ((Double)tm.get("Evan")).doubleValue();
tm.put("Evan",new Double(balance + 1000));
System.out.println("Evan's new balance : " + tm.get("Evan"));
}
}

private Hashtable<String, String> emails = new Hashtable<String, String>();

//      方法一: 用entrySet()
//  Iterator it = emails.entrySet().iterator();
//  while(it.hasNext()){
//   Map.Entry m=(Map.Entry)it.next();
//   logger.info("email-" + m.getKey() + ":" + m.getValue());
//  }

// 方法二：直接再循环中
for (Map.Entry<String, String> m : emails.entrySet()) {

logger.info("email-" + m.getKey() + ":" + m.getValue());
}

// 方法三：用keySet()
Iterator it = emails.keySet().iterator();
while (it.hasNext()){
String key;
key=(String)it.next();
logger.info("email-" + key + ":" + emails.get(key));
}

Map   aa   =   new   HashMap();
aa.put("tmp1",   new   Object());     //追加     替换用同样的函数.
aa.remove("temp1");                       //删除
for   (Iterator   i   =   aa.values().iterator();   i.hasNext();   )   {
Object   temp   =   i.next();
}         //遍历

hashmap遍历实例

import java.util.HashMap;
import java.util.Iterator;
import java.util.Calendar;

public class HashMapTest {

public static void main(String[] args) {
HashMap hashmap = new HashMap();
for (int i = 0; i < 1000; i++) {
hashmap.put("" + i, "hello");
}

long bs = Calendar.getInstance().getTimeInMillis();
Iterator iterator = hashmap.keySet().iterator();
// String value = "";
while (iterator.hasNext()) {
// value = hashmap.get(iterator.next());
System.out.println(hashmap.get(iterator.next()));
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
listHashMap();
}

public static void listHashMap() {
java.util.HashMap hashmap = new java.util.HashMap();
for (int i = 0; i < 1000; i++) {
hashmap.put("" + i, "hello");
}
long bs = Calendar.getInstance().getTimeInMillis();
// Set set = hashmap.entrySet() ;
java.util.Iterator it = hashmap.entrySet().iterator();
while (it.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
// entry.getKey() 返回与此项对应的键
// entry.getValue() 返回与此项对应的值
System.out.println(entry.getValue());
}
System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
}

}

• 评论

• 上一篇
• 下一篇