Results 1 to 6 of 6

Thread: hashmap problem

  1. #1
    minotaurus is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default hashmap problem

    hi. i want to iterate through a hashmap and it works by now. but the output is not as i want. i have input like this:
    a = 3,4
    b = 5,6

    and i want output like this:

    a=3, b=5.
    a=3, b=6.
    a=4, b=5.
    a=4, b=6.

    so every combination is what i need. but i just cant seem to get it...

    i would really appreciate if someone could post a solution for this...

    here is my code:

    Java Code:
    HashMap<String, String[]> map = new HashMap<String, String[]>();
    
            BufferedReader file = new BufferedReader(new FileReader("test.txt"));
            String str;
            while ((str = file.readLine()) != null) { 
    
            ... logic
    
            map.put(key, value);
            }
            System.out.println("number of keys: " + map.size());
            for(Entry<String, String[]> entry : map.entrySet()) {
                        for(String value : entry.getValue()) {
                            System.out.println(entry.getKey() + ": " + value);
                        }
                }
            file.close();

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Java Code:
    Map<String, List<String>> whatever = new HashMap<String, List<String>>();
    // get the input (also in a loop/etc)
    List<String> curList = whatever.get(firstInt);
    if (curList == null) {
        curList = new ArrayList<String>()
        whatever.put(firstInt, curList);
    }
    curList.add(secondInt);
    Last edited by masijade; 03-16-2011 at 10:10 AM.

  3. #3
    minotaurus is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    what exactly tries your code to do? and how will it solve my problem? how shall i use it?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Tell me what you think it does? Once you know what does (even if you need to ask for clarification) then you tell me how you think it could solve your problem.

  5. #5
    minotaurus is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    ok. it looks as you also use a hashmap. string for the keys and the values get into a list of strings. than you create the list naming curlist and fill it with a get-method from the hashmap. as i read one of my integers from my example (they can also be strings, therefore i used String[] in my original hashmap).
    if the list of strings is null, you create an arraylist for this and this is put into the hashmap. otherwise second int is put to the array.

    most probably my example is not clear. the number of keys in my original hashmap and the size of my String[] is not clear. this is textinput. it could also be a = 1,3,6,z. and than i still need every combination. it looks like nested for-loops but one cannot generate for-loops dynamically.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by minotaurus View Post
    ok. it looks as you also use a hashmap. string for the keys and the values get into a list of strings. than you create the list naming curlist and fill it with a get-method from the hashmap. as i read one of my integers from my example (they can also be strings, therefore i used String[] in my original hashmap).
    if the list of strings is null, you create an arraylist for this and this is put into the hashmap. otherwise second int is put to the array.
    Not an array, an arraylist, and the "secondint" is always added to the list.
    most probably my example is not clear. the number of keys in my original hashmap and the size of my String[] is not clear. this is textinput. it could also be a = 1,3,6,z. and than i still need every combination. it looks like nested for-loops but one cannot generate for-loops dynamically.
    It doesn't matter how many there are, that is why you use a map and a list (arraylists grow in size "dynamically"). If you have two lists that need to be combinied in all combinations, it's even easier. Simply cycle through the first list creating the lists and placing them in the hashmap using the "value" out of that first list as the key, then add all elements of the second list to that list contained in the hashmap. IOW, the values from the first list are the keys of the hashmap.

Similar Threads

  1. Problem with HashMap
    By maz09 in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 09:40 PM
  2. problem with removing element from HashMap
    By checho in forum New To Java
    Replies: 15
    Last Post: 01-10-2010, 01:10 PM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. 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
  •