We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to create a list of countries where each country is supposed to have multiple values and use those values. While trying to access the values I keep getting the wrong datatype. Trying to change the datatype to int doesn't work.
HashMap<String, HashMap> countries = new HashMap<String, HashMap>();
for (int i=0; i<destCountryArray.length; i++) {
HashMap<String, Float> country = new HashMap<String, Float>();
countries.put(destCountryArray[i], country);
country.put("x", i*20.0);
country.put("y", i*10.0);
}
for (Map.Entry hm : countries.entrySet()) {
print(hm.getKey() + " is ");
println(hm.getValue());
}
for (int i = 0; i< destCountryArray.length; i++) {
println("get x = " + (countries.get(destCountryArray[i])).get("x")); // this prints well
//println(int((countries.get(destCountryArray[i])).get("x"))); // this creates an error
//int xSource = 1;
int xSource = (countries.get(destCountryArray[i])).get("x"); // also doesn't work
}
Answers
Can you post a runable example?
And the data?
Do they not teach unit testing any more?
@koogs sorry I'm a designer not a programmer they don't teach us code everything I know is through tutorials and forums like this one :)
Link to the data :) https://www.dropbox.com/s/ubu4ny5aygsjnsr/asylum_seekers.csv?dl=0
I suspect that the problem is that Java is not inferring the datatypes for Map.Entry. If you are going to use generics then I suggest you use them throughout like this.
Which gives the output
what does Vector3D give you that PVector doesn't?
another personal bugbear
those comments would be a lot more useful if you knew they were there but they are far too far to the right and you don't know you need to scroll
one T in Britain.
this
HashMap<String, HashMap<String, Float>>()
is kind of terrible. you know what 'shape' the data is, write a class to hold it rather than using that inner hashmap dogpile.^ some example data (dropbox killed my browser trying to display 129,000 lines of csv in an html table)
for example. this does what your original code does, adds countries with an x and y for each, puts them in a hash, gets them out again. you can add any other data to the class as members, rather than as entries into a hash.
oh ok that makes much more sense, I'm going to try this and report back asap
I'm still confused though as to why the hashmap of hashmaps doesn't return the correct data type
because you don't tell it what type the inner hashmap entries are
vs quark's
when you extract the entries from the outer map it only knows it's a hashmap, not what that hashmap contains.
but you don't really want a hashmap here.