Home > Mobile >  Incompatible types. Found: 'java.util.Map.Entry', required: 'java.lang.Object'
Incompatible types. Found: 'java.util.Map.Entry', required: 'java.lang.Object'

Time:04-24

I am working on a simple project for fun, but I am getting an odd error while writing the test method. In it is a loop, and I have made sure that it causes the problem. It gives me this compilation error

Incompatible types. Found: 'java.util.Map.Entry', required: 'java.lang.Object'

The code is written as below:

        for(Map.Entry entry : map.entrySet()){
            entry.getKey();
            entry.getValue();
            System.out.println(entry.getKey() "  " entry.getValue());
        }

The searchUtil was wirrten to search in the sql,I have tested it and maybe it has not error.

public Map Search(T t, String... condition){
        JdbcPool pool = JdbcPoolImpl.poolEnum.INSTANCE.getPoolInstance();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        StringBuilder sql = new StringBuilder("select * from " t.getClass().getSimpleName());
        Map map = new HashMap();
        try {
            conn = pool.getConn();
            ps = conn.prepareStatement(sql.toString());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (!"".equals(condition[0])){
            try {
                if (condition.length > 0){
                    sql = sql.append(" where " condition[0] " =?");
                    Method method = t.getClass().getMethod("get" condition[0].substring(0,1).toUpperCase() condition[0].substring(1));
                    Object object = method.invoke(t);
                    ps = conn.prepareStatement(sql.toString());
                    ps.setObject(1,object);
                    if (condition.length>1){
                        for (int i = 1; i < condition.length; i  ) {
                            sql = sql.append(" and " condition[i] " =?");
                        }
                        ps = conn.prepareStatement(sql.toString());
                        ps.setObject(1,object);
                        for (int i = 1; i < condition.length; i  ) {
                            method = t.getClass().getMethod("get" condition[i].substring(0,1).toUpperCase() condition[i].substring(1));
                            object = method.invoke(t);
                            ps.setObject(i 1,object);
                        }
                    }
                }
            } catch (SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException throwables) {
                throwables.printStackTrace();
            }
        }
        try {
            rs = ps.executeQuery();
            ResultSetMetaData resultSetMetaData = rs.getMetaData();
            Field[] fields = t.getClass().getDeclaredFields();
            int count = fields.length;
            int id;
                while (rs.next()) {
                    id = rs.getInt(1);
                    Constructor constructor = t.getClass().getConstructor();
                    T t1 = (T)constructor.newInstance();
                    for (int i = 0; i < count; i  ) {
                        Method method = t.getClass().getMethod("set"   resultSetMetaData.getColumnName(i   1).substring(0, 1).toUpperCase()   resultSetMetaData.getColumnName(i   1).substring(1), fields[i].getType());
                        method.invoke(t1, rs.getObject(i   1));
                    }
                    map.put(id,t1);
                }
            }catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | SQLException throwables) {
                throwables.printStackTrace();
            }
        finally {
            pool.release(conn);
            JdbcUtil.close(ps,rs);
        }
        return map;
    }

I have googled it many times but have no function.Any help would be appreciated.

CodePudding user response:

Don't use raw types, Map is a generic interface, you may define the type of keys and values

// at method return type
public <T> Map<Integer, Object> Search(T t, String... condition){

    // at definition
    Map<Integer, Object> map = new HashMap<>();

}

And when retrieving and using it

Map<Integer, Object> map = foo.Search();

for (Map.Entry<Integer, Object> entry : map.entrySet()) {
    System.out.println(entry.getKey()   "  "   entry.getValue());
}
  •  Tags:  
  • java
  • Related