6

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.

olidev
  • 20,058
  • 51
  • 133
  • 197
  • 1
    All questions should have expected behaviour _and_ actual behaviour (the error generated in your case). What is javac/Eclipse/NetBeans (or the JRE if it's a runtime thing) saying the actual problem is? – paxdiablo May 25 '11 at 07:57
  • can you give a "concise" answer? – olidev May 25 '11 at 07:58
  • what does "does not work" actually mean? – kostja May 25 '11 at 07:59
  • I can not create a new Entry object! – olidev May 25 '11 at 08:08
  • 1
    To anyone having the same question, be sure to check [this answer](http://stackoverflow.com/questions/3110547/java-how-to-create-new-entry-key-value/3110563#3110563) to the [_How to create a new Entry (key, value)_](http://stackoverflow.com/questions/3110547/java-how-to-create-new-entry-key-value) question. – Bloke May 13 '12 at 15:41

5 Answers5

13

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 :-)

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Anam Jabbar
  • 131
  • 1
  • 3
5

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
4

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);
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
2

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));

Abhiroop Sarkar
  • 2,251
  • 1
  • 26
  • 45
0

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.

Miki
  • 7,052
  • 2
  • 29
  • 39
  • listObjects.add(new Entry("abc", 1)); also does work. Thanks! – olidev May 25 '11 at 07:59
  • @stifin you are right, however the original question did not mention `Map.Entry`; OP clarified it later, once the answers were already given; of course in that case you must implement `Map.Entry` and instantiate that – Miki Feb 03 '13 at 12:01