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
我需要创建一个键为整数的hashmap,它应该包含不同数据类型的多个值。例如,如果键是msg id且值是
- message of type string
- timestamp of type time
- count of type integer
- version of type integer
类型字符串的消息
类型时间的时间戳
整数类型的计数
整数类型的版本
Then how to store the values of different data type with a single key into the hashmap?
那么如何用一个键将不同数据类型的值存储到hashmap中?
4 个解决方案
#1
36
If you don't have Your own Data Class, then you can design your map as follows
如果您没有自己的数据类,则可以按如下方式设计地图
Map<Integer, Object> map=new HashMap<Integer, Object>();
Here don't forget to use "instanceof" operator while retrieving the values from MAP.
这里不要忘记在从MAP中检索值时使用“instanceof”运算符。
If you have your own Data class then then you can design your map as follows
如果您有自己的Data类,那么您可以按如下方式设计地图
Map<Integer, YourClassName> map=new HashMap<Integer, YourClassName>();
Map
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
Map<Integer,Demo> map=new HashMap<Integer, Demo>();
Demo d1= new Demo(1,"hi",new Date(),1,1);
Demo d2= new Demo(2,"this",new Date(),2,1);
Demo d3= new Demo(3,"is",new Date(),3,1);
Demo d4= new Demo(4,"mytest",new Date(),4,1);
//adding values to map
map.put(d1.getKey(), d1);
map.put(d2.getKey(), d2);
map.put(d3.getKey(), d3);
map.put(d4.getKey(), d4);
//retrieving values from map
Set<Integer> keySet= map.keySet();
for(int i:keySet){
System.out.println(map.get(i));
}
//searching key on map
System.out.println(map.containsKey(d1.getKey()));
//searching value on map
System.out.println(map.containsValue(d1));
}
}
class Demo{
private int key;
private String message;
private Date time;
private int count;
private int version;
public Demo(int key,String message, Date time, int count, int version){
this.key=key;
this.message = message;
this.time = time;
this.count = count;
this.version = version;
}
public String getMessage() {
return message;
}
public Date getTime() {
return time;
}
public int getCount() {
return count;
}
public int getVersion() {
return version;
}
public int getKey() {
return key;
}
@Override
public String toString() {
return "Demo [message=" + message + ", time=" + time
+ ", count=" + count + ", version=" + version + "]";
}
}
#2
18
You have some variables that are different types in Java language like that:
你有一些Java语言中不同类型的变量:
message of type string
timestamp of type time
count of type integer
version of type integer
If you use a HashMap like:
如果您使用HashMap,例如:
HashMap<String,Object> yourHash = new HashMap<String,Object>();
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);
}
#3
15
Define a class to store your data first
定义一个类来存储您的数据
public class YourDataClass {
private String messageType;
private Timestamp timestamp;
private int count;
private int version;
// your get/setters
...........
}
And then initialize your map:
然后初始化你的地图:
Map<Integer, YourDataClass> map = new HashMap<Integer, YourDataClass>();
#4
2
Create an object holding following properties with an appropriate name.
使用适当的名称创建一个包含以下属性的对象。
- message
- timestamp
- count
- version
and use this as a value in your map.
并将其用作地图中的值。
Also consider overriding the equals() and hashCode() method accordingly if you do not want object equality to be used for comparison (e.g. when inserting values into your map).
如果您不希望将对象相等性用于比较(例如,将值插入到地图中),还应考虑相应地重写equals()和hashCode()方法。
#1
36
If you don't have Your own Data Class, then you can design your map as follows
如果您没有自己的数据类,则可以按如下方式设计地图
Map<Integer, Object> map=new HashMap<Integer, Object>();
Here don't forget to use "instanceof" operator while retrieving the values from MAP.
这里不要忘记在从MAP中检索值时使用“instanceof”运算符。
If you have your own Data class then then you can design your map as follows
如果您有自己的Data类,那么您可以按如下方式设计地图
Map<Integer, YourClassName> map=new HashMap<Integer, YourClassName>();
Map
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
Map<Integer,Demo> map=new HashMap<Integer, Demo>();
Demo d1= new Demo(1,"hi",new Date(),1,1);
Demo d2= new Demo(2,"this",new Date(),2,1);
Demo d3= new Demo(3,"is",new Date(),3,1);
Demo d4= new Demo(4,"mytest",new Date(),4,1);
//adding values to map
map.put(d1.getKey(), d1);
map.put(d2.getKey(), d2);
map.put(d3.getKey(), d3);
map.put(d4.getKey(), d4);
//retrieving values from map
Set<Integer> keySet= map.keySet();
for(int i:keySet){
System.out.println(map.get(i));
}
//searching key on map
System.out.println(map.containsKey(d1.getKey()));
//searching value on map
System.out.println(map.containsValue(d1));
}
}
class Demo{
private int key;
private String message;
private Date time;
private int count;
private int version;
public Demo(int key,String message, Date time, int count, int version){
this.key=key;
this.message = message;
this.time = time;
this.count = count;
this.version = version;
}
public String getMessage() {
return message;
}
public Date getTime() {
return time;
}
public int getCount() {
return count;
}
public int getVersion() {
return version;
}
public int getKey() {
return key;
}
@Override
public String toString() {
return "Demo [message=" + message + ", time=" + time
+ ", count=" + count + ", version=" + version + "]";
}
}
#2
18
You have some variables that are different types in Java language like that:
你有一些Java语言中不同类型的变量:
message of type string
timestamp of type time
count of type integer
version of type integer
If you use a HashMap like:
如果您使用HashMap,例如:
HashMap<String,Object> yourHash = new HashMap<String,Object>();
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);
}
#3
15
Define a class to store your data first
定义一个类来存储您的数据
public class YourDataClass {
private String messageType;
private Timestamp timestamp;
private int count;
private int version;
// your get/setters
...........
}
And then initialize your map:
然后初始化你的地图:
Map<Integer, YourDataClass> map = new HashMap<Integer, YourDataClass>();
#4
2
Create an object holding following properties with an appropriate name.
使用适当的名称创建一个包含以下属性的对象。
- message
- timestamp
- count
- version
and use this as a value in your map.
并将其用作地图中的值。
Also consider overriding the equals() and hashCode() method accordingly if you do not want object equality to be used for comparison (e.g. when inserting values into your map).
如果您不希望将对象相等性用于比较(例如,将值插入到地图中),还应考虑相应地重写equals()和hashCode()方法。