Super Hornet
Super Hornet

Reputation: 2907

java : cannot set Map field

I have a simple bean like this:

class Account {
    private String username;
    private String password;
    private Map<String, String> extras;

    String getUsername() {
        return username;
    }

    void setUsername(String username) {
        this.username = username;
    }

    String getPassword() {
        return password;
    }

    void setPassword(String password) {
        this.password = password;
    }

    Map<String, String> getExtras() {
        return extras;
    }

    void setExtras(Map<String,String> attr) {
        this.extras=attr;
    }
 }

now I'm going to set extra by:

 Account tmpAccount=new Account();
 tmpAccount.setExtras(new HashMap<String, String>().put("x","y"));

but I got this error:

 setExtras(Map<String,String> in Account cannot be applied to Object.

Why?

Upvotes: 2

Views: 1841

Answers (6)

Muhammad
Muhammad

Reputation: 7324

The Method put() returns a string object while the method setExtras(Map m) is expecting an object of calls Map, Try the following :)

    Account tmpAccount = new Account();
    Map map = new HashMap<String, String>();
    map.put("x", "y");
    tmpAccount.setExtras(map);

Upvotes: 0

vinayknl
vinayknl

Reputation: 1252

Account tmpAccount=new Account();
tmpAccount.setExtras(new HashMap<String, String>().put("x","y"));

will not work. Use as below

  Account tmpAccount=new Account();
  Map<String, String> myExtras = new HashMap<String, String>();
  myExtras.put("x","y");
  tmpAccount.setExtras(myExtras);

Reason, HashMap's put method signature is as below

 public V put(K key,V value)

and the value it returns is

Returns:
        the previous value associated with key, or null if there was no mapping for key. (A null return can      
         also indicate that the map previously associated null with key.)

So, when you do

tmpAccount.setExtras(new HashMap<String, String>().put("x","y"));

You are setting null to setExtras where as it is expecting a HashMap

Upvotes: 0

Super Hornet
Super Hornet

Reputation: 2907

There is another solution that I just found, casting to Map:

  tmpAccount.setExtras(Map<String, String>)new HashMap<String, String>().put("x","y"));

It works properly.

Upvotes: 0

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

.put() returns the previous value associated with key, or null if there was no mapping for key. You should do something like this:

Account tmpAccount=new Account();
Map<String, String> values = new HashMap<String, String>();
values.put("x","y");
tmpAccount.setExtras(values);

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240890

new HashMap<String, String>().put("x","y")

this statement returns a String instance

void setExtras(Map<String,String> attr)

See put(K, V) API doc for reference

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201439

If I understand your question, the issue is you can't chain the HashMap put,

Map<String, String> map = new HashMap<>();
tmpAccount.setExtras(map.put("x","y"));

Per the Map#put() method defintion,

V put(K key, V value)

It returns V, not Map. And setExtras(map); takes a Map (not a String).

Upvotes: 3

Related Questions