Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: Problem iterating using Map.Entry for HashMap

  1. #1
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default 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:
    Java Code:
    Iterator iterator = places.entrySet().iterator();
    		while (iterator.hasNext()) {
    			Map.Entry pi = (Map.Entry)places.next();
    			System.out.println(pi.getKey() + " "  + pi.getValues());
    		}
    As such, I have to import this class as well -> import java.util.Map

    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();

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    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:
    Java Code:
    java.util.Map m = new java.util.HashMap();
    Alternatively, you can import java.util.Map and fully qualify references to your own Map class.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.
    Java Code:
    java.util.Map
    You can also iterate through a map much easier than creating an iterator

    Java Code:
    for(E type : map.keySet()){
      System.out.println(type + ": " + map.get(type));
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Quote Originally Posted by sunde887 View Post
    You can also iterate through a map much easier than creating an iterator

    Java Code:
    for(E type : map.keySet()){
      System.out.println(type + ": " + map.get(type));
    }
    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);
    }
    sunde887 likes this.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Thanks for pointing that out. Ignorance on my part; thanks Tolls.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    I only really remember it because Findbugs used to always point it out at one place I worked.
    Annoying thing that it can be sometimes...

Similar Threads

  1. Iterating through a hashmap<String, Stinrg[]>
    By verWire3619 in forum Advanced Java
    Replies: 4
    Last Post: 05-17-2011, 05:57 PM
  2. hashmap problem
    By minotaurus in forum Advanced Java
    Replies: 5
    Last Post: 03-16-2011, 11:24 AM
  3. Problem with HashMap
    By maz09 in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 09:40 PM
  4. Iterating through HashMap problem
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-28-2009, 11:28 PM
  5. Hibernate Duplicate Entry problem
    By mc1392 in forum Advanced Java
    Replies: 0
    Last Post: 09-02-2008, 10:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •