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
时间: 2024-02-14 17:11:25 浏览: 37
这个错误通常是由于 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<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中。
两个List<ClientFollowUp> list 根据字段 title,country,customerName 去重
如果想要根据字段 title,country,customerName 去重两个 List<ClientFollowUp>,可以使用 Java 8 的 Stream API 和 lambda 表达式结合 `distinct()` 方法和 `concat()` 方法来实现。具体实现代码如下:
```java
List<ClientFollowUp> distinctList = Stream.concat(list1.stream(), list2.stream())
.distinct()
.collect(Collectors.toList());
```
上述代码中的 `concat()` 方法会将两个 List 合并成一个 Stream,然后再使用 `distinct()` 方法来根据对象的 `equals()` 和 `hashCode()` 方法去重。这样就能实现对两个 List<ClientFollowUp> 根据字段 title,country,customerName 去重的操作了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)