/*
程序开始创建了一个散列映射,然后将名字的映射增加到平衡表。接下来,映射的内容通过使用由调用函数entrySet()而获得的集合“视图”而显示出来。关键字和值通过调用由Map.Entry定义的getKey()和getValue()方法而显示。注意存款是如何被制成Evan的账目的。put()方法自动用新值替换与指定关键字相关联的原先值。
*/
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"));
}
}
最近再写一段很恶心的系统代码,上网搜索资料时,偶然见看到这篇文章不错,给了我很多的启发。先收藏一下,作为以后开发备用资料。希望看到的朋友也能多多学习学习!
发现HashMap 用到下面的格式 ,直接循环遍历整个hashMap hashTable
返回的 set 中的每个元素都是一个 Map.Entry
类型。
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));
}
另外 我们可以先把hashMap 转为集合Collection,再迭代输出,不过得到的对象
Map aa = new HashMap();
aa.put("tmp1", new Object()); //追加 替换用同样的函数.
aa.remove("temp1"); //删除
for (Iterator i = aa.values().iterator(); i.hasNext(); ) {
Object temp = i.next();
} //遍历
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);
}
}