副本列表<;A>;要列出的项目<;B>;使用Java流API的项目

副本列表<;A>;要列出的项目<;B>;使用Java流API的项目,java,collections,java-8,Java,Collections,Java 8,我正在尝试将列表元素从列表复制到列表,并遇到一些类型错误 我的代码: List<AttachmentTypeDto> attachmentTypeList=clientDetails.getAttachmentTypeDto(); List<ClientAttachmentTypeEntity> mappedAttachmentList=attachmentTypeList.stream().map(temp ->{ ClientAttac

我正在尝试将列表元素从
列表
复制到
列表
,并遇到一些类型错误

我的代码:

   List<AttachmentTypeDto> attachmentTypeList=clientDetails.getAttachmentTypeDto();
   List<ClientAttachmentTypeEntity> mappedAttachmentList=attachmentTypeList.stream().map(temp ->{

      ClientAttachmentTypeEntity toAttachmentObject=new ClientAttachmentTypeEntity();
      toAttachmentObject.setAttachmentTypeId(temp.getAttachmentTypeId());
      toAttachmentObject.setClientId(temp.getClientId());

  }).collect(Collectors.toList());
List attachmentTypeList=clientDetails.getAttachmentTypeDto();
List mappedAttachmentList=attachmentTypeList.stream().map(临时->{
ClientAttachmentTypeEntity to AttachmentObject=新ClientAttachmentTypeEntity();
toAttachmentObject.setAttachmentTypeId(temp.getAttachmentTypeId());
toAttachmentObject.setClientId(temp.getClientId());
}).collect(Collectors.toList());
它给了我这样的错误:

The method map(Function<? super AttachmentTypeDto,? extends R>) in the type Stream<AttachmentTypeDto> is not applicable for the arguments ((<no type> temp) -> {})

方法映射(函数我不明白为什么有人投票-1你应该在映射函数中返回
toAttachmentObject
.map(temp->{…return toAttachmentObject;}).
这确实解决了我的问题,谢谢@Hadi JA备注:我建议提取映射过程(即
.map中的lambda体)(…)
在单独的方法中,例如,
convertAttachmentDtoToClientAttachmentDto(…)
然后调用
map(this::convertAttachmentDtoToClientAttachmentDto)
。太好了,我正在研究相同的解决方案:)我不明白为什么有人投票-1你应该在map函数中返回
toAttachmentObject
.map>(temp->{…return to attachmentobject;}).
这真的解决了我的问题,谢谢@Hadi JA备注:我建议用单独的方法提取映射过程(即
.map(…)
中的lambda体,例如
convertAttachmentDtoToClientAttachmentDto(…)
然后调用
映射(this::convertAttachmentDtoToClientAttachmentDto)
。太好了,我正在使用相同的解决方案:)