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

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 int to 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 long instead of int for collection sizes would seem wasteful at the time.

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

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

Leave a Comment