nt fury
nt fury

Reputation: 39

Storing data with different data types in a HashMap with single key

I have the following structure of data with datatypes as below:

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

I want to store this into an HashMap with Key as keyval and all others info (x,x2,result,performance) for the respective keyval.

Output Example:

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

How do I store it and how can I access it?

Upvotes: 0

Views: 4080

Answers (4)

azro
azro

Reputation: 54148

You need to store all the element in a class (and a constructor) :

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 + '}';
    }
}

To be used like this :

 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
}

To get back an element ;)

Upvotes: 2

shivam pandey
shivam pandey

Reputation: 21

Create a class WrapperClass which holds the below private field variables:

x                      Float           
x2                     Float                
result                 Float
performance            String  

and a constructor :

public WrapperClass(Float x, Float x2, Float result, String performance) {
     this.x = x;
     this.x2 = x2;
     this.result = result;
     this.performance = performance; 
}

Then Define a 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));

and so on ...

now you can get myMap.get("com.a.service") which will return WrapperClass object.

I hope this answers your question.

Upvotes: 0

Yahya
Yahya

Reputation: 14062

You can do something like this:

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

Output

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

Upvotes: 1

fabfas
fabfas

Reputation: 2228

You can have map of type Map<String, CustomObject> map. Your CustomObject POJO looks like;

public class CustomObject {

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

 // constructor , setter and getters
}

You can retrieve the value calling

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

Upvotes: 1

Related Questions