Results 1 to 6 of 6
- 08-13-2011, 11:58 AM #1
Member
- Join Date
- May 2008
- Posts
- 12
- Rep Power
- 0
Problem iterating using Map.Entry for HashMap
Hi, I would like advice on alternative solutions to using the 'Map.Entry' for iterating of HashMap
As normally done, this is the coding for iterating through a HashMap:
As such, I have to import this class as well -> import java.util.MapJava Code:Iterator iterator = places.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry pi = (Map.Entry)places.next(); System.out.println(pi.getKey() + " " + pi.getValues()); }
The problem I face at this moment is because I have a Map interface created on my own and is this is clashing with the util.Map class. Is there another way around for this?
A few examples of the errors I get upon implementation of the util.Map class:
MapPanel.java:56: cannot find symbol
symbol : method getPlaces()
location: interface java.util.Map
Set<Place> actualPlaces = map.getPlaces();
^
MapPanel.java:86: cannot find symbol
symbol : method getRoads()
location: interface java.util.Map
Set<Road> actualRoads = map.getRoads();
- 08-13-2011, 12:12 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
If you don't want to rename your Map class, then you can avoid importing java.util.Map by fully qualifying the name, as such:
Alternatively, you can import java.util.Map and fully qualify references to your own Map class.Java Code:java.util.Map m = new java.util.HashMap();
- 08-13-2011, 12:12 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You should avoid giving your classes the same names as classes in the Java library. It makes code more difficult to work with and requires you to use fully qualified names when using some class.
You can also iterate through a map much easier than creating an iteratorJava Code:java.util.Map
Java Code:for(E type : map.keySet()){ System.out.println(type + ": " + map.get(type)); }
- 08-13-2011, 01:23 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You'll find code analysis tools recommending you iterate over the entrySet and not the keySet. It means you don't have to go back to the map to pull out the value for a key.
Java Code:for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey()+ ": " + entry.getValue); }
- 08-13-2011, 01:39 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Thanks for pointing that out. Ignorance on my part; thanks Tolls.
- 08-13-2011, 01:46 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Iterating through a hashmap<String, Stinrg[]>
By verWire3619 in forum Advanced JavaReplies: 4Last Post: 05-17-2011, 05:57 PM -
hashmap problem
By minotaurus in forum Advanced JavaReplies: 5Last Post: 03-16-2011, 11:24 AM -
Problem with HashMap
By maz09 in forum New To JavaReplies: 2Last Post: 04-14-2010, 09:40 PM -
Iterating through HashMap problem
By JordashTalon in forum New To JavaReplies: 1Last Post: 01-28-2009, 11:28 PM -
Hibernate Duplicate Entry problem
By mc1392 in forum Advanced JavaReplies: 0Last Post: 09-02-2008, 10:03 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks