如何在java中解决这种不兼容的类型?

时间:2012-02-28 08:45:23

标签: java generics

我在以下几行中收到错误。

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

完整代码位于

之下
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");
    }
}

请帮忙

3 个答案:

答案 0 :(得分:11)

您需要将request.getParameterMap()投射到Map<String, String[]>

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

答案 1 :(得分:5)

尝试以下方法:

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
            }

我不确定这会起作用,无论如何,你得到了方法。希望这会有所帮助。

答案 2 :(得分:2)

作为评论中的pointed outgetParameterMap()必须返回原始类型Map而不是Map<String, String[]>。这意味着getParameterMap().entrySet()返回原始Iterable,导致编译器错误。

如果你想避免像其他答案建议那样做一个明确的未经检查的演员表,另一种方法是使用变量赋值进行未经检查的转换:

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