如何在java中迭代Map?
2022-09-02 05:22:41
我需要迭代一个并获取所有内容,但是我如何获得类似的东西,例如,当我在这里尝试时,无需手动执行此操作:BucketMap
keys
buckets[i].next.next.next.key
public String[] getAllKeys() {
//index of string array "allkeys"
int j = 0;
String allkeys[] = new String[8];
//iterates through the bucketmap
for (int i = 0; i < buckets.length; i++) {
//checks wether bucket has a key and value
if (buckets[i] != null) {
//adds key to allkeys
allkeys[j] = buckets[i].key;
// counts up the allkeys index after adding key
j++;
//checks wether next has a key and value
if (buckets[i].next != null) {
//adds key to allkeys
allkeys[j] = buckets[i].next.key;
j++;
}
}
}
return allkeys;
}
另外,我如何初始化使用迭代完成后我们得到的版本作为索引?String[] allkeys
j