How to solve these incompatible types in java?

I get an error in the following lines.

error: incompatible types required : java.util.Map.entry<java.lang.String,java.lang.String[]> found :java.lang.Object 

full code below

 package com.auth.actions; public class SocialAuthSuccessAction extends Action { final Log LOG = LogFactory.getLog(SocialAuthSuccessAction.class); @Override public ActionForward execute(final ActionMapping mapping, final ActionForm form, final HttpServletRequest request, final HttpServletResponse response) throws Exception { AuthForm authForm = (AuthForm) form; SocialAuthManager manager = null; if (authForm.getSocialAuthManager() != null) { manager = authForm.getSocialAuthManager(); } if (manager != null) { List<Contact> contactsList = new ArrayList<Contact>(); Profile profile = null; try { Map<String, String> paramsMap = new HashMap<String, String>(); for (Map.Entry<String, String[]> entry :request.getParameterMap().entrySet() ) { // error in this line! String key = entry.getKey(); String values[] = entry.getValue(); paramsMap.put(key, values[0].toString()); // Only 1 value is } AuthProvider provider = manager.connect(paramsMap); profile = provider.getUserProfile(); contactsList = provider.getContactList(); if (contactsList != null && contactsList.size() > 0) { for (Contact p : contactsList) { if (StringUtils.isEmpty(p.getFirstName()) && StringUtils.isEmpty(p.getLastName())) { p.setFirstName(p.getDisplayName()); } } } } catch (Exception e) { e.printStackTrace(); } request.setAttribute("profile", profile); request.setAttribute("contacts", contactsList); return mapping.findForward("success"); } // if provider null return mapping.findForward("failure"); } } 

Please, help

+6
source share
3 answers

You need to drop request.getParameterMap() in Map<String, String[]>

 for (Map.Entry<String, String[]> entry : ((Map<String, String[]>)request.getParameterMap()).entrySet()) 
+11
source

Try the following:

 for (Object obj :request.getParameterMap().entrySet() ) { Map.Entry<String, String[]> entry = (Map.Entry<String, String[]>) obj; String key = entry.getKey(); String values[] = entry.getValue(); paramsMap.put(key, values[0].toString()); // Only 1 value is } 

Not sure if this will work, anyway, you got this approach. Hope this helps.

+5
source

As pointed out in the comments , getParameterMap() should return a raw Map type instead of Map<String, String[]> . This means that getParameterMap().entrySet() returns raw Iterable , causing a compiler error.

If you want to avoid an explicit uncontrolled act, as other answers suggest, the alternative is to use a variable assignment for an unchecked conversion:

 @SuppressWarnings("unchecked") // getParameterMap returns raw Map Map<String, String[]> params = request.getParameterMap(); for (Map.Entry<String, String[]> entry : params.entrySet()) { ... } 
+2
source

Source: https://habr.com/ru/post/909527/


All Articles