用HashMap在Java中比较带有整数键的关键项

huangapple go评论108阅读模式
英文:

Comparing Key items with an integer using Hasmap in Java

问题

  1. public class Translate {
  2. Map<Integer, String> map = new HashMap<>();
  3. private int number;
  4. {
  5. map.put(0, "zero");
  6. map.put(1, "one");
  7. map.put(2, "two");
  8. map.put(3, "three");
  9. map.put(4, "four");
  10. map.put(5, "five");
  11. map.put(6, "six");
  12. map.put(7, "seven");
  13. map.put(8, "eight");
  14. map.put(9, "nine");
  15. }
  16. public Translate(int number) {
  17. this.number = number;
  18. }
  19. // This is where I'm stuck. I need to compare the Key item to see if it's equal to the integer that is in the Translate constructor.
  20. public String convertToString() {
  21. if (map.containsKey(number)) {
  22. return map.get(number);
  23. }
  24. return "";
  25. }
  26. }
英文:

I'm trying to compare the Key item to see if is equal to the integer that is in the Translate constructor. But someone I can't really figure it out. Sorry for the mess and I' still fairly new to Java. Thanks

  1. public class Translate {
  2. Map&lt;Integer, String&gt; map = new HashMap&lt;&gt;();
  3. private int number;
  4. map.put(0, &quot;zero&quot;);
  5. map.put(1, &quot;one&quot;);
  6. map.put(2, &quot;two&quot;);
  7. map.put(3, &quot;three&quot;);
  8. map.put(4, &quot;four&quot;);
  9. map.put(5, &quot;five&quot;);
  10. map.put(6, &quot;six&quot;);
  11. map.put(7, &quot;seven&quot;);
  12. map.put(8, &quot;eight&quot;);
  13. map.put(9, &quot;nine&quot;);
  14. public Translate(int number) {
  15. this.number = number;
  16. }
  17. //this is where I&#39;m stuck. I need to compare the Key item to see if is it equal to the integer that is in the Translate constructor.
  18. public String convertToString() {
  19. if (map.get(number) == this.number) {
  20. };
  21. }
  22. }

答案1

得分: 2

  1. 你可以尝试类似这样的写法
  2. import java.util.*;
  3. public class Translate {
  4. private static Map<Integer, String> map;
  5. static {
  6. map = new HashMap<>();
  7. map.put(0, "zero");
  8. map.put(1, "one");
  9. map.put(2, "two");
  10. map.put(3, "three");
  11. map.put(4, "four");
  12. map.put(5, "five");
  13. map.put(6, "six");
  14. map.put(7, "seven");
  15. map.put(8, "eight");
  16. map.put(9, "nine");
  17. }
  18. private int number;
  19. public Translate(int number) {
  20. this.number = number;
  21. }
  22. public String convertToString() {
  23. if (map.containsKey(this.number)) {
  24. return map.get(this.number);
  25. }
  26. return null;
  27. }
  28. public static void main(String[] args) {
  29. Translate t = new Translate(7);
  30. System.out.println(t.convertToString());
  31. }
  32. }
英文:

You have to try something like this:

  1. import java.util.*;
  2. public class Translate {
  3. private static Map&lt;Integer, String&gt; map;
  4. static {
  5. map = new HashMap&lt;&gt;();
  6. map.put(0, &quot;zero&quot;);
  7. map.put(1, &quot;one&quot;);
  8. map.put(2, &quot;two&quot;);
  9. map.put(3, &quot;three&quot;);
  10. map.put(4, &quot;four&quot;);
  11. map.put(5, &quot;five&quot;);
  12. map.put(6, &quot;six&quot;);
  13. map.put(7, &quot;seven&quot;);
  14. map.put(8, &quot;eight&quot;);
  15. map.put(9, &quot;nine&quot;);
  16. }
  17. private int number;
  18. public Translate(int number) {
  19. this.number = number;
  20. }
  21. public String convertToString() {
  22. if (map.containsKey(this.number)) {
  23. return map.get(this.number);
  24. }
  25. return null;
  26. }
  27. public static void main(String[] args) {
  28. Translate t = new Translate(7);
  29. System.out.println(t.convertToString());
  30. }
  31. }

答案2

得分: 1

我不是 Java 的专家,但以下是我发现有问题的部分:

  1. 如果 (map.get(number).equals(this.number)) <--- 这里你正在获取值,而不是键。

    而应该使用 map.contains() 进行检查,如果返回为 true,则返回你传入的键的值。

  2. 你永远不应该使用 "==" 来比较对象,而应该使用该集合的 .equals() 方法,如果有的话。

PS:如果我写错了什么内容,欢迎提供反馈。

英文:

I'm not the expert on java but here is what i found troublesome

if (map.get(number).equals(this.number)) <--- 1.you are fetching the value not the key in this.

Instead check with map.contains() , if returned true by it, return the value of the key you passed.

  1. You should never compare objects with "==" but should instead use .equals() method of that collection , if it is there.

PS: Open for feedback if I wrote something wrong

答案3

得分: 0

  1. public String convertToString() {
  2. // 只需要判断映射中是否包含这个键,可以使用 containsKey 方法
  3. if (map.containsKey(this.number)) {
  4. // 进行其他你想要的操作
  5. }
  6. }

或者,如果你要在映射中存在值的情况下使用该值,可以省略 containsKey 直接使用 get

  1. public String convertToString() {
  2. // 返回映射中键为 this.number 的值,如果不存在则返回一个提示信息
  3. return map.getOrDefault(this.number, "找不到 " + this.number + "!");
  4. }
英文:
  1. public String convertToString() {
  2. // You just need to see if the map contains this key, so you can use containsKey
  3. if (map.containsKey(this.number)) {
  4. // Do whatever else you want to do
  5. }
  6. }

Alternatively, if you will use the value in the map if it exists, you can skip the containsKey and just go straight to get

  1. public String convertToString() {
  2. // Return either the value in the map with key of this.number, or a message that
  3. // indicates that no value is present
  4. return map.getOrDefault(this.number, &quot;Couldn&#39;t find &quot; + this.number + &quot;!&quot;);
  5. }

答案4

得分: 0

你应该首先调用Translate方法并传入任何值。或者,你可以给数字赋予默认值,比如 int number = 5;

然后你可以使用以下代码块:

  1. public String convertToString() {
  2. if (map.containsKey(this.number)) {
  3. return map.get(this.number);
  4. }
  5. return null;
  6. }

接着,打印该值:System.out.println(t.convertToString());

英文:

You should call Translate method with any value at first. Or, you can give default value to number, such as int number =5;

Then you can use

  1. public String convertToString() {
  2. if (map.containsKey(this.number)) {
  3. return map.get(this.number);
  4. }
  5. return null;
  6. } method.

Then, print the value System.out.println(t.convertToString());

答案5

得分: 0

请尝试以下代码:

  1. public class Translate {
  2. private int number;
  3. Map<Integer, String> map = Map.of(0, "zero", 1, "one", 2, "two", 3, "three", 4, "four", 5, "five", 6, "six", 7, "seven", 8, "eight", 9, "nine");
  4. public Translate(int number) {
  5. this.number = number;
  6. }
  7. public String convertToString() {
  8. return map.get(number); // 如果没有对应的条目,将返回null
  9. }
  10. }

以上代码如果没有对应的条目,将返回null。或者,您也可以直接返回数字本身:

  1. public String convertToString() {
  2. return map.getOrDefault(number, Integer.toString(number));
  3. }

如果您想要严格一些,可以在构造函数中针对无效的数字抛出异常:

  1. public Translate(int number) {
  2. if (!map.containsKey(number)) {
  3. throw new IllegalArgumentException("number must be 0-9");
  4. }
  5. this.number = number;
  6. }
英文:

Try this:

  1. public class Translate {
  2. private int number;
  3. Map&lt;Integer, String&gt; map = Map.of(0, &quot;zero&quot;, 1, &quot;one&quot;, 2, &quot;two&quot;, 3, &quot;three&quot;, 4, &quot;four&quot;, 5, &quot;five&quot;, 6, &quot;six&quot;, 7, &quot;seven&quot;, 8, &quot;eight&quot;, 9, &quot;nine&quot;);
  4. public Translate(int number) {
  5. this.number = number;
  6. }
  7. public String convertToString() {
  8. return map.get(number); // returns null if there&#39;s no entry
  9. }
  10. }

Theabove code rerurns null if there's no entry for the number. Alternatively, you could just return the number itself.

  1. public String convertToString() {
  2. return map.getOrDefault(number, Integer.toString(number));
  3. }

If you wanted to be strict, you could throw an exception if an invalid number is passed to the constructor:

  1. public Translate(int number) {
  2. if (!map.containsKey(number)) {
  3. throw new IllegalArgumentException(&quot;number must be 0-9&quot;);
  4. }
  5. this.number = number;
  6. }

huangapple
  • 本文由 发表于 2020年8月31日 12:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63664859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定