Java 使用单个键在HashMap中存储具有不同数据类型的数据
我有以下数据类型的数据结构,如下所示:Java 使用单个键在HashMap中存储具有不同数据类型的数据,java,hashmap,Java,Hashmap,我有以下数据类型的数据结构,如下所示: Name Type Keyval String //This is the key element x Float x2 Float result Float performance
Name Type
Keyval String //This is the key element
x Float
x2 Float
result Float
performance String
我想将其存储到一个HashMap
中,其中键为keyval
,以及相应keyval
的所有其他信息(x,x2,result,performance
)
输出示例:
[Keyval=com.a.service, x=0.05, x2=0.07, result=0.02, performance = IMPROVED]
[Keyval=com.b.service, x=0.03, x2=0.07, result=0.04, performance = IMPROVED]
[Keyval=com.c.service, x=0.15, x2=0.07, result=0.08, performance = DEGRADED]
如何存储它以及如何访问它?创建一个类
WrapperClass
,该类包含以下私有字段变量:
x Float
x2 Float
result Float
performance String
和一个构造器:
public WrapperClass(Float x, Float x2, Float result, String performance) {
this.x = x;
this.x2 = x2;
this.result = result;
this.performance = performance;
}
然后定义一个Map myMap=newhashmap()代码>
等等
现在您可以获取myMap.get(“com.a.service”)
,它将返回WrapperClass
对象
我希望这能回答你的问题 创建一个类WrapperClass
,该类包含以下私有字段变量:
x Float
x2 Float
result Float
performance String
和一个构造器:
public WrapperClass(Float x, Float x2, Float result, String performance) {
this.x = x;
this.x2 = x2;
this.result = result;
this.performance = performance;
}
然后定义一个Map myMap=newhashmap()代码>
等等
现在您可以获取myMap.get(“com.a.service”)
,它将返回WrapperClass
对象
我希望这能回答你的问题 您需要将所有元素存储在类(和构造函数)中:
要像这样使用:
public static void main(String[] args) {
HashMap<String, Element> map = new HashMap<String, Element>();
map.put("com.a.service", new Element(0.05, 0.07, 0.02, "IMPROVED"));
//...
Element a = map.get("com.a.service"); //x=0.05, x2=0.07, result=0.02, performance = IMPROVED
}
publicstaticvoidmain(字符串[]args){
HashMap=newHashMap();
map.put(“com.a.service”,新元素(0.05,0.07,0.02,“改进”);
//...
元素a=map.get(“com.a.service”);//x=0.05,x2=0.07,result=0.02,性能=改进
}
要获取元素,请执行以下操作;) 您需要将所有元素存储在类(和构造函数)中:
要像这样使用:
public static void main(String[] args) {
HashMap<String, Element> map = new HashMap<String, Element>();
map.put("com.a.service", new Element(0.05, 0.07, 0.02, "IMPROVED"));
//...
Element a = map.get("com.a.service"); //x=0.05, x2=0.07, result=0.02, performance = IMPROVED
}
publicstaticvoidmain(字符串[]args){
HashMap=newHashMap();
map.put(“com.a.service”,新元素(0.05,0.07,0.02,“改进”);
//...
元素a=map.get(“com.a.service”);//x=0.05,x2=0.07,result=0.02,性能=改进
}
要获取元素,请执行以下操作;) 您可以拥有类型为map map
的map。您的CustomObject
POJO看起来像
public class CustomObject {
Float x;
Float x2;
Float result;
String performance;
// constructor , setter and getters
}
您可以通过调用
CustomObject object = map.get('keyValue');
您可以拥有类型为map-map
的地图。您的CustomObject
POJO看起来像
public class CustomObject {
Float x;
Float x2;
Float result;
String performance;
// constructor , setter and getters
}
您可以通过调用
CustomObject object = map.get('keyValue');
您可以这样做:
import java.util.HashMap;
import java.util.Map;
public class Value{
String performance;
float x, x2, result;
public Value(float x, float x2, float result, String performance) {
this.performance = performance;
this.x = x;
this.x2 = x2;
this.result = result;
}
public static void main(String[] args) {
Map<String, Value> map = new HashMap<>();
// to add values
map.put("com.a.service", new Value(0.03f, 0.07f, 0.04f, "IMPROVED"));
// to access them
Value value = map.get("com.a.service");
System.out.printf("KeyValue= com.a.service , X= %.2f, X2= %.2f, Result= %.2f, Performance= %s",
value.x, value.x2, value.result, value.performance );
}
}
您可以这样做:
import java.util.HashMap;
import java.util.Map;
public class Value{
String performance;
float x, x2, result;
public Value(float x, float x2, float result, String performance) {
this.performance = performance;
this.x = x;
this.x2 = x2;
this.result = result;
}
public static void main(String[] args) {
Map<String, Value> map = new HashMap<>();
// to add values
map.put("com.a.service", new Value(0.03f, 0.07f, 0.04f, "IMPROVED"));
// to access them
Value value = map.get("com.a.service");
System.out.printf("KeyValue= com.a.service , X= %.2f, X2= %.2f, Result= %.2f, Performance= %s",
value.x, value.x2, value.result, value.performance );
}
}
Map
其中Value
是包含引用字段以表示值(x,x2,结果和性能)的类Map
其中Value
是包含引用字段以表示值(x,x2,结果和性能)的类?这不是“包装器”类。这不是“包装器”同学们,嗨!非常感谢您的回复。实际上,当我使用“System.out.println(Arrays.asList(map4));”打印hashmap时,检索hashmap时遇到了一个问题,列表打印如下:[{com.a.service=Element@12a3a380,com.b.service=Element@29453f44,com.c.service=Element@5cad8086,com.d.service=Element@6e0be858;你能帮我吗?@azroIt是因为没有“String-toString()方法,以便它使用对象1并打印需要附加的引用(请参见我的编辑)没问题,取决于您的IDE,您可能有地方生成这些方法,如构造函数、setter/getter、toString…easilyHi!非常感谢您的响应。实际上,当我使用“System.out.println(Arrays.asList(map4));”打印hashmap时,检索hashmap时遇到问题。列表打印如下:[{com.a.service=Element@12a3a380,com.b.service=Element@29453f44,com.c.service=Element@5cad8086,com.d.service=Element@6e0be858;你能帮我吗?@azroIt是因为没有“String-toString()方法,以便它使用对象1并打印需要附加的引用(请参见我的编辑)没问题,取决于您的IDE,您可能有地方生成这些方法,如构造函数、setter/getter、toString…easilyHi!非常感谢您的响应。实际上,当我使用“System.out.println(Arrays.asList(map4));”打印hashmap时,检索hashmap时遇到问题。列表打印如下:[{com.a.service=Value@12a3a380,com.b.service=Value@29453f44,com.c.service=Value@5cad8086,com.d.service=Value@6e0be858;你能在这方面帮我吗?嗨!非常感谢你的回复。实际上,当我用“System.out.println(Arrays.asList(map4));”打印hashmap时,检索hashmap时遇到了一个问题。列表打印如下:[{com.a.service=Value@12a3a380,com.b.service=Value@29453f44,com.c.service=Value@5cad8086,com.d.service=Value@6e0be858你能帮我一下吗?