簡體   English   中英

多個HashMap實現

[英]multiple HashMap implementation

我正在嘗試實現兩個HashMap的使用,似乎也或多或少地成功了。但是現在我的問題是我想基於一個check.ie更新hashMap的值,如果userId已存在於哈希中,需要更新對應的時間戳...

下面給出的是我的代碼。我們如何完成值的更新。可以使用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
    }
  }
}

在第二張地圖上循環(我假設您要更新用戶的所有sessionIds)並修改值。

if (key.equals(userId)) 
{
    System.out.println("Check Successful");
    String TimeStamp=newTime;

    for ( String sessionId : entry.getValue().keySet() )
    {
       entry.put(sessionid, timeStamp);
    }
}

你可以不用for循環

if(userSessionHash.get(userId)!=null)
  userSessionHash.get(userId).put(userSessionHash.get(userId).keySet().toArray[0], timestamp);
String userId = "userID3";
Long timeStamp =  userSessionHash.containsKey(userId) ? userSessionHash.get(userSessionHash) : 0;
userSessionHash.put(userId,new AtomicLong(request.getSession(false).getMaxInactiveInterval()))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM