[英]Why it returns null, but not String in HashMap? java
How could I get values from Map?我如何从 Map 中获取值? I study Collections and wanna leave that order <String, Integer>.
我研究 Collections 并想保留 <String, Integer> 的顺序。 I tried before <Integer, String> and in loop I put res.append(map.get(num));
我之前尝试过 <Integer, String> 并在循环中放入res.append(map.get(num)); — that's help me.
——那是帮我。 But have no clue how to do it in reverse order.
但是不知道如何以相反的顺序进行操作。 Also I wrote 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..
我也写了https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object- ,但我确切地知道我有一个密钥..
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();
}
}
Traversing a HashMap
is pretty straightforward.遍历
HashMap
非常简单。 First of all you have to understand that the first Class in the <>
diamond brackets is the KEY
and the second is the VALUE
.首先,您必须了解
<>
菱形括号中的第一个 Class 是KEY
,第二个是VALUE
。
So if I create a new HashMap<Integer, String>
the elements will be of the following form所以如果我创建一个新的
HashMap<Integer, String>
元素将是以下形式
1 -> "str",
2 -> "str2"
....
To traverse this HashMap
And we want only the keys
this is the way to do it:遍历这个
HashMap
并且我们只需要keys
这是这样做的方式:
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.我仍然确定您正在尝试转换罗马数字中的
integers
。 This already has a solution here .这在这里已经有了解决方案。
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:在当前实现中,您需要迭代
Map::entrySet
方法返回的映射条目,以便在找到值后立即使用该键:
// ...
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:或者,如果实现了反向映射
Map<Integer, String> map
则应迭代keySet
:
for (Integer number2: map.keySet()) {
while (number2 <= value) {
stringBuilder.append(map.get(number2));
value -= number2;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.