首页 > 解决方案 > 错误->“com.google.gson.internal.LinkedTreeMap 无法转换为”使用 Stream.filter

问题描述

尝试使用 Stream.filter 获取子列表过滤器时出现此异常:

 List<Presupuesto> PresupuestosTienda = presupuestos.stream().filter(item -> item.tienda.equals(LaTienda.nombre)).collect(Collectors.toList());

我的 Presupuesto 课程非常简单:

public class Presupuesto {
String vendedor, tienda;
Date fecha;
double total;

public Presupuesto(){

}
public Presupuesto(String vendedor, String tienda, Date fecha, double total){
    this.vendedor=vendedor;
    this.tienda=tienda;
    this.fecha=fecha;
    this.total=total;
}

}

我真的不知道为什么会这样。我从互联网上举了一个例子,它有效!没有太大区别。

https://github.com/rey5137/tutorials-1/blob/master/java-streams/src/test/java/com/baeldung/stream/filter/StreamFilterUnitTest.java

有人知道会发生什么吗?非常感谢

标签: javalistfilterjava-stream

解决方案


我发现由于我的 List 的来源是一个用 gson 转换的 Json,所以 List 中每个元素的真实类型是 LinkedTreeMap 而不是 Presupuesto 类 Type。所以知道我的问题是其他的。谢谢


推荐阅读