在map中,java集合- keyset() vs entrySet()。

时间:2021-08-27 16:22:09

I put a string array elements is a map where elements of string array is key and frequency of word is value, e.g.:

我放了一个字符串数组元素是一个map,其中string数组的元素是key,而word的频率是value,例如:

String[] args = {"if","it","is","to","be","it","is","up","me","to","delegate"};

then the map will have entries like [ if:1, it:2 .... ]

那么地图将条目[如果:1,:2 ....]

Set<String> keys = m.keySet();
System.out.println("keyset of the map : "+keys);

prints all keys: "if","it","is","to","be","it","is","up","me","to","delegate"

打印所有的钥匙:“如果”,“它”,“是”,“”,“”,“它”,“是”,“上”、“我”、“到”、“委托”

Set<Map.Entry<String, Integer>> entrySet = m.entrySet();
Iterator<Map.Entry<String, Integer>> i = entrySet.iterator();
while(i.hasNext()){
    Map.Entry<String, Integer> element = i.next();
    System.out.println("Key: "+element.getKey()+" ,value: "+element.getValue());
}

prints all key values pairs :

打印所有键值对:

Using entry set prints all values:

使用条目集打印所有值:

Key: if ,value: 1
Key: it ,value: 2
Key: is ,value: 2
Key: to ,value: 2
Key: be ,value: 1
Key: up ,value: 1
Key: me ,value: 1
Key: delegate ,value: 1

But the block of code below should print exactly the same output as above, but it does not:

但是下面的代码块应该打印与上面相同的输出,但它没有:

Iterator<String> itr2 = keys.iterator();
while(itr2.hasNext()){
    //System.out.println(itr1.next()+" ");
    //System.out.println(m.get(itr1.next())+" ");
    System.out.println("Key: "+itr2.next()+" ,value: "+m.get(itr2.next()));
}

It prints:

它打印:

Key: if ,value: 2
Key: is ,value: 2
Key: be ,value: 1
Key: me ,value: 1

But if we uncomment line 1 in the while loop i.e

但是如果我们在while循环中取消第一行的注释。

System.out.println(itr1.next()+" ");

and comment the line

和评论

System.out.println("Key: "+itr2.next()+" ,value: "+m.get(itr2.next()));

Then we get all keys: {"if","it","is","to","be","it","is","up","me","to","delegate"};

然后我们把所有钥匙:{“如果”、“它”、“是”,“”,“”,“它”,“是”,“上”,“我”,“”,“委托”};

If we use m.get() with itr2.next(), then the iterator does not have few keys!

如果我们使用mget()和itr2.next(),那么迭代器就没有几个键!

5 个解决方案

#1


43  

Every call to the Iterator.next() moves the iterator to the next element. If you want to use the current element in more than one statement or expression, you have to store it in a local variable. Or even better, why don't you simply use a for-each loop?

每次调用迭代器。next()将迭代器移动到下一个元素。如果要在多个语句或表达式中使用当前元素,则必须将其存储在局部变量中。或者更好的是,为什么不简单地使用for-each循环呢?

for (String key : map.keySet()) {
    System.out.println(key + ":" + map.get(key));
}

Moreover, loop over the entrySet is faster, because you don't query the map twice for each key. Also Map.Entry implementations usually implement the toString() method, so you don't have to print the key-value pair manually.

此外,在entrySet上的循环会更快,因为您不会为每个键查询两次映射。也映射。Entry实现通常实现toString()方法,因此您不必手工打印键值对。

for (Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry);
}

#2


3  

Every time you call itr2.next() you are getting a distinct value. Not the same value. You should only call this once in the loop.

每一次你叫它2.next()你得到的是一个不同的值。不一样的价值。您应该只在循环中调用它一次。

Iterator<String> itr2 = keys.iterator();
    while(itr2.hasNext()){
        String v = itr2.next();
        System.out.println("Key: "+v+" ,value: "+m.get(v));
    }

#3


2  

Traversal over the large map entrySet() is much better than the keySet(). Check this tutorial how they optimise the traversal over the large object with the help of entrySet() and how it helps for performance tuning.

遍历大型映射entrySet()比keySet()要好得多。请查看本教程,了解如何在entrySet()的帮助下对大型对象进行遍历,以及它如何帮助进行性能调优。

#4


1  

An Iterator moves forward only, if it read it once, it's done. Your

迭代器只向前移动,如果它读取一次,就完成了。你的

m.get(itr2.next());

is reading the next value of itr2.next();, that is why you are missing a few (actually not a few, every other) keys.

下一个值是读取下一个值,这就是为什么你缺了几个(实际上不是几个)键的原因。

#5


0  

Hi @NINCOMPOOP to make things simple , please note that every time you do itr2.next() the pointer moves to the next element,i.e., here if you notice carefully, then the output is perfectly fine according to the logic you have written .

Hi @NINCOMPOOP让事情变得简单,请注意每次你做itr2.next()时,指针移动到下一个元素,即。在这里,如果你仔细地注意到,那么根据你所写的逻辑,输出是完美的。

This may help you in understanding better:

这可以帮助你更好地理解:

1st Iteration of While loop(pointer is before the 1st element): Key: if ,value: 2 //{itr2.next()=if; m.get(itr2.next()=it)=>2}

While循环的第一次迭代(指针位于第1元素之前):Key: if,value: 2 //{itr2.next()=if;m.get(itr2.next()=)= > 2 }

2nd Iteration of While loop(pointer is before the 3rd element): Key: is ,value: 2 //{itr2.next()=is; m.get(itr2.next()=to)=>2}

While循环的第二个迭代(指针位于第三个元素之前):Key: is,value: 2 //{itr2.next()=is;m.get(itr2.next()=)= > 2 }

3rd Iteration of While loop(pointer is before the 5th element): Key: be ,value: 1 //{itr2.next()="be"; m.get(itr2.next()="up")=>"1"}

While循环的第3次迭代(指针位于第5元素之前):Key: be,value: 1 //{itr2.next()="be";m.get(itr2.next()=“向上”)= > " 1 " }

4th Iteration of While loop(pointer is before the 7th element): Key: me ,value: 1 //{itr2.next()="me"; m.get(itr2.next()="delegate")=>"1"}

While循环的第4次迭代(指针位于第7元素之前):Key: me,value: 1 //{itr2.next()="me";m.get(itr2.next()= "代表")= > " 1 " }

Key: if ,value: 1 Key: it ,value: 2 Key: is ,value: 2 Key: to ,value: 2 Key: be ,value: 1 Key: up ,value: 1 Key: me ,value: 1 Key: delegate ,value: 1

Key: if,value: 1 Key: it,value: 2 Key: is,value: 2 Key: to,value: 2 Key: be,value: 1 Key: up,value: 1 Key: me,value: 1 Key: delegate,value: 1。

It prints:

它打印:

Key: if ,value: 2
Key: is ,value: 2 Key: be ,value: 1 Key: me ,value: 1

键:如果,值:2键:是,值:2键:是,值:1键:我,值:1。

#1


43  

Every call to the Iterator.next() moves the iterator to the next element. If you want to use the current element in more than one statement or expression, you have to store it in a local variable. Or even better, why don't you simply use a for-each loop?

每次调用迭代器。next()将迭代器移动到下一个元素。如果要在多个语句或表达式中使用当前元素,则必须将其存储在局部变量中。或者更好的是,为什么不简单地使用for-each循环呢?

for (String key : map.keySet()) {
    System.out.println(key + ":" + map.get(key));
}

Moreover, loop over the entrySet is faster, because you don't query the map twice for each key. Also Map.Entry implementations usually implement the toString() method, so you don't have to print the key-value pair manually.

此外,在entrySet上的循环会更快,因为您不会为每个键查询两次映射。也映射。Entry实现通常实现toString()方法,因此您不必手工打印键值对。

for (Entry<String, Integer> entry : map.entrySet()) {
    System.out.println(entry);
}

#2


3  

Every time you call itr2.next() you are getting a distinct value. Not the same value. You should only call this once in the loop.

每一次你叫它2.next()你得到的是一个不同的值。不一样的价值。您应该只在循环中调用它一次。

Iterator<String> itr2 = keys.iterator();
    while(itr2.hasNext()){
        String v = itr2.next();
        System.out.println("Key: "+v+" ,value: "+m.get(v));
    }

#3


2  

Traversal over the large map entrySet() is much better than the keySet(). Check this tutorial how they optimise the traversal over the large object with the help of entrySet() and how it helps for performance tuning.

遍历大型映射entrySet()比keySet()要好得多。请查看本教程,了解如何在entrySet()的帮助下对大型对象进行遍历,以及它如何帮助进行性能调优。

#4


1  

An Iterator moves forward only, if it read it once, it's done. Your

迭代器只向前移动,如果它读取一次,就完成了。你的

m.get(itr2.next());

is reading the next value of itr2.next();, that is why you are missing a few (actually not a few, every other) keys.

下一个值是读取下一个值,这就是为什么你缺了几个(实际上不是几个)键的原因。

#5


0  

Hi @NINCOMPOOP to make things simple , please note that every time you do itr2.next() the pointer moves to the next element,i.e., here if you notice carefully, then the output is perfectly fine according to the logic you have written .

Hi @NINCOMPOOP让事情变得简单,请注意每次你做itr2.next()时,指针移动到下一个元素,即。在这里,如果你仔细地注意到,那么根据你所写的逻辑,输出是完美的。

This may help you in understanding better:

这可以帮助你更好地理解:

1st Iteration of While loop(pointer is before the 1st element): Key: if ,value: 2 //{itr2.next()=if; m.get(itr2.next()=it)=>2}

While循环的第一次迭代(指针位于第1元素之前):Key: if,value: 2 //{itr2.next()=if;m.get(itr2.next()=)= > 2 }

2nd Iteration of While loop(pointer is before the 3rd element): Key: is ,value: 2 //{itr2.next()=is; m.get(itr2.next()=to)=>2}

While循环的第二个迭代(指针位于第三个元素之前):Key: is,value: 2 //{itr2.next()=is;m.get(itr2.next()=)= > 2 }

3rd Iteration of While loop(pointer is before the 5th element): Key: be ,value: 1 //{itr2.next()="be"; m.get(itr2.next()="up")=>"1"}

While循环的第3次迭代(指针位于第5元素之前):Key: be,value: 1 //{itr2.next()="be";m.get(itr2.next()=“向上”)= > " 1 " }

4th Iteration of While loop(pointer is before the 7th element): Key: me ,value: 1 //{itr2.next()="me"; m.get(itr2.next()="delegate")=>"1"}

While循环的第4次迭代(指针位于第7元素之前):Key: me,value: 1 //{itr2.next()="me";m.get(itr2.next()= "代表")= > " 1 " }

Key: if ,value: 1 Key: it ,value: 2 Key: is ,value: 2 Key: to ,value: 2 Key: be ,value: 1 Key: up ,value: 1 Key: me ,value: 1 Key: delegate ,value: 1

Key: if,value: 1 Key: it,value: 2 Key: is,value: 2 Key: to,value: 2 Key: be,value: 1 Key: up,value: 1 Key: me,value: 1 Key: delegate,value: 1。

It prints:

它打印:

Key: if ,value: 2
Key: is ,value: 2 Key: be ,value: 1 Key: me ,value: 1

键:如果,值:2键:是,值:2键:是,值:1键:我,值:1。