开发者

multiple HashMap implementation

I am trying to implement the use of two HashMaps and it seems to be more or less successful also.But now my problem is i want to update the value of a hashMap based on a check.ie if userId is already existing in the hash i need to update the timestamp corresponding to it...

Below given is my code.How can we accomplish the updation of values .Can that be done usiong setValue..?but how..?please help me friends..

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
开发者_StackOverflow中文版  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
    }
  }
}


You can do without for loop

if(userSessionHash.get(userId)!=null)
  userSessionHash.get(userId).put(userSessionHash.get(userId).keySet().toArray[0], timestamp);


Loop on your second map (I assume you want to update all the sessionIds for the user) and modify the value.

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

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


String userId = "userID3";
Long timeStamp =  userSessionHash.containsKey(userId) ? userSessionHash.get(userSessionHash) : 0;
userSessionHash.put(userId,new AtomicLong(request.getSession(false).getMaxInactiveInterval()))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜