List<ClientFollowUp> distinctList = Stream.concat(list.stream(), result.stream()) .distinct() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<ClientFollowUp>(Comparator.comparing( p -> Arrays.asList(p.getCustomerName())))), ArrayList::new)); 报错 The method getCustomerName() is undefined for the type Object
这个错误通常是由于 Java 泛型类型推断失败导致的。在这段代码中,由于 concat()
方法中的两个 List 可能包含不同类型的元素,所以在进行流操作时,编译器将 Stream 的泛型类型推断为 Object 类型,而 Object 类型并没有 getCustomerName()
方法,因此会报错。
为了解决这个问题,你需要在 Stream 操作中指定泛型类型,例如:
List<ClientFollowUp> distinctList = Stream.<ClientFollowUp>concat(list.stream(), result.stream()) // 显式指定泛型类型
.distinct()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<ClientFollowUp>(Comparator.comparing(
p -> Arrays.asList(p.getCustomerName())))),
ArrayList::new));
在 concat()
方法后面使用 <ClientFollowUp>
显式指定泛型类型,这样就可以避免该错误了。
List<ClientFollowUp> distinctList = Stream.concat(list.stream(), result.stream()) .distinct() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing( p -> Arrays.asList(p.getCustomerName())))), ArrayList::new));
这段代码的作用是将两个 List 合并,并去重,去重的方式是按照 ClientFollowUp 对象的 customerName 属性进行比较。
具体来说,这段代码使用了 Java 8 中的 Stream API,首先通过 Stream.concat(list.stream(), result.stream())
将两个 List 进行合并,得到一个 Stream 对象。然后调用 distinct()
方法对 Stream 中的元素进行去重操作,由于 ClientFollowUp 对象没有实现 Comparable 接口,因此需要在创建 TreeSet 对象时指定 Comparator 对象进行比较。这里使用了 Lambda 表达式 Comparator.comparing(p -> Arrays.asList(p.getCustomerName()))
创建了一个 Comparator 对象,表示按照 ClientFollowUp 对象的 customerName 属性进行比较。最后,使用 collect(Collectors.collectingAndThen(...))
方法将去重后的元素收集到一个 ArrayList 中,并返回该 List。
需要注意的是,这种方式只能对 List 中的元素进行浅层去重,即只能去除对象的引用相同的元素,如果需要对 List 中的元素进行深层去重,需要在 ClientFollowUp 类中重写 equals 和 hashCode 方法。
List<ProtocolActualExecutionInfo> protocolActualExecutionInfos = protocolActualExecutionInfoMapper.selectByExample(protocolActualExecutionInfoExample); List<ProtocolDetailInfo> protocolDetailInfos = protocolDetailInfoMapper.selectByExample(protocolDetailInfoExample);根据ProtocolActualExecutionInfo中的dlcgid+dlcgxyid与ProtocolDetailInfo中的dlcgid+dlcgxyid匹配合并成一个list
可以使用Java8的Stream API来实现这个需求,具体代码如下:
List<ProtocolActualExecutionInfo> protocolActualExecutionInfos = protocolActualExecutionInfoMapper.selectByExample(protocolActualExecutionInfoExample);
List<ProtocolDetailInfo> protocolDetailInfos = protocolDetailInfoMapper.selectByExample(protocolDetailInfoExample);
List<ProtocolInfo> protocolInfos = Stream.concat(
protocolActualExecutionInfos.stream().map(info -> new ProtocolInfo(info.getDlcgid(), info.getDlcgxyid())),
protocolDetailInfos.stream().map(info -> new ProtocolInfo(info.getDlcgid(), info.getDlcgxyid()))
)
.distinct()
.collect(Collectors.toList());
其中,ProtocolInfo
是一个简单的POJO类,表示dlcgid
和dlcgxyid
的组合:
public class ProtocolInfo {
private String dlcgid;
private String dlcgxyid;
public ProtocolInfo(String dlcgid, String dlcgxyid) {
this.dlcgid = dlcgid;
this.dlcgxyid = dlcgxyid;
}
// 省略getter和setter方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProtocolInfo that = (ProtocolInfo) o;
return Objects.equals(dlcgid, that.dlcgid) &&
Objects.equals(dlcgxyid, that.dlcgxyid);
}
@Override
public int hashCode() {
return Objects.hash(dlcgid, dlcgxyid);
}
}
这里使用了Stream.concat()
方法将两个Stream合并起来,然后使用distinct()
方法去重,最后使用Collectors.toList()
方法将结果收集到一个List中。
相关推荐
















