热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将long列表转换为intjava8列表

如何解决《将long列表转换为intjava8列表》经验,为你挑选了1个好方法。

我有一个方法,它采用整数列表作为参数.我目前有一个很长的列表,并希望将其转换为整数列表,所以我写道:

  List student =
  studentLong.stream()
  .map(Integer::valueOf)
  .collect(Collectors.toList());

但我收到一个错误:

method "valueOf" can not be resolved. 

实际上是否可以将long列表转换为整数列表?



1> alfasin..:

您应该使用mapToIntwith Long::intValue来提取int值:

List student = studentLong.stream()
           .mapToInt(Long::intValue)
           .boxed()
           .collec?t(Collectors.toList(??))

您得到的原因method "valueOf" can not be resolved.是因为没有签名Integer::valueOf接受Long作为参数。

编辑
Per Holger在下面的评论,我们也可以这样做:

List student = studentLong.stream()
           .map(Long::intValue)
           .collec?t(Collectors.toList(??))


代替`.mapToInt(Long :: intValue).boxed()`,您可以简单地使用`.map(Long :: intValue)`。
推荐阅读
author-avatar
甘文靖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有