开发者

List of entries, how to add a new Entry?

In Java,开发者_运维问答 I am implementing this:

List<Entry<String, Integer>> listObjects = new ArrayList<Entry<String, Integer>>();

but how can I add a new Entry?

as it does not work with: listObjects.add(new Entry<"abc", 1>());

thanks in advance.


I know it's a pretty older thread but you can do it as follows:

listObjects.add(new java.util.AbstractMap.SimpleEntry<String, Integer>("abc", 1));

It might help someone like me, who was trying to do this recently!

I hope it helps :-)


Do you mean Map.Entry? That is an interface (so you can't instantiate without an implementation class, you can learn about interfaces in the Java Tutorial). Entry instances are usually only created by Map implementations and only exposed through Map.entrySet()

Of course, since it's an interface you could add your own implementation, something like this:

public class MyEntry<K, V> implements Entry<K, V> {
    private final K key;
    private V value;
    public MyEntry(final K key) {
        this.key = key;
    }
    public MyEntry(final K key, final V value) {
        this.key = key;
        this.value = value;
    }
    public K getKey() {
        return key;
    }
    public V getValue() {
        return value;
    }
    public V setValue(final V value) {
        final V oldValue = this.value;
        this.value = value;
        return oldValue;
    }
}

That way you could do listObjects.add(new MyEntry<String,Integer>("abc", 1))

But that doesn't really make sense outside of a map context.


Entry is a parametized class and you need to create instances of Entry with a constructor (typical way).

Without knowing the implementation of Entry: this could already work (at least it shows how it usually works):

// create a list
List<Entry<String, Integer>> listObjects = 
               new ArrayList<Entry<String, Integer>>()

// create an instance of Entry
Entry<String, Integer> entry = new Entry<String, Integer>("abc", 1);

// add the instance of Entry to the list
listObjects.add(entry);

With Map.Entry it is somewhat different (OP just mentioned, that Entry is Map.Entry in Fact. We can't create Map.Entry instances, we usually get them from an existing map:

Map<String, Integer> map = getMapFromSomewhere();
List<Map.Entry<String, Integer>> listObjects = 
               new ArrayList<Map.Entry<String, Integer>>();

for (Map.Entry<String, Integer> entry:map.entrySet())
    listObjects.add(entry);


One way to use a ready made implementation of Entry would be Guava's Immutable Entry Something like this listObjects.add(Maps.immutableEntry("abc",1));


Perhaps listObjects.add(new Entry<String, Integer>("abc", 1));? The generics specify the type (String, Integer), not the values (abc, 1).

Edit - Note that it was later added that the Entry is in fact Map.Entry, so you need to create an implementation of Map.Entry that you later instantiate, exactly as the chosen answer explains.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜