为什么 java stream.count() 返回一个长的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44661078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:17:33  来源:igfitidea点击:

Why does java stream.count() return a long?

javajava-8java-stream

提问by NDavis

Why doesn't a stream.count()return an int?

为什么不stream.count()返回一个int

I understand that I can easily convert the longto an intby casting,

我知道我可以通过强制转换轻松地将 the 转换long为 an int

return (int) players.stream().filter(Player::isActive).count();

but whywould a java stream.count()return a longinstead of an int?

但是为什么java 会stream.count()返回 along而不是 an int

采纳答案by Eugene

Well simply because it's the biggest 64-bit primitive value that java has. The other way would be twocounts:

原因很简单,因为它是 java 拥有的最大的 64 位原始值。另一种方式是两个计数:

countLong/countInt

and that would look really weird.

那看起来真的很奇怪。

intfits in a long, but not the other way around. Anything you want to do with int you can fit in a long, so why the need to provide both?

int适合 a long,但反之则不行。你想用 int 做的任何事情都可以放在 long 中,那么为什么需要同时提供两者呢?

回答by dasblinkenlight

When Java came out in early 1996, common PCs had 8 to 16 Mb of memory. Since both arrays and collections were closely tied to memory size, using intto represent element counts seemed natural, because it was sufficient to address an array of ints that is 4Gb in size - a size gigantic even for hard drives in 1996, let alone RAM. Hence, using longinstead of intfor collection sizes would seem wasteful at the time.

当 Java 于 1996 年初问世时,普通 PC 具有 8 到 16 Mb 的内存。由于数组和集合都与内存大小密切相关,因此使用int来表示元素计数似乎很自然,因为它足以处理int大小为 4Gb的s数组——即使对于 1996 年的硬盘驱动器来说,这个大小也是巨大的,更不用说 RAM。因此,使用long而不是int收集大小在当时看起来很浪费。

Although intsize may be a limiting factor at times, Java designers cannot change it to long, because it would be a breaking change.

尽管int有时大小可能是一个限制因素,但 Java 设计人员不能将其更改为long,因为这将是一个重大更改。

Unlike Java collections, streams could have potentially unlimited number of elements, and they carry no compatibility considerations. Therefore, using longwith its wider range of values seems like a very reasonable choice.

与 Java 集合不同,流可能具有无限数量的元素,并且它们没有兼容性考虑。因此,使用long更广泛的值似乎是一个非常合理的选择。

回答by Greg Osgood

This statement

这个说法

players.stream().filter(Player::isActive).count(); 

is equivalent to:

相当于:

players.stream().filter(Player::isActive).collect(Collectors.counting());

This still returns a longbecause Collectors.counting()is implemented as

这仍然返回一个long因为Collectors.counting()被实现为

reducing(0L, e -> 1L, Long::sum)

Returning an intcan be accomplished with the following:

返回 anint可以通过以下方式完成:

players.stream().filter(Player::isActive).collect(Collectors.reducing(0, e -> 1, Integer::sum));

This form can be used in groupingBystatement

此表格可用于groupingBy声明

Map<Player, Integer> playerCount = players.stream().filter(Player::isActive).collect(Collectors.groupingBy(Function.identity(), Collectors.reducing(0, e -> 1, Integer::sum)));