【问题标题】:Error -> "com.google.gson.internal.LinkedTreeMap cannot be cast to" using Stream.filter错误->“com.google.gson.internal.LinkedTreeMap 无法转换为”使用 Stream.filter
【发布时间】:2020-07-15 03:03:32
【问题描述】:

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

 List<Presupuesto> PresupuestosTienda = presupuestos.stream().filter(item -> item.tienda.equals(LaTienda.nombre)).collect(Collectors.toList());
  • “presupuestos”也是List&lt;Presupuesto&gt;

我的课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

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

【问题讨论】:

  • 变量presupuestos是什么类型的?
  • 嗨 Nosrep,它也是一个 List

标签: java list filter java-stream


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    相关资源
    最近更新 更多