How can I implement a map with different data types as values?
You want to use boost::variant
:
std::map <std::string, boost::variant<typeX, typeY>>
How to hold the mutiple values of type in HashMap?
Simply define the Map
as
Map<String, Object> body = new HashMap<>();
A Map<String, Object>
can hold any kind of value so also:
String
ArrayList<Integer>
This solution give you the most flexibility if you don't know what values (type and name) you need to store.
If you know exactly what data you need to handle a better solution is to define a custom class having properties named as the keys of the Map.
public class DataHolder {
private String message;
private List<Integer> groupdestId;
private String id;
private List<Integer> userdestId;
// Add getters and setters
}
Additionally if you know that those data don't change values over time a better solution is to create an immutable class so it can be shared in a multithreading environment without syncronize access to them.
Storing data with different data types in a HashMap with single key
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 ;)
Maps with multiple types of values in java
It is possible to do something like Map<String, Object>
.
But: I would strongly suggest to think your design over. You should use a class for your persons instead. That way you could do: Map<String, Person>
with Person
having getters and setters for names, phone numbers and other information.
Example Person
class:
public class Person {
private String name;
private List<String> phoneNumbers = Collections.emptyList();
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPhoneNumbers(List<String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
public void addPhoneNumber(String number) {
phoneNumbers.add(number);
}
public List<String> getPhoneNumbers() {
return phoneNumbers;
}
}
make a map with different data types in go for making post request with json data
postBody, err := json.Marshal(map[string]interface{}{
"name": name,
"stream": stream,
"grades": []map[string]interface{}{{
sub1: sub1_score,
sub2: sub2_score,
}},
})
or, if you'd like to avoid having to retype map[string]interface{}
type Obj map[string]any
postBody, err := json.Marshal(Obj{
"name": name,
"stream": stream,
"grades": []Obj{{
sub1: sub1_score,
sub2: sub2_score,
}},
})
https://go.dev/play/p/WQMiE5gsx9w
How to create a Map of different type of value in typescript
Typescript is not able to infer value type properly in this case, but you can specify generic type parameters explicitly when calling Map
's constructor:
const testMap = new Map<string, string | number>([["a", "test1"], ["b", "test2"], ["c", 1]]);
Playground
Related Topics
How Serious Is the New/Delete Operator Mismatch Error
Variable or Field Declared Void
Does Copy List Initialization Invoke Copy Ctor Conceptually
Move Out Element of Std Priority_Queue in C++11
When How to Omit the Return Type in a C++11 Lambda
Opencv on MAC Is Not Opening Usb Web Camera
How to Parse CSV Using Boost::Spirit
Application Has Failed to Start Because Msvcp100D.Dll Was Not Found, Reinstalling App May Work
Improve Matching of Feature Points with Opencv
How to Load a Bmp on Glut to Use It as a Texture
C++ Templates: Conditionally Enabled Member Function
When Would You Use an Array Rather Than a Vector/String
Why Does Sizeof(Int) Vary Across Different Operating Systems