Hashmap holding different data types as values for instance Integer, String and Object

前端 未结 5 1174
别那么骄傲
别那么骄傲 2020-12-12 12:11

I need to create a hashmap with key as integer and it should hold multiple values of different data types. For example if the key is msg id and the values are

5条回答
  •  抹茶落季
    2020-12-12 12:26

    You have some variables that are different types in Java language like that:

     message of type string
     timestamp of type time
     count of type integer
     version of type integer
    

    If you use a HashMap like:

    HashMap yourHash = new HashMap();
    yourHash.put("message","message");
    yourHash.put("timestamp",timestamp);
    yourHash.put("count ",count);
    yourHash.put("version ",version);
    

    If you want to use the yourHash:

    for(String key : yourHash.keySet()){
      String message = (String) yourHash.get(key);
      Datetime timestamp= (Datetime) yourHash.get(key);
      int timestamp= (int) yourHash.get(key);
    }
    

提交回复
热议问题