Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKishan Gupta
6,567 PointsMap.Entry<String, String>
So what is the difference between these two, i tried both and both of them works flawlessly
Map<String, String> mMenu = new HashMap<String, String>();
mMenu.put("add", "add a new song to the song book");
mMenu.put("quit", "Give up. Exit the program...");
for (Map.Entry<String, String> option : mMenu.entryset()) {
System.out.printf("%s-%s \n", option.getKey(), option.getValue());
}
Map<String, String> mMenu = new HashMap<String, String>();
mMenu.put("add", "add a new song to the song book");
mMenu.put("quit", "Give up. Exit the program...");
for (Map.Entry option : mMenu.entryset()) {
System.out.printf("%s-%s \n", option.getKey(), option.getValue());
}
1 Answer
Lukas Dahlberg
53,736 PointsThe second example does not use what are called generics. So while it will work just fine, it's usually not a good idea in the real world because you don't get the benefits that come from the compiler's type-checking. The generics also allow you to avoid casting when you get an object from the map.
So it's better to use the former example with a firm declaration of what's in the Map. (In fact, the second version is what people had to do before Java 1.5 was released, so it's fairly outdated code.)
Kishan Gupta
6,567 PointsKishan Gupta
6,567 PointsSystem.out.println("Sounds Legit. Thanks for the Answer :) ");