※ 引述《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
→
03/05 01:10, , 1F
03/05 01:10, 1F
→
03/05 01:15, , 2F
03/05 01:15, 2F
好吧,我錯了,會影響
key 1 = "--" key 2 = "-"
就會跟
key 1 = "-" key 2 = "--"
搞混了...
那就換個跟前後key不同pattern的連接符號吧~_~
※ 編輯: GreatShot 來自: 220.133.110.47 (03/05 01:21)
→
03/05 01:19, , 3F
03/05 01:19, 3F
推
03/05 01:19, , 4F
03/05 01:19, 4F
→
03/05 01:24, , 5F
03/05 01:24, 5F
如果不能限制key的domain就沒辦法用這種方法,就像email
一定有人會說我的host name 跟 user name也要包含 @這個字元
※ 編輯: GreatShot 來自: 220.133.110.47 (03/05 01:28)
討論串 (同標題文章)