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

    Default Changing the values of a map.


    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
    Blog Entries
    Rep Power


    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;
        for (String key : map.keySet()) {
          int value = map.get(key);
          value *= multiplier;
          map.put(key, value);

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-11-2009, 12:35 AM
  2. its not changing bgcolor
    By javanoobita in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:29 PM
  3. Changing the Jframe
    By Nemo1959 in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 04:58 PM
  4. Replies: 0
    Last Post: 05-22-2008, 11:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 02: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