作者GreatShot ()
看板java
標題Re: [問題] hashtable的問題
時間Fri Mar 5 01:03:49 2010
※ 引述《tstanly ()》之銘言:
: 已爬文
: http://caterpillar.onlyfun.net/Gossip/JavaGossip-V2/HashMap.htm
: 根據上面基本的Hashtable(Hashmap)教學,
: 宣告
: Hashtable<String,Integer> test=new Hashtable<String,Integer>();
: 我知道這樣是一個key->value
: 那如果要兩個key對一個value呢?
: 謝謝!
Q1: 兩個key組合後map一個value
String combinedKey = key1 + "-" + key2;
最簡單了(如果key是String的話)
Q2: 累加hashmap裡value的值
HashMap<String, Integer> aa = new HashMap<String,Integer>();
aa.put("super-star", 5565);
System.out.println("super star is " + aa.get("super-star"));
//should be 5565
//這一行就是你要的答案,不一定比較快但是很直觀
aa.put("super-star", new Integer(aa.get("super-star").intValue() + 1));
System.out.println("super star is " + aa.get("super-star"));
//should be 5566
--
import
java.ptt.stationery.*;//推坑是一種無窮遞迴
public void static Pushdown(
Victim newbie)
{
Victim[] newbies =
SearchNewbieNearby(
newbie);
for(
Victim p:newbies)
Pushdown(p);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.133.110.47
→ sbrhsieh:如果 key 可能包含 '-' 字元呢? 03/05 01:10
→ GreatShot:不影響 03/05 01:15
好吧,我錯了,會影響
key 1 = "--" key 2 = "-"
就會跟
key 1 = "-" key 2 = "--"
搞混了...
那就換個跟前後key不同pattern的連接符號吧~_~
※ 編輯: GreatShot 來自: 220.133.110.47 (03/05 01:21)
→ sbrhsieh:怎麼會不影響呢?("super-", "star") ("super", "-star") 03/05 01:19
推 tstanly:thanks!! 03/05 01:19
→ sbrhsieh:我的焦點就是如果 key1/key2 的 domain 等於 String 03/05 01:24
如果不能限制key的domain就沒辦法用這種方法,就像email
一定有人會說我的host name 跟 user name也要包含 @這個字元
※ 編輯: GreatShot 來自: 220.133.110.47 (03/05 01:28)