我想實現兩個HashMap的使用,它似乎也或多或少成功也。但現在我的問題是我想更新基於check.ie的哈希值的值,如果userId是已經存在的哈希我需要更新對應的時間戳...多個HashMap的實現
下面給出的是我的代碼。我們如何完成值的更新。可以做到我的setValue ..?但如何..?請幫助我的朋友..
public static void main(String[] args)
{
HashMap sessionTimeStampHash = new HashMap<Long, Long>(); //sessionID is the key and timeStamp is the value
//userID is the key and sessionTimeStampHash object is the value
HashMap<String, HashMap<Long, Long>> userSessionHash = new HashMap<String, HashMap<Long, Long>>();
sessionTimeStampHash.put("sessionID", "timeStamp");
userSessionHash.put("userID", sessionTimeStampHash);
// System.out.println(userSessionHash);
sessionTimeStampHash = new HashMap();
sessionTimeStampHash.put("sessionID1", "timeStamp1");
userSessionHash.put("userID1", sessionTimeStampHash);
// System.out.println(userSessionHash);
sessionTimeStampHash = new HashMap();
sessionTimeStampHash.put("sessionID2", "timeStamp2");
userSessionHash.put("userID2", sessionTimeStampHash);
// System.out.println(userSessionHash);
sessionTimeStampHash = new HashMap();
sessionTimeStampHash.put("sessionID3", "timeStamp3");
userSessionHash.put("userID3", sessionTimeStampHash);
// System.out.println(userSessionHash);
for (Entry<String, HashMap<Long, Long>> entry : userSessionHash.entrySet())
{
String key = entry.getKey();
System.out.println(key);
System.out.println(entry.getValue());
String userId = "userID3";
if (key.equals(userId))
{
System.out.println("Check Successful");
String TimeStamp = newTime;
entry.setValue() // how can i change my timeStamp
}
}
}
您聲明`sessionTimeStampHash`作爲`地圖`但你`sessionTimeStampHash.put( 「sessionID1」, 「timeStamp1」); `你在輸入'Long'值還是`String`值? –
2011-02-16 11:18:41