11.HashMap:Access


#1



Hello.Why dont we use brackets before and after our "System.out.ptintln" code here.In some previous exercises we had to



#2

@warriorcodes,
Could you give code-samples....


#3

import java.util.HashMap;

public class Restaurant {
	public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new  HashMap<String,Integer>();
    restaurantMenu.put("Turkey Burger", 13);
    restaurantMenu.put("Naan Pizza", 11);
    restaurantMenu.put("Cranberry Kale Salad", 10); 
      System.out.println(restaurantMenu.get("Naan Pizza"));
    

	}
}

import java.util.ArrayList;

public class TemperaturesForEach {
	public static void main(String[] args) {

		ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
		weeklyTemperatures.add(78);
		weeklyTemperatures.add(67);
		weeklyTemperatures.add(89); 
		weeklyTemperatures.add(94);
		
		for (Integer temperature : weeklyTemperatures) {
			System.out.println(temperature);
		}
	
	}
}

Notice the HashMap "println" and the for each "println"


#4

In the HashMap println,

System.out.println(restaurantMenu.get("Naan Pizza"));

you are printing the value associated with "Naan Pizza". Since it is an integer 11, you can directly print it.

Objects (like ArrayList, or even normal arrays) if printed using System.out.println, like this:

System.out.println(weeklyTemperatures);

would print the memory reference of that object and not its value, which you generally need. So, you need to loop over their individual elements and print them, like you are doing in that ArrayList example:

for (Integer temperature : weeklyTemperatures) { // looping
    System.out.println(temperature);
}