9

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

1
  • 1
    A single entry isn't a set of entries... why would you expect that to work? (And do you control the API? It's really weird to accept a Map.Entry<String, String> as a parameter instead of just two strings...) Commented Aug 5, 2015 at 12:20

7 Answers 7

7

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
     */
}
Sign up to request clarification or add additional context in comments.

Comments

4

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
}

Comments

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

Comments

1

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

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

1 Comment

You probably want to specify your answer is Java 8 only.
1

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

Comments

1

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

Comments

0

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;

}

}

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.