Covert the map value to list<{specific type}>

Viewed 76

description:

  • I try to use the value which got by map.get() to another method's parameter, but got a type error 'instanceToMap(java.util.List<java.lang.String>, Foo)' in 'test' cannot be applied to '(java.lang.Object, Foo)'

origin code:

test.java
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class test{
    public static void main(String[] args) {
        Foo info = new Foo();
        info.setType("cpu");
        List<String> fieldsNameCpu = new ArrayList<>(Arrays.asList("ID","Cpu"));
        List<String> fieldsNameFps = new ArrayList<>(Arrays.asList("Name","Fps"));
        List<String> fieldsNameMem = new ArrayList<>(Arrays.asList("Time","Mem"));
        Map<String, Object> fieldsNameMap = new HashMap<String, Object>() {{
            put("cpu",fieldsNameCpu);
            put("fps",fieldsNameFps);
            put("mem",fieldsNameMem);
        }};
        Map<String, Object> result = new test().instanceToMap(fieldsNameMap.get(info.getType()),info);
        System.out.println(result);
    }

    public Map<String, Object> instanceToMap(List<String> fieldsName,Foo info){
        Map<String, Object> fields = new HashMap<>();
        try {
            for (String field:fieldsName){
                fields.put(field, info.getClass().getMethod("get"+field).invoke(info));
            }
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return fields;
    }
}

Foo.java
public class Foo {
    private String type;
    public void setType(String newType){
        this.type = newType;
    }
    public String getType(){
        return type;
    }
    public int getID(){
        return 1;
    }
    public String getName(){
        return "name";
    }
    public String getTime(){
        return "time";
    }
    public int getPid(){
        return 2;
    }
    public int getUid(){
        return 3;
    }
    public float getIdel(){
        return 1.0F;
    }
    public double getTotal(){
        return 1.0;
    }
    public int getFg(){
        return 4;
    }
    public int getCpu(){
        return 11;
    }
    public int getFps(){
        return 19;
    }
    public int getMem(){
        return 8;
    }
}

what I have tried:

  1. I used Debug and breakpoint to check type of fieldsNameMap.get(info.getType())
        Object temp = fieldsNameMap.get(info.getType());
        Map<String, Object> result = new test().instanceToMap(temp,info);//add breakpoint here
  • Debugger told me temp = {ArrayList@491} size = 2, but I still got same error.
  1. I tried to convert object to list:
        Object temp = fieldsNameMap.get(info.getType());
        Map<String, Object> result = new test().instanceToMap(Arrays.asList(temp),info);
  • Obviously it didn't work, it convert the object to a type like List<List<String>>.
  1. Follow the IDE hint.
        Map<String, Object> result = new test().instanceToMap((List<String>) fieldsNameMap.get(info.getType()),info);
  • It worked, but got a warning Unchecked cast: 'java.lang.Object' to 'java.util.List<java.lang.String>'.
  1. Change instanceToMap required parameter type.
  • It should work, but it need change the irrelevant code. I think it maybe not a good pratice.

question:

  1. (Maybe a X-Y problem) How to convert the value which got by map.get() to list<{specific type}>?
  2. Maybe I did something wrong but I don't know?
  3. In general, I need a solution without any warning, bad practice or hack.
0 Answers
Related