I have an overrriden method like this
@Override
public Build auth (Map.Entry<String, String> auth) {
this.mAuth = auth;
return this;
}
Here am trying to call this method in the following way
Map<String, String> authentication = new HashMap<String , String> ();
authentication.put("username" , "testname");
authentication.put("password" , "testpassword");
Map.Entry<String, String> authInfo =(Entry<String , String>) authentication.entrySet();
AuthMethod.auth(authInfo)
While running this am getting
java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.Map$Entry
How can i pass Map.Entry<String, String>
to auth method
You are trying to cast a set to a single entry.
You can use each entry item by iterating the set:
Iterator it = authentication.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next(); //current entry in a loop
/*
* do something for each entry
*/
}
Well, yes.
You are trying to cast a Set<Map.Entry<String, String>>
as a single Map.Entry<String, String>
.
You need to pick an element in the set, or iterate each entry and process it.
Something in the lines of:
for (Map.Entry<String, String> entry: authentication.entrySet()) {
// TODO logic with single entry
}
Map.Entry<String, String> authInfo =(Entry<String, String>) authentication.entrySet();
Here you are doing a wrong cast. The auth method you mentioned seem to be expecting just the values of username/password pair. So something like below would do:
Map<String, String> authentication = new HashMap<String, String>();
authentication.put("testname", "testpassword");
Map.Entry<String, String> authInfo = authentication.entrySet().iterator().next();
AuthMethod.auth(authInfo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With