简体   繁体   English

使用地图确定键的数据类型-Java

[英]Determine the data type of a key with a map - Java

I have a method with a map as a passed in parameter. 我有一个方法,将地图作为传入参数。 The key for the map will either be a Long or an Integer data type. 映射的键将是Long或Integer数据类型。

I am using the method map.containsKey() to check to see if a number being randomly generated as primitive type int is contained in the passed in map. 我正在使用map.containsKey()方法来检查传入的映射中是否包含作为原始类型int随机生成的数字。

The problem occurs when the map's key is of type Long and map.containsKey() evaluates to false. 当地图的键的类型为Long且map.containsKey()计算为false时,会发生此问题。 Even if the map contains that int key value in Long form. 即使映射包含Long形式的int键值。

However if I try and cast the int to a long, the map expecteing an Integer will now have the same error. 但是,如果我尝试将int强制转换为long类型,则期望一个Integer的映射现在将具有相同的错误。

I was wondering if there is a way to get the data type of a Map's key and then cast the generated number to that type so I can then use the map.containsKey() method? 我想知道是否有一种方法来获取Map键的数据类型,然后将生成的数字转换为该类型,以便随后可以使用map.containsKey()方法?

I understand and wish I could just use a long as the key value for both maps, however that is not within the requirements of the assignment. 我理解并希望我可以将long用作两个地图的键值,但这不在分配要求之内。

Appreciate any help. 感谢任何帮助。

Here is the code and the fields I am using 这是我正在使用的代码和字段

private TreeMap<Integer, LabClass> classes;
private HashMap<Long, Member> members;

public long generateUniqueNumber(AbstractMap map, int low, int high)
{
    Random rand = new Random();
    int number = OUT_OF_BOUNDS;
    int max = high - low + 1;

    if (map.size() < max)
    {
        do {
            number = rand.nextInt(max) + low;
        } while (map.containsKey(number) );
    }

    return number;
}

Just use Long as the key type and then there would be no need for casting. 只需使用Long作为键类型,然后就无需进行强制转换。 (Maybe you have a reason for not using Long as the key) (也许您有理由不使用Long作为键)

You can get a data type from an Object by using .getClass() and check if it is an integer or a long. 您可以使用.getClass()从对象获取数据类型,并检查它是整数还是长整数。

Unless you are doing something very big and you are trying to conserve data, you should cast all the integers to longs when adding them to the map. 除非您做的事情非常大并且要保存数据,否则在将它们添加到地图时,应将所有整数都强制转换为long。 Remember, an integer can be a long, but a long can not be an integer unless the long is within the integer's max size. 请记住,整数可以是一个长整数,但是长整数不能是整数,除非该整数在整数的最大大小内。

The easiest solution would be to change your while statement: 最简单的解决方案是更改您的while语句:

while (map.containsKey(number) || map.containsKey((long) number))

Since the keys might be Integers or might be Longs, you might as well check if either type of key is present. 由于密钥可能是整数,也可能是长整数,因此您最好检查是否存在这两种密钥。

Update: The contract for the containsKey method states that it may throw a ClassCastException "if the key is of an inappropriate type for this map." 更新: containsKey方法协定指出,“如果密钥的类型不适合此映射,则可能抛出ClassCastException”。 So my answer is not viable. 所以我的答案不可行。

for (Long key : map.keySet()) {
     // check if int i == (int) (long) key;
}

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

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