简体   繁体   English

检索哈希图的键

[英]Retrieving keys of a hashmap

Is there any way to retreive the keys of a hashmap other then using keyset? 除了使用键集以外,还有什么方法可以检索哈希图的键? I have written the following code , I have a hashmap named map, it includes integer keys and double values : 我编写了以下代码,我有一个名为map的哈希表,它包含整数键和双精度值:

    Set<Integer> keys = sorted_map.keySet();
    Object[] weel = new Object[keys.size()];
    Iterator it = keys.iterator();
    int l = 0;
    while(it.hasNext())
    {
        weel[l] = it.next();
    }

Now I have an array that includes the keys of my map. 现在,我有了一个包含地图键的数组。 but now I need to compare these keys with some integer. 但是现在我需要将这些键与某个整数进行比较。 for example : 例如 :

              if(weel[1] == 5) 

but as the weel type is Object, I can not do the above and also I cannot cast it to int. 但是由于weel类型是Object,因此我无法执行上述操作,也无法将其强制转换为int。 how can I do this? 我怎样才能做到这一点? is it possible? 可能吗?

Not sure why you want to create a copy of the keys give you can just use the keys. 不确定为什么要创建密钥副本,您可以只使用密钥。

You can do 你可以做

List<Integer> keys = new ArrayList<Integer>(sorted_map.keySet());
if (kyes.get(1) == 5)

or 要么

Integer[] keys = (Integer[]) sorted_map.keySet()
                 .toArray(new Integer[sorted_map.size()]);
if (kyes[1] == 5)

or 要么

Iterator<Integer> iter = sorted_map.keySet().iterator();
iter.next(); // skip the first.
if(iter.next() == 5);

Why don't you declare your array as an Integer[] or int[] instead? 为什么不将数组声明为Integer[]int[] In the latter case, you can even use == for comparison. 在后一种情况下,您甚至可以使用==进行比较。 However, you also implicitly use unboxing in that case, which might affect performance if you have a huge map. 但是,在这种情况下,您也会隐式使用拆箱,如果地图很大,这可能会影响性能。

Your keyset is of type Integer (not int) as you have correctly denoted with Set. 您的键集类型为Integer(不是int),因为已正确用Set表示。 So what you are getting out of the array are objects of type Integer, which cannot be cast to an int. 因此,您要从数组中删除的是类型为Integer的对象,该类型不能转换为int类型。 What you should do is create an array of type Integer rather than Object, then when you do your comparison you can use the intValue() method of Integer to get it as an int. 您应该做的是创建一个类型为Integer而不是Object的数组,然后进行比较时,可以使用Integer的intValue()方法将其作为int值获取。

Firstly you don't need to use an iterator to populate the array you can call the .toArray() method on the keyset like so: 首先,您不需要使用迭代器来填充数组,您可以像这样在键集上调用.toArray()方法:

sortedMap.keySet().toArray();

Then all you have to do is cast each element of the object array to an integer as you use it (Or you could cast the array to an integer array when creating it) 然后,您所要做的就是将对象数组的每个元素转换为使用时的整数(或者您可以在创建数组时将其转换为整数数组)

if (((Integer)weel[1]0 == 5){
}

Also you might want to note that your if statment only contains one = sign thats and assignment not a comparison. 另外,您可能要注意,您的if语句仅包含一个=多数民众赞成在和分配而不是一个比较。

EDIT: 编辑:

On reflection you can also you the toArray Method that actually returns a typed array: 在反射时,您还可以使用toArray方法,该方法实际上返回一个类型化数组:

Set<Integer> keySet = new sortedMap.keySet();       
Integer[] weel = {};
weel = keySet.toArray(weel);

you will then have an array of integers which you can compare to any other integer. 您将拥有一个整数数组,可以将其与任何其他整数进行比较。

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

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