Results 1 to 5 of 5
  1. #1
    verWire3619 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Iterating through a hashmap<String, Stinrg[]>

    Anyone have an algorithm for iterating through an array stored in a hashmap<String, String[]>?

    Here's how I read an ascii file into the hashamp.

    Map<String,String[]> map = new HashMap<String,String[]>();
    BufferedReader br = null;
    StringTokenizer st = null;
    String str = null;
    String key = null;
    String[] array = new String[4];

    // File format:
    // Hostname IPaddress userid password port
    // . . .

    br = new BufferedReader(new FileReader(cltLst));
    while ((str = br.readLine()) != null) {
    st = new StringTokenizer(str);
    while (st.hasMoreTokens()) {
    key = st.nextToken();
    array[0] = st.nextToken();
    array[1] = st.nextToken();
    array[2] = st.nextToken();
    array[3] = st.nextToken();
    }
    map.put(key,array);


    This does not work:

    while(it.hasNext()) {
    String arrayName = it.next();
    String[] array = map.get(arrayName);
    // String[] array = (String[])map.get(arrayName);
    for(int i=0;i<array.length;i++)
    System.out.println(array[i]);
    }

    This only gives the last entries into the array. It's like I need a second iterator or something for the array values.

    Thank you in advance for any help on this. :)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Your Iteration is not your problem! You only have one array. You have to create more than one array!
    Put String[] array = new String[4]; into your first while-loop!
    Last edited by eRaaaa; 05-17-2011 at 06:17 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Are you sure that there are more entries in the array than you see printed out?
    Your sample code shows 4 entries: 0-3.

    How many are printed out?

  4. #4
    verWire3619 is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Bingo!

    This is strange. I thought because I am assigning new values to the array elements, then writing to my hashmap before I read the next row from the file, that that would do it. Obviously not. I don't create more than one 'String key' in the first while loop. I only make the assignment. Maybe making the assignment creates it. At any rate, that does the trick. Thanks for your help.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    String is immutable. String[] (or any other array) is not.

    Maps and Lists store references, not the content referenced.

    db

Similar Threads

  1. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  2. Iterating through HashMap problem
    By JordashTalon in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 12:28 AM
  3. HashMap String Return
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 04:34 AM
  4. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 05:34 PM
  5. Iterating through ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 09:50 AM

Posting Permissions

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