2017-07-03 270 views
0

我的数据与数据类型如下结构如下:与HashMap中存储不同数据类型的数据单键

Name     Type 
Keyval     String  //This is the key element 
x      Float   
x2      Float     
result     Float 
performance   String 

我想这个存储到HashMap与Key作为keyval和所有其他信息(x,x2,result,performance)分别为keyval

输出例:

[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] 

如何存放它,我怎么能访问它?

+1

'地图<字符串值>地图'哪里'价值'是包含您引用的字段的类表示值(x,x2,结果和性能)? – davidxxx

回答

2

您需要存储所有在一个类中的元素(和构造函数):

public class Element { 
    float x; 
    float x2; 
    float result; 
    String performance; 

    public Element(float x, float x2, float result, String performance) { 
     this.x = x; 
     this.x2 = x2; 
     this.result = result; 
     this.performance = performance; 
    } 

    @Override 
    public String toString() { 
     return "Element{" + "x=" + x + ", x2=" + x2 + ", result=" + result + ", performance=" + performance + '}'; 
    } 
} 

要这样来使用:

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 
} 

要返回元素;)

+0

嗨!非常感谢您的回复。实际上,当我用“System.out.println(Arrays.asList(map4))”打印散列表时,我遇到了一个检索散列表的问题;“该列表打印如下:[{[email protected],com.b.serv[email protected],[email protected],[email protected];你能帮我解答吗?@azro –

+0

这是因为在该类中没有“String toString()方法,因此它使用Object one并打印需要附加的引用(请参阅我的编辑) – azro

+0

它的工作非常好!非常感谢! –

0

创建一个类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<KeyVal, WrapperClass> myMap = new HashMap<>();

myMap.put("com.a.service", new WrapperClass(0.5,0.07,0.02,IMRPOVED)); 
myMap.put("com.b.service", new WrapperClass(0.03,0.07,0.04,IMRPOVED)); 

等.. 。

现在你可以得到myMap.get("com.a.service")这将返回WrapperClass对象。

我希望这能回答你的问题。

+0

这不是一个“包装”类。 –

1

您可以使用Map<String, CustomObject> map类型的地图。您的CustomObject POJO看起来像;

public class CustomObject { 

Float x; 
Float x2; 
Float result; 
String performance; 

// constructor , setter and getters 
} 

可以检索调用

CustomObject object = map.get('keyValue'); 
1

你可以做这样的事情的价值:

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); 
    } 
} 

输出

KeyValue= com.a.service , X= 0.03, X2= 0.07, Result= 0.04, Performance= IMPROVED 
+0

嗨!非常感谢您的回应。其实,我有一个问题检索hashmap,当我打印使用“System.out.println(Arrays.asList(map4));”列表打印如下:[{[email protected],[email protected],com.c .service = Value @ 5cad8086,[email protected];你能帮我解决这个问题吗? –

+0

非常感谢@Yahya !!! –