Smat
Smat

Reputation: 17

adding values in list with user defined class

i am new to andoird and i am using list with custom defined class but i am not sure how to add my key value pair in this list here is my code

public class Pair<K,V> {
    private K k;
    private V v;

    public Pair(K k,V v)
    {
        this.k=k;
        this.v=v;

    }
    public K getK()
    {
        return k;
    }

    public void setK( K k)
    {
        this.k=k;
    }

    public V getV()
    {
        return v;
    }
    public void setV(V v)
    {
        this.v=v;
    }
}

and

List<Pair<String,String>> pl = new ArrayList<Pair<String,String>>();

I tried

pl.add(Pair("name","ahsen"));
 pl.addAll(Pair("name","ahsen"));

but its not working

Upvotes: 0

Views: 134

Answers (2)

Matej Špil&#225;r
Matej Špil&#225;r

Reputation: 2687

am i missing something or its just missing new operator ?

pl.add(new Pair("name","ahsen"));
pl.addAll(new Pair("name","ahsen"));

Upvotes: 1

Andrei
Andrei

Reputation: 3106

You are missing a new operator.

pl.add(new Pair("name","ahsen"));

You can use a HashMap for key/value pairs.

Upvotes: 1

Related Questions