Results 1 to 7 of 7
  1. #1
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default iterating through a hashmap

    I need to iterate through a hash map and I have the following example which works fine
    Java Code:
    public static void printMap(Map mp) {
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
    }
    My problem is that in my program, I actually have a class called Map which is causing the line
    Java Code:
    Map.Entry pairs = (Map.Entry)it.next();
    to cause errors. I am not able to change the name of my Map class. Can anyone see anyway around this

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: iterating through a hashmap

    Use the full class name including the package name to differentiate between your class and the java SE class.


    Using the name of a Java SE class for your class is a bad idea.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Re: iterating through a hashmap

    Thanks Norm, my Map is actually an interface, not a class as i mentioned before, and this has been set by my lecturer.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: iterating through a hashmap

    Did you try using the full name including the package path?

    Post a small, complete (import statements through the ending }) program that shows the problem.


    Copy the full text of the error messages and post it here.
    Last edited by Norm; 10-27-2013 at 01:53 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Re: iterating through a hashmap

    I've actually worked around it another way, much less efficient as what i was trying to do, but will get the job done for this assignment.
    Cheers.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default Re: iterating through a hashmap

    Efficient is such a vague term to use. In a business environment it often means "costs the least" while in a real time processing environment it means "executes with the least amount of interruptions". Programmers like to bind it to code execution speed which is 99/100 time totally irrelevant to the matter at hand. During study time the meaning should be totally different again: efficiency is what your teacher / mentor / guide thinks is correct (from the range of possibilities that in fact will get the job done).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: iterating through a hashmap

    Iterator is a generic type so you should add generic type arguments to your method. Then you would not have to cast the object.
    In general, you should not use a raw type if the class or interface is a generic type. And if you write new code, incorporate generic
    types. It makes your code more robust.

    Java Code:
    public static <K,V> void printMap(Map<K,V> foo) {
       Iterator<Map.Entry<K,V>> it = foo.entrySet().iterator();
       while (it.hasNext()) {
          Map.Entry<K,V> pairs = it.next();
          System.out.println(pairs.getKey() + " = " + pairs.getValue());
          it.remove(); // avoids a ConcurrentModificationException
       }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Problem iterating using Map.Entry for HashMap
    By d0nmin0 in forum New To Java
    Replies: 5
    Last Post: 08-13-2011, 01:46 PM
  2. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  3. Iterating through a hashmap<String, Stinrg[]>
    By verWire3619 in forum Advanced Java
    Replies: 4
    Last Post: 05-17-2011, 05:57 PM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  5. Iterating through HashMap problem
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-28-2009, 11:28 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
  •