2017-07-03 9 views
0

でのHashMapの異なるデータ型のデータを格納し、私はデータ型とのデータの次のような構造を持っている:私はkeyvalなどの主要および他のすべての情報をHashMapにこれを格納したい次のように単一のキー

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

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] 

私はそれを保存しないと私はそれをどのようにアクセスするにはどうすればよいですか?

+1

'地図<文字列値の> map'ここで 'Value'値(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]、[email protected]、[email protected]、[email protected];これを私に助けてもらえますか?@azro –

+0

そのクラスには "String toString()メソッドがないので、オブジェクト1を使用し、それを追加する必要がある参照を印刷します(私の編集を参照してください) – 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 =値@ 5cad8086、[email protected];これで私を助けてくれますか? –

+0

ありがとう@Yahya! –

関連する問題