Results 1 to 2 of 2
  1. #1
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default Changing the values of a map.

    Hello,

    I have a variable which is of the type map with integers as keys and integers as values.

    Now I want to write following method:

    Java Code:
    public static void changeValues(int n){
     //multiply the values of the map by n
    }
    I have no idea how to do that. Can anyone help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'd get the keys out of the map via the keySet() method, then iterate through the map using this, getting the values, multiplying them and then putting them back in the map.

    for e.g.,
    Java Code:
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        map.put("Mon", 1);
        map.put("Tues", 2);
        map.put("Wed", 3);
        map.put("Thurs", 4);
        map.put("Fri", 5);
    
        int multiplier = 5;
        System.out.println(map);
        for (String key : map.keySet()) {
          int value = map.get(key);
          value *= multiplier;
          map.put(key, value);
        }
        System.out.println(map);

Similar Threads

  1. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  2. its not changing bgcolor
    By javanoobita in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 02:29 PM
  3. Changing the Jframe
    By Nemo1959 in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:58 PM
  4. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 01:04 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
  •