2

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?

3
  • 3
    Because new HashMap<String, String>().put("x","y") doesn't bring you the same HashMap, the method put("x","y") will rather bring you the last object mapped as "x" Commented Jul 14, 2014 at 5:05
  • 1
    have you tried to add without using of anonymous object? Commented Jul 14, 2014 at 5:06
  • 1
    To avoid ugly nullPointException change to private Map<String, String> extras = Collections.emptyMap(); and set your Strings to "" Commented Jul 14, 2014 at 5:12

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

2
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

1 Comment

It will not return "y" but the previous mapping for "x" and since there wasn't any, it will return null.
0

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

1 Comment

No, put() returns the previous mapping for the key inserted, if any, or null if there is none.
0

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.

Comments

0
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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.