简体   繁体   English

有没有一种方法可以在Integer对象之间用Java中的HashMap值执行加/减?

[英]Is there a way to perform addition/subtraction with HashMap values in Java among Integer objects?

    public static HashMap makeHashMap() {
        HashMap<String, Integer> table = new HashMap<String, Integer>();
        table.put("C", 1);
        table.put("V", 2);
        table.put("D", 3);
        table.put("W", 4);
        table.put("E", 5);
        table.put("F", 6);
        table.put("X", 7);
        table.put("G", 8);
        table.put("Y", 9);
        table.put("A", 10);
        table.put("Z", 11);
        table.put("B", 12);
        // System.out.print(table);

        return table;
    }

    public static ArrayList<Object> makeArrayList(HashMap<String, Integer> table) {
        Object[] keys = table.keySet().toArray();
        ArrayList<Object> randomlyGeneratedNotes = new ArrayList<Object>();
        for (int i = 0; i < 83; i++) {
            Object key = keys[new Random().nextInt(keys.length)];
            //  System.out.println(key + " " + table.get(key));
            randomlyGeneratedNotes.add(key);
        }
        System.out.println(randomlyGeneratedNotes);
        return randomlyGeneratedNotes;
    }

    public static void pitchMovement(ArrayList<Object> randomlyGeneratedNotes, HashMap table) {
      Iterator<String> keySetIterator = table.keySet().iterator();
        String key = keySetIterator.next();
        int first = table.get(key);

        for (Object o : randomlyGeneratedNotes) {
            //you want to grab the values of 2 neighboring keys
            if (table.containsKey(o)) {
              //  System.out.println("true");
                Object firstNote=  table.get(key);
                System.out.println(firstNote);
            }
        }
    }
}

How can I perform addition/subtraction between my HashMap values? 如何在我的HashMap值之间执行加/减? Or did I use an inappropriate data structure? 还是我使用了不合适的数据结构? I tried doing something my pitchMovement method, but the int first = table.get(key); 我尝试做一些我的pitchMovement方法,但是int first = table.get(key); line gives an error of incompatible types: 行给出了不兼容类型的错误:

Object cannot be converted to int. 无法将对象转换为int。

Not sure if this is a simple error or a more complex one. 不知道这是一个简单的错误还是一个更复杂的错误。

I'd like to encourage you to put some learning effort into understanding generics and interfaces. 我想鼓励您付出一些学习努力来理解泛型和接口。 Java will be friendly in its conversion between primitives and value types (like int and Integer ), but plays dumb when dealing with objects of type Object . Java将很友好地在基元和值类型(例如intInteger )之间进行转换,但是在处理Object类型的Object时会显得愚蠢。 The problem you're encountering in multiple places is that you're needlessly letting your strongly typed values degrade to type Object . 您在多个地方遇到的问题是,不必要地让强类型化的值降级为Object

I'd suggest the following changes: 我建议进行以下更改:

Change public static HashMap makeHashMap() to public static HashMap<String,Integer> . public static HashMap makeHashMap()更改为public static HashMap<String,Integer> Otherwise, you'll get back the equivalent of HashMap<Object,Object> , which you don't want. 否则,您将获得不需要的HashMap<Object,Object>的等效项。

In public static ArrayList<Object> makeArrayList(HashMap<String, Integer> table) , you're extracting the String typed keys from HashMap, and then converting them to type Object when building your array. public static ArrayList<Object> makeArrayList(HashMap<String, Integer> table) ,您将从HashMap中提取String类型的键,然后在构建数组时将其转换为Object类型。 Don't change their type-- keep them as String. 不要更改它们的类型-将它们保留为String。 You may have been thrown by "toArray()" converting to an array of Object values. 您可能已被“ toArray()”转换为Object值数组而抛出。 You can use this technique to preserve the type: 您可以使用此技术来保留类型:

    String[] keys=new String[table.size()];
    keys=table.keySet().toArray(keys);

In doing so, you can easily change the signature of makeArrayList to return an ArrayList of type String : 这样,您可以轻松更改makeArrayList的签名以返回String类型的ArrayList:

  public static ArrayList<String> makeArrayList(HashMap<String, Integer> table)

Your pitchMovement signature could be changed to: 您的pitchMovement签名可以更改为:

  public static void pitchMovement(ArrayList<String> randomlyGeneratedNotes,
                                   HashMap<String,Integer> table)

Note that the specific error you encountered related to HashMap table being defined without generic parameters (which is equivalent to HashMap<Object,Object> ). 请注意,您遇到的与HashMap table相关的特定错误是在没有通用参数的情况下定义的(等效于HashMap<Object,Object> )。

When your types haven't been reduced to Object , then Java will use "auto-boxing" to automatically convert between your int and Integer types as needed. 当您的类型尚未简化为Object ,Java将根据需要使用“自动装箱”在intInteger类型之间自动转换。 The following, for example, is valid. 例如,以下内容有效。

    Integer x=new Integer(5);
    int y=10+x;
    Integer z=x+y;

In sum, math operations will work as you'd expect without you having to cast and convert between primitives and their equivalent value types, no matter where you store them, but you must take efforts to keep those types preserved wherever you use them. 总而言之,无论在何处存储原语及其等效值类型,数学操作都可以按您期望的方式进行,而不必在原语及其等效值类型之间进行转换和转换,但是无论在何处使用它们,都必须努力保持这些类型的保留。

Try Integer first = table.get(key); 首先尝试Integer = table.get(key);

as table is a map of String and Integer, you can get the value as Integer. 由于table是String和Integer的映射,因此您可以将值获取为Integer。 You can then convert your Integer variable to int. 然后,您可以将Integer变量转换为int。

您需要使用通用参数更新方法签名:

public static void pitchMovement(ArrayList<Object> randomlyGeneratedNotes, HashMap<String, Integer> table)

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

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