I am trying to find a way to store a new instance of a class as the value in a Java hashmap. The idea was given to me by a Java instructor in order to create a data storage structure that could be used to for a program I am working on.
The idea he recommended to me was to use a hashmap that stored the name of a computer as the key and the value would be a new instance of the class InfoStor.class. InfoStor contains methods such as getName(), setName(), getMemory()...
I have the class and the method pretty much setup for a basic test to see if it would work or not. The problem I am running into is I cannot figure out how to use the methods inside of InfoStor once I have created a new entry in the hashmap.
This is the code I have so far;
VMware.class
import java.util.HashMap;
public class VMware {
public static void main(String[] args) {
HashMap <String, Object> mapper = new HashMap();
mapper.put("NS01", new InfoStor("NS01"));
//mapper.get("NS01").
}
}
InfoStor.class
public class InfoStor {
private String vmName;
private String platform;
private Integer memory;
public InfoStor (String name) {
vmName = name;
}
String getName(){
return vmName;
}
void setPlatform(String p){
platform = p;
}
String getPlatform(){
return platform;
}
void setMemory(Integer m){
memory = m;
}
Integer getMemory(){
return memory;
}
}
What I am trying to accomplish is something like this (basic idea).
Object var = mapper.get("NS01");
System.out.println(var.getMemory());
Am I going about this the wrong way? Any help is appreciated thanks.
The problem is that your code only specifies that the values in the map are Object. You know more than that, so tell the compiler that information:
HashMap<String, InfoStor> mapper = new HashMap<String, InfoStor>();
mapper.put("NS01", new InfoStor("NS01"));
...
InfoStor value = mapper.get("NS01");
Integer memory = value.getMemory();
Note that it's generally though not always better to use interfaces for the variable types - and you can use the diamond operator for the constructor call, letting the compiler use type inference to fill in the type arguments:
Map<String, InfoStor> mapper = new HashMap<>();
mapper.put("NS01", new InfoStor("NS01"));
...
InfoStor value = mapper.get("NS01");
Integer memory = value.getMemory();
If you declare your hashmap like so:
HashMap<String, InfoStor> mapper = new HashMap<String, InfoStor>();
Then when you get an object out of the mapper, it will be an instance of InfoStor (you won't need to cast it or worry about a class cast exception because it's not the rist class.)
So:
InfoStor myStor = mapper.get("somekey");
myStor.getMemory(); // this will work
Otherwise, if you stick with the HashMap<String, Object> you used in your original code, you'll need to cast it before you call the method:
Object obj = mapper.get("somekey");
((InfoStor)obj).getMemory(); // cast is required
obj.getMemory(); // this will not compile
You should read up on Java generics.
Make use of the generics added to java. They help with both compile-time type-checking and they make the casts unnecessary.
HashMap <String, Object> mapper = new HashMap();
//you will be able to retrieve an object and then cast it to your InfoStore
InforStore isN01 = (InfoStore)mapper.get("N01");
//this will unfortunately be accepted, even thought it's a bug
mapper.put("N02", new Integer(0));
________________________
HashMap <String, InfoStore> mapper = new HashMap();
//you will be able to retrieve an object and then cast it to your InfoStore
InforStore isN01 = mapper.get("N01"); //no cast
Youre on the right track...
Initialise the map as:
HashMap <String, InfoStor> mapper = new HashMap<String, InfoStor>();
Then after adding objects to the map retrieve them with:
InfoStor var = mapper.get("NS01");
System.out.println(var.getMemory());
you can cook something by using array...for example if you can store objects in arrays then use that idea to achieve it in hash map...i dont knw how you design but i once got stuck in that and made through like this
example...
class princess{
int age;
public princess(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
}
public class hashmaptest {
public static void main(String[] args) {
princess[] p=new princess[10];
HashMap scores = new HashMap();
scores.put("a",new princess(6));
scores.put("b",new princess(7));
p[0]=(princess)scores.get("a");
System.out.println(p[0].getAge());
p[0]=null;
p[0]=(princess)scores.get("b");
System.out.println(p[0].getAge());
}
}
Related
I am wondering how to build a nested Map and then retrieve the nested key:value pairs. This is how I created the map to be nested.
//Create List of Nested HashMaps
List<Map> nestedMap = new ArrayList<>();
nestedMap.add(building3Map);
nestedMap.add(building31Map);
nestedMap.add(buildingHFTFMap);
System.out.println("The nested map is: " + nestedMap);
This is the system output for the following code:
The nested map is: [{buildingname=[Building 3], buildingid=[3]}, {buildingname=[Building 31], buildingid=[1]}, {buildingname=[HFTF], buildingid=[4]}]
This is correct as I want a list of maps. But the next step is what is confusing to me. When I try to build the outer layer Map as below:
HashMap<String, List<Map>> queryMap = new HashMap<>();
queryMap.put("buildings", nestedMap);
System.out.println(queryMap.get("buildings.buildingid"));
I get a system output of null when attempting the .get("buildings.buildingid") method. Ideally, I need the output to look like this:
[[3, 1, 4]]
Where it returns all values with a key of "buildings.buildingid" in an array. I am new to coding so please let me know if I'm making any fundamental errors and how I can create a nested Map where I can access the inner layer or if there is another method I should be using.
I think you are making it way too complicated than it should be. you can store your data in a simple map where the ids are the keys for example and the names are the values. So you only need to read the keys or the values from the map to get your result.
Map<Integer, String> myMap = new HashMap<>();
myMap.put(3, "Building 3");
myMap.put(31, "Building 31");
myMap.put(4, "HFTF");
System.out.println(myMap.keySet());
System.out.println(myMap.values());
However, Java is an object-oriented language. If it makes sense for your use case you might want to create a custom class "Building" instead of frantically trying to store your data in nested data structures. See below for an example of how it might look using a custom class:
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String args[]) {
List<Building> buildings = new ArrayList<>();
buildings.add(new Building(3, "Building 3"));
buildings.add(new Building(31, "Building 31"));
buildings.add(new Building(4, "HFTF"));
List<Integer> buildingIds = new ArrayList<>();
buildings.forEach(building -> buildingIds.add(building.getBuildingId()));
List<String> buildingNames = new ArrayList<>();
buildings.forEach(building -> buildingNames.add(building.getBuildingName()));
System.out.println(buildingIds);
System.out.println(buildingNames);
}
public static class Building {
int buildingId;
String buildingName;
public Building(final int buildingId, final String buildingName) {
this.buildingId = buildingId;
this.buildingName = buildingName;
}
public int getBuildingId() {
return buildingId;
}
public void setBuildingId(final int buildingId) {
this.buildingId = buildingId;
}
public String getBuildingName() {
return buildingName;
}
public void setBuildingName(final String buildingName) {
this.buildingName = buildingName;
}
}
}
queryMap.get("buildings.buildingid") returns null, because queryMap only contains a value under the key
buildings. A HashMap can only access the value using the same key it was stored under. The key and the value is not processed in any further way.
A simple alternative could be
queryMap.get("buildings").stream() // Create a Stream<Map>
.map(building -> building.get("buildingid")) // Create Stream<String> by extracting the buildingid values.
.collect(Collectors.toList()); // Collect Stream into a List<String> which contains the buildingid's
If you don't like this solution you could take a deeper look into property expressions, and write your own map implementation that can resolve these expressions. But it will be a lot of work to get it working correctly...
Is there a way to get or set an array element stored in a Java Map?
Example:
If we have a map like this:
{
name: "Blah",
friends: ["Foo", "Bar"]
}
Map<String, Object> myMap = new HashMap<>();
List<String> friends = new ArrayList<>();
myMap.put("name", "Blah");
myMap.put("friends", friends);
Is it possible to use Reflection to get or set the first element in the friends array in the "myMap" from the string: "myMap.friends[0]"
Your question is not very clearly written and I believe that's why you are not getting the answer you expect but, If I understood your question correctly, you need to parse the following input string at runtime that you don't know beforehand:
myMap.friends[0]
And this should be parsed into components like:
mapName = "myMap"
mapKey = "friends"
valueIndex = 0
And with this information, you need to manipulate data in a Map at runtime through reflection.
Note: This only makes sense if you could potentially have more complex expressions, using different sort of objects and accessing nested properties of retrieved objects, otherwise you wouldn't need reflection at all.
Note 2: You may want to have a look at JXPath which already does a lot of this for you based on a XPath-like syntax for navigating object graphs.
That said, if my assumptions are correct and you still want to do it yourself, consider the following example.
For the sake of demonstration, let's consider our map is returned by a method myMap inside a Context.
private static class Context {
public Map<String, Object> myMap() {
Map<String, Object> myMap = new HashMap<>();
List<String> friends = new ArrayList<>();
friends.add("Foo");
friends.add("Bar");
myMap.put("name", "Blah");
myMap.put("friends", friends);
return myMap;
}
}
I'm assuming you are already parsing the input string into the different components. If not, for this simple string you could do it with simple regular expressions. If you already have the components, let's consider the following method:
public static Object readContextMap(Context context,
String mapName, String mapKey, Integer mapValueIndex) throws Exception {
// gets Context class for inspection
Class<?> cls = context.getClass();
// search for a method based on supplied mapName
Method mapMethod = cls.getDeclaredMethod(mapName);
// get a value from the retrieved map based on mapKey
Object mapValue = mapMethod.getReturnType()
.getDeclaredMethod("get", Object.class)
.invoke(mapMethod.invoke(context), mapKey);
// if the result is of type list, use the index to return the indexed element
if (List.class.isAssignableFrom(mapValue.getClass())) {
return ((List<?>)mapValue).get(mapValueIndex);
}
// otherwise return the object itself
return mapValue;
}
For testing purposes, consider the following main method:
public static void main(String[] args) throws Exception {
Context context = new Context();
String input = "myMap.friends[0]";
// parse input into...
String mapName = "myMap";
String mapKey = "friends";
Integer valueIndex = 0;
Object firstFriend = readContextMap(context, mapName, mapKey, valueIndex);
System.out.println(firstFriend);
// prints Foo
Object name = readContextMap(context, "myMap", "name", null);
System.out.println(name);
// prints Blah
}
This should be approximately what you want. You can easily create variations of this to set values as well. Please bear in mind that this code is just for demo purposes and needs a better error handling (e.g. verify if the context is really returning a map and nothing else).
This should be something along the lines you are looking for.
There's no need to use reflection here. You can simply cast it (which is also unsafe, but less so).
You can just do this:
List<String> friends = (List<String>) myMap.get("friends");
friends.set(0, "Bob");
Suppose i have a method which has map as return type and uses generics.
I would like to know what is the best practice of filling that Map object.
Please see the snippet.
public Map<String,?> getEmployeeInfo(String query) {
Map<String,Object> dataMap = new HashMap<String,Object>();
// do some op.
String empId = "abc123";
List<Long> projectIds = new ArrayList<Long>();
List<String> performanceGoals = new ArrayList<String>();
dataMap.put("empId",empId);
dataMap.put("projectIds",projectIds);
dataMap.put("performanceGoals",performanceGoals);
return dataMap;
}
The best practise is: Don't use this.
Make a Class Employee with members
public class Employee {
String id;
List<Long> projectIds;
List<String> performanceGoals;
...
}
And you method changes to:
public Employee getEmployeeInfo(String query) {
...
update for clarification why returning Map is bad in general
If your method returns:
Map<String,?> or Map<String,? extends Object> you say (in slang):
Hey look here, I am returning something. Store it in a variable called "something", because I don't say anything about the value.
If you write this method, you have to ensure, that you know every single line of code, where you work with your Map.
Lets say I would like to change employeeId from String to Integer. This will lead to really bad RuntimeExceptions and ClassCastExceptions.
I need to create a JSONObject from a HashMap of a custom class's toString and a float value. I tried the code below hoping that it would just work:
public class MyClass {
...
public String toString() {
return "a nice string"
}
}
HashMap<MyClass,Float> map = new HashMap<MyClass,Float>();
map.put(...);
JSONObject json = new JSONObject(map);
But I get:
java.lang.ClassCastException: MyClass cannot be cast to java.lang.String
What's the best way to create this JSONObject? Thanks.
You need to change this:
HashMap<MyClass,Float> map = new HashMap<MyClass,Float>();
with
HashMap<String,Float> map = new HashMap<String,Float>();
as you said "HashMap of a custom class's toString and a float value"
You haven't mentioned how are you putting the values into the hashmap.
But if you using toString method of your custom class, then you should put it like :
MyClass m = new MyClass();
map.put(m.toString,123.45f);
Seems like you're using the org.json library. If you take a look at the code of the JSONObject class, apparently they're not using generics.
public JSONObject(Map map) {
this.map = new HashMap();
if (map != null) {
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Map.Entry e = (Map.Entry)i.next();
Object value = e.getValue();
if (value != null) {
this.map.put(e.getKey(), wrap(value));
}
}
}
}
This map seems to handle entries with a String key and an Object value by the look of the keyPool map they use to manage unique String keys. In the comments, its also stated that:
This is used by JSONObject.put(string, object).
So it would be correct to assume the keys of the JSON objects are Strings.
Your MyClass type can't be upcasted to String directly (String is not a superclass of MyClass), that's why the constructor is actually complaining about the map, because it needs a map of the form HashMap<String,Object> (Note that there's no problem with Float and Object).
To fix the issue, you have to define a HashMap<String,Float> where you should store a String representation of your MyClass object either by using toString.
If you can't use a String you can consider using an intermediate structure that maps a code represented with a String to a certain MyClass object, so you can retain your MyClass class.
Both Gamb's and Abu's answers are correct and helped me to get to my final result.
I solved my problem like this:
HashMap<MyClass,Float> obj = functionThatReturnsThisStructure();
JSONObject jsonObj = new JSONObject();
for (Entry<MyClass,Float> entry: obj.entrySet()) {
jsonObj.put(entry.getKey().toString(), entry.getValue());
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Maps with multiple types of values in java
I have an odd question. Maybe I'm going about this the wrong way, but let's see where this question goes :)
I would like a Map container that contains either Strings or lists of Strings. I want to enforce this rule during construction of this object so that you can't create a map with values that aren't either of those.
e.g.
class Record {
public Record(String key, Map<String,Object> attrs) {
// check that attrs only contains Objects which are Strings or List<Strings>
}
}
Other ways I have thought of to solve the problem might be...
1)
class Record {
public Record(String key, Map<String,String> attrs, Map<String,List<String>> multiAttrs) {
// ...
}
}
2)
class Record {
public Record(String key, Map<String,Value> attrs) {
// ...
}
}
class Value {
// Create some funky class that encapsulates lists.
// Perhaps returning the only element in the list if the size is 1,
// but returning the list otherwise
}
I am not immediately excited at the alternatives, but I'm just putting it there as stuff I've already considered. Really I want the distinction between Strings and List to be transparent to the user of the class.
Have you considered ListMultimap? For the single value case the list would only have one element. Multimap allows multiple elements (values) to be mapped to each key. So your method would be:
public Record(String key, ListMultimap<String, String> attrs)...
Also, since your Record seems to be another mapping, consider using Table which allows for two-key mapping.
Check out ArrayListMultimap from Google which will help with this need
You can continue calling put on this map, if you need to get the map in its simplified form you can use this method, or modify it :)
public static Map<Field, String> toSingularMap(ArrayListMultimap<Field, String> map) {
Map<Field, String> singular_map = new HashMap<Field, String>();
if (map != null && !map.isEmpty()) {
Map<Field, Collection<String>> real_map = map.asMap();
for (Iterator<Entry<Field, Collection<String>>> it = real_map
.entrySet().iterator(); it.hasNext();) {
Entry<Field, Collection<String>> entry = it.next();
Field field = entry.getKey();
Collection<String> values = entry.getValue();
String value = null;
if (values != null && !values.isEmpty()) {
ArrayList<String> list = new ArrayList<String>(values);
value = list.get(0);
}
singular_map.put(field, value);
}
}
return singular_map;
}
Or if you do not want to use an extra library, you can create a simple Wrapper class
class Wrap {
String value;
String[] values
}
and have your map use Map<String, Wrap> map, when looping you can then determine either through use of your class methods or just testing, which one of the Wrapper variables are populated
I would use only List<String>. You could maybe add some methods to allow adding a single String and wrap the passed argument using Arrays.asList(...). Using only a single type of objects will reduce the quantity of code to write and avoid many if/else.
Why not create a class
class MyFunkyValue{
private String onlyOneString;
private List<String> stringValues;
public MyFunkyValue(String s){
...
}
public MyFunkyValue(List<String>ls){
...
}
}
and use it like this:
Map<KeyClass,MyFunkyValue> m;