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)); 这代码中 报错Cannot infer type arguments for TreeSet<>
时间: 2024-02-14 14:11:24 浏览: 36
这个错误通常是由于 Java 泛型类型推断失败导致的。在这段代码中,由于 TreeSet 是一个泛型类,需要在创建对象时指定类型参数,但是没有指定类型参数,导致编译器无法推断类型参数,从而报错。
为了解决这个问题,你可以显式指定类型参数,例如:
```
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));
```
在创建 TreeSet 对象时,指定了类型参数为 ClientFollowUp,这样就可以避免该错误了。
相关问题
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<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中。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)