Psl
Psl

Reputation: 3920

java.lang.ClassCastException: java.util.HashMap$EntrySet cannot be cast to java.util.Map$Entry

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

Upvotes: 9

Views: 23732

Answers (7)

cyril
cyril

Reputation: 1005

i finally managed to do it but with an ubly way and i really do not understand why it is so complicated

package utils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TreeTraversingParser;

import models.Product;
import play.Logger;
import play.libs.Json;

public class ProductDeserializer extends JsonDeserializer<List<Product>>{


@Override
public List<Product> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {

    Map.Entry<String,ObjectNode> map;
    try {   

    TreeTraversingParser parser=(TreeTraversingParser)p;
    Class ftClass = parser.getClass();
    Field nodeCursor = ftClass.getDeclaredField("_nodeCursor");
    boolean nodeCursorAccessible=nodeCursor.isAccessible();
    // Allow modification on the field
    nodeCursor.setAccessible(true);
    Class nodeClass =nodeCursor.get(parser).getClass();
    Field content = nodeClass.getDeclaredField("_current");
    boolean contentAccessible=content.isAccessible();
    content.setAccessible(true);
    map=(Map.Entry<String,ObjectNode>)content.get(nodeCursor.get(parser));
    ObjectNode ob=map.getValue();   
    nodeCursor.setAccessible(nodeCursorAccessible);
    content.setAccessible(contentAccessible);

    return Json.mapper().readValue(ob.get("order_rows").traverse(), new TypeReference<List<Product>>(){});
}catch(Exception e) {
    Logger.error("could not map product for this order"+p.getCurrentValue().toString());
}
    return null;

}

}

Upvotes: 0

Rishabh Mamgain
Rishabh Mamgain

Reputation: 11

The below function can be taken as reference for generic iteration, setting, getting values for a map.

(Iterator<Entry> i = entries.iterator(); i.hasNext(); ) {
    Map.Entry e = (Entry) i.next(); 
    if(((String)e.getValue())==null ){
        i.remove();
    } else {
        e.setValue("false");
    }
    System.out.println(e.getValue());
}

Upvotes: 1

Ashish Agrawal Yodlee
Ashish Agrawal Yodlee

Reputation: 123

If we want to return a particular Entry(row), we need to iterate the first entry via iterator.next():

Map.Entry<String, String> authInfo = (Entry<String, String>)   
authentication.entrySet().iterator().next();

and if we want to iterate over the map, we need to keep this is a loop like:

for( Map.Entry<String, String> authInfo : authentication.entrySet()){
    System.out.println("authInfo:"+authInfo.getKey());
}

Upvotes: 1

Amila
Amila

Reputation: 5213

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)

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

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
     */
}

Upvotes: 7

ka4eli
ka4eli

Reputation: 5424

authentication.entrySet() is a collection of Entry. You can process them all like this:

   authentication.entrySet().stream().map(x->auth(x)).collect(Collectors.toList())

Upvotes: 1

Mena
Mena

Reputation: 48404

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
}

Upvotes: 4

Related Questions