Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

java - Simple loop iteration over Eclipse Collections maps (for example: IntObjectHashMap)

Is there a way to use simple Java for-each loop over Eclipse Collections maps?

I am looking for something like this (but for Eclipse Collections maps):

for (Map.Entry<Integer, String> entry : map.entrySet()) {
}

... but I can't find anything like it for Eclipse Collections maps.

Of course I know this type of Eclipse Collections iterations:

import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap;

public class Test {
    public static void main(String[] args) {
        IntObjectHashMap<String> map = new IntObjectHashMap<>();
        map.put(1, "one");
        map.put(2, "two");
        int i = 0;
        map.forEachKeyValue((int key, String val) -> {
            i++; // Compilation error. 
            System.out.println("key: " + key + ", val: " + val);
        });
    }
}

... but this construct has some drawbacks, for example I don't have easy access to the surrounding local variables (as shown in the example above, which example will not compile due to incorrect access to the local variable i).

Any ideas how to write simple loop over Eclipse Collections maps?

question from:https://stackoverflow.com/questions/65923600/simple-loop-iteration-over-eclipse-collections-maps-for-example-intobjecthashm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The recommendations from Hulk and Basil in the comments are good. I'll include a test for your code that works for future reference.

@Test
public void keyValuesView()
{
    IntObjectHashMap<String> map = new IntObjectHashMap<>();
    map.put(1, "one");
    map.put(2, "two");
    int i = 0;
    for (IntObjectPair<String> pair : map.keyValuesView())
    {
        i++;
        System.out.println("key: " + pair.getOne() + ", val: " + pair.getTwo());
    }
    Assert.assertEquals(2, i);
}

The best option is to use an internal iterator as you have in your question, as there will be less garbage generated while iterating (an IntObjectPair for each key/value pair). This comes with the downside of not being able to reference anything from the outer scope of the lambda that is not final. If you want to have a simple counter for things inside of the internal iterator, you can use the Counter class available in Eclipse Collections.

@Test
public void forEachKeyValueWithCounter()
{
    IntObjectHashMap<String> map = new IntObjectHashMap<>();
    map.put(1, "one");
    map.put(2, "two");
    Counter counter = new Counter();
    map.forEachKeyValue((int key, String val) -> {
        counter.increment();
        System.out.println("key: " + key + ", val: " + val);
    });
    Assert.assertEquals(2, counter.getCount());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...