简体   繁体   English

通过键从 HashMap 获取值

[英]Get value from HashMap by key

I need to compare the roman letters and get the correct integer out of it.我需要比较罗马字母并从中得到正确的整数。 If I'm correct, there should be a way to compare the hashmap key with the arraylist element and if they match, get the associated value from the key.如果我是对的,应该有一种方法可以将 hashmap 键与 arraylist 元素进行比较,如果它们匹配,则从键中获取关联的值。

The return 2020 is there just for test purposes, since I wrote a JUnit test in a different class.返回 2020 仅用于测试目的,因为我在不同的类中编写了 JUnit 测试。 It can be ignored for now.暂时可以忽略。

I hope someone could give me a hint, since I wouldn't like to use the solutions from the web, because I need to get better with algorithms.我希望有人能给我一个提示,因为我不想使用网络上的解决方案,因为我需要更好地使用算法。

package com.company;

import java.util.*;

public class Main {
    static HashMap<String, Integer> romanNumbers = new HashMap<String, Integer>();

    static {
        romanNumbers.put("I", 1);
        romanNumbers.put("V", 5);
        romanNumbers.put("X", 10);
        romanNumbers.put("L", 50);
        romanNumbers.put("C", 100);
        romanNumbers.put("D", 500);
        romanNumbers.put("M", 1000);
    }

    public static void main(String[] args) {
        romanToArabic("MMXX");
    }

    static int romanToArabic(String roman) {
        ArrayList romanLetters = new ArrayList();
        roman = roman.toUpperCase();

        for (int i = 0; i < roman.length(); i++) {
            char c = roman.charAt(i);

            romanLetters.add(c);
        }
        // [M, M, X, X]
        System.out.println(romanLetters);

        // iterates over the romanLetters
        for (int i = 0; i < romanLetters.size(); i++) {
            System.out.println(romanLetters.get(i));
        }

        // retrive keys and values
        for (Map.Entry romanNumbersKey : romanNumbers.entrySet()) {
            String key = (String) romanNumbersKey.getKey();
            Object value = romanNumbersKey.getValue();

            System.out.println(key + " " + value);
        }

        return 2020;
    }
}

You could just Map.get each array element.你可以只Map.get每个数组元素。

package com.company;

import java.util.ArrayList;
import java.util.HashMap;

public class Main {

    static HashMap<String, Integer> romanNumbers = new HashMap<String, Integer>();

    static {
        romanNumbers.put("I", 1);
        romanNumbers.put("V", 5);
        romanNumbers.put("X", 10);
        romanNumbers.put("L", 50);
        romanNumbers.put("C", 100);
        romanNumbers.put("D", 500);
        romanNumbers.put("M", 1000);
    }

    public static void main(String[] args) {
        System.out.println(romanToArabic("MMXX"));
    }

    static int romanToArabic(String roman) {
        ArrayList romanLetters = new ArrayList();
        roman = roman.toUpperCase();

        for (int i = 0; i < roman.length(); i++) {
            char c = roman.charAt(i);

            romanLetters.add(c);
        }
        // [M, M, X, X]
        System.out.println(romanLetters);

        // iterates over the romanLetters
        int sum = 0;
        for (int i = 0; i < romanLetters.size(); i++) {
            String key = String.valueOf(romanLetters.get(i));
            if (romanNumbers.containsKey(key)) {
                sum += romanNumbers.get(key);
            }
        }

        return sum;
    }

}

As stated in the comments, this just answers your question of how to get values from an hashmap where keys are the array elements.正如评论中所述,这只是回答了您如何从键是数组元素的哈希图中获取值的问题。 It is not a solution to calculate the numeric value out of a roman number.从罗马数字中计算数值不是解决方案。 For that, you will have to look at the next letter, and join if it is larger, before consulting the map for the final value to sum.为此,您将不得不查看下一个字母,如果它更大,则在查看地图以获取最终值之前加入。

I'd like to suggest a completly different approach using an enum.我想建议使用枚举的完全不同的方法。

public enum RomanNumber {
    I(1), V(5), X(10), L(50), C(100), D(500), M(1000);

    private final int arabic;

    RomanNumber(int arabic) {
        this.arabic = arabic;
    }

    public int getArabicNumber() {
        return arabic;
    }

    // This is obviously broken. IV wouldn't work for example.
    public static int toArabic(String romanLetters) {
        romanLetters = romanLetters.toUpperCase();
        int arabicResult = 0;
        for (int i = 0; i < romanLetters.length(); i++) {
            char romanNumber = romanLetters.charAt(i);
            // valueOf(String) returns the enum based on its name. So a String of "I" returns the RomanNumber I enum.
            int arabicNumber = valueOf(String.valueOf(romanNumber)).getArabicNumber();
            arabicResult = arabicResult + arabicNumber;
        }
        return arabicResult;
    }
}

Can be used like this:可以这样使用:

String romanNumber = "MMXX";
System.out.println(RomanNumber.toArabic(romanNumber));

Btw.顺便提一句。 every enum has an ordinal() method which returns the declaration position inside the enum.每个枚举都有一个 ordinal() 方法,它返回枚举内的声明位置。 (I.ordinal() == 1 or V.ordinal() == 2) I think this could help you with the IV problem aswell. (I.ordinal() == 1 或 V.ordinal() == 2)我认为这也可以帮助您解决 IV 问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM