Why can't I do this with the HashMap?


#1



/* Why does this code work and the following similar code doesn't? 
   In the for loop I specified a function Integer and gave it a name 
   and then tried to retrieve it in the print method.
   What's wrong?
*/

	//Major cities and the year they were founded (works)
		HashMap<String, Integer> majorCities = new HashMap<String, Integer>();

		majorCities.put("New York", 1624);
		majorCities.put("London", 43);
		majorCities.put("Mexico City", 1521);
		majorCities.put("Sao Paulo", 1554);

		for (String city : majorCities.keySet()) {

			System.out.println(city + " was found in " + 
                         majorCities.get(city));

		}

	//Major cities and the year they were founded (doesnt work)
		HashMap<String, Integer> majorCities = new HashMap<String, Integer>();

		majorCities.put("New York", 1624);
		majorCities.put("London", 43);
		majorCities.put("Mexico City", 1521);
		majorCities.put("Sao Paulo", 1554);

		for (Integer year : majorCities.keySet()) {

			System.out.println(year + " is the year when " + 
                         majorCities.get(year) + " was found. ");

		}


#2

The answer may be here... http://stackoverflow.com/questions/19922181/java-convert-hashmap-values-to-setinteger


#3

You're pretty much doing this:

Integer age = "blue";

"blue" simply doesn't match that type, and I don't think you'd expect java to react with: oh, did you mean 5? Sure, here you go. No, if you want 5, you would put 5 there.


#4

I would put "5" where? What is the "blue" in this code?
My reasoning for the lower version of the code was that if I set the for loop to Integer and name it year then when I use "year" in the println it would return the integer from the HashMap and the get function would return the string instead.

Why does it have to be a string method in the for loop?
In the HashMap variables we have both string values and integer values

Thank you!


#5

Changing where you store the value doesn't affect what value it is you are storing. The values you assign to your variable Integer year is still the same as what you assigned to String city -- why would a value that represents a city suddenly be a year?


#6

I think here is the confusion.
I run these two blocks of code independently from one another.
Like this is one program and the code underneath after a comment is a different program.

//Major cities and the year they were founded (works)
HashMap majorCities = new HashMap();

	majorCities.put("New York", 1624);
	majorCities.put("London", 43);
	majorCities.put("Mexico City", 1521);
	majorCities.put("Sao Paulo", 1554);

	for (String city : majorCities.keySet()) {

		System.out.println(city + " was found in " + 
                     majorCities.get(city));

	}

I'm just trying to figure out why when I change the for loop to display intger values an error comes out saying I can't convert string into an integer


#7

You have strings. Trying to store them in an integer variable doesn't make sense. You are using the same values, and those values are strings, not int.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.