1

How could I get values from Map? I study Collections and wanna leave that order <String, Integer>. I tried before <Integer, String> and in loop I put res.append(map.get(num)); — that's help me. But have no clue how to do it in reverse order. Also I red https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object-, but I know exactly that I have a key..

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        int x = 555;
        String result = romanianConverter(x);
        System.out.println(result);
    }

    public static String romanianConverter(int value) {
        Map<String, Integer> map = new LinkedHashMap<>();
//        map.put(1000, "M");
//        map.put(900,"CM");
//        map.put(500,"D");
//        map.put(400,"CD");
//        map.put(100,"C" );
//        map.put(90,"XC" );
//        map.put(50,"L" );
//        map.put(40,"XL");
//        map.put(10,"X" );
//        map.put(9,"IX" );
//        map.put(5,"V" );
//        map.put(4,"IV" );
//        map.put(1,"I" );
        map.put("M", 1000);
        map.put("CM", 900);
        map.put("D", 500);
        map.put("CD", 400);
        map.put("C", 100);
        map.put("XC", 90);
        map.put("L", 50);
        map.put("XL", 40);
        map.put("X", 10);
        map.put("IX", 9);
        map.put("V", 5);
        map.put("IV", 4);
        map.put("I", 1);

        StringBuilder stringBuilder = new StringBuilder("Result = ");

        for (int number2 : map.values()) {
            while (number2 <= value) {
                stringBuilder.append(map.keySet().add(number2));
                value -= number2;
            }
        }
        return stringBuilder.toString();
    }
}
OverBuffer
  • 11
  • 2
  • Please provide code that compiles. – Scary Wombat Oct 20 '21 at 06:51
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). I'm afraid it's hard to make out what you're asking here. – T.J. Crowder Oct 20 '21 at 06:52

2 Answers2

0

Traversing a HashMap is pretty straightforward. First of all you have to understand that the first Class in the <> diamond brackets is the KEY and the second is the VALUE.

So if I create a new HashMap<Integer, String> the elements will be of the following form

1 -> "str",
2 -> "str2"
....

To traverse this HashMap And we want only the keys this is the way to do it:

Map<String, Object> map = ...;

for (Integer key : map.keySet()) {
    // ...
}

If we want only the values:

for (String value : map.values()) {
    // ...
}

If we want them both:

for (Map.Entry<Integer, String> entry : map.entrySet()) {
    Integer key = entry.getKey();
    String value = entry.getValue();
    // ...
}

Still I am sure you are trying to conver integers in Roman Numerals. This already has a solution here.

Renis1235
  • 4,116
  • 3
  • 15
  • 27
0

In current implementation you need to iterate the map entries returned by Map::entrySet method to use the key as soon as the value is found:

// ...
    for (Map.Entry<String, Integer> me: map.entrySet()) {
        int number2 = me.getValue();
        while (number2 <= value) {
            stringBuilder.append(me.getKey());
            value -= number2;
        }
    }
// ...

Or, if the reverse map is implemented Map<Integer, String> map a keySet should be iterated:

        for (Integer number2: map.keySet()) {
            while (number2 <= value) {
                stringBuilder.append(map.get(number2));
                value -= number2;
            }
        }

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42