简体   繁体   English

迭代LinkedHashMap显示编译错误

[英]Iterating the LinkedHashMap shows compilation error

Below is the code for AttributeValue class- 以下是AttributeValue类的代码-

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class AttributeValue<T> {
    private T value;                    // value
    private Date timestamp;             // timestamp
    String valueType = null;        // class type of the value object

    @Deprecated
    private int classNumber = 0;        // internal

    private static Logger s_logger = Logger.getInstance(BEAttributeValue.class);

    @JsonProperty("v")
    public T getValue() {
        if (valueType != null && !value.getClass().getName().equalsIgnoreCase(valueType)) {
            value = convert(value, valueType);
        }
        return value;
    }

    @JsonProperty("v")
    public void setValue(T value) {
        this.value = value;
    }

    private T convert(Object other, String classType) {
        T value = null;
        if (other != null) {
            IJsonMapper mapper = JsonMapperFactory.getInstance().getJsonMapper();
            try {
                String json = mapper.toJson(other);
                Class<T> className = (Class<T>)Class.forName(classType);
                value = mapper.toPojo(json, className);
            } catch (Exception e) {
                s_logger.log(LogLevel.ERROR, "BEAttributeValue::convert(), caught an exception: \n",e.getStackTrace());
            }       
        }
        return value;
    }
}

Problem Statement:- 问题陈述:-

Now I am trying to iterate over the list of AttributeValue with the below code- 现在,我尝试使用以下代码遍历AttributeValue的列表:

for(AttributeValue<?> al: list) {

System.out.println(al.getValue());

}

When I inspect on al , I see value as LinkedHashMap<K,V> and when I print al.getValue() , it gives me this- 当我检查al ,我看到的值为LinkedHashMap<K,V> ,当我打印al.getValue() ,它给了我-

{predictedCatRev=0;101;1,1;201;2, predictedOvrallRev=77;2,0;1,16;3, sitePrftblty=77;2,0;1671679, topByrGms=12345.67, usrCurncy=1, vbsTopByrGmb=167167.67}

So I thought al.getValue will be a Map and I can iterate it like this- 所以我认为al.getValue将是一个Map ,我可以像这样迭代它-

for (Map.Entry<Integer, Integer> entry : al.getValue().entrySet()) {
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

But it is giving me compilation error at entrySet() with red color. 但这在红色的entrySet()处给了我编译错误。 And I am not sure how can I iterate over the value as clearly while inspection, I can see that as LinkedHashMap<K,V> . 而且我不确定在检查时如何清楚地迭代该value ,可以看到它为LinkedHashMap<K,V>

Can anyone help me with this? 谁能帮我这个?

You need to do a cast . 你需要做一个演员 The compiler does not know that al.getValue() is of type Map<Integer, Integer> , so you have to tell him specifically: 编译器不知道al.getValue()的类型为Map<Integer, Integer> ,因此您必须专门告诉他:

for (Map.Entry<Integer, Integer> entry : ((Map<Integer, Integer>) al.getValue()).entrySet()) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM