0

下面是 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;
    }
}

问题陈述:-

现在我正在尝试AttributeValue使用以下代码迭代列表 -

for(AttributeValue<?> al: list) {

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

}

当我检查时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}

所以我想al.getValue会是一个Map,我可以像这样迭代它-

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

但它给了我entrySet()红色的编译错误。而且我不确定如何value在检查时清楚地迭代 ,我可以看到LinkedHashMap<K,V>.

谁能帮我这个?

4

1 回答 1

1

你需要做一个演员。编译器不知道al.getValue()是 type Map<Integer, Integer>,所以你要具体告诉他:

for (Map.Entry<Integer, Integer> entry : ((Map<Integer, Integer>) al.getValue()).entrySet()) {
于 2013-06-13T21:41:30.020 回答