Why toList () doesn't exist directly on Java 8 Stream

I understand that Java 8 Stream provides a method .collectfor converting a stream to any collection / data structure of our choice and is very general. Understand that Stream can be created from a non-collection / resouce object. But in terms of usage, it’s natural to convert Stream to List / Set.

Stream already provides a method toArray()for converting to an array, so why not toList()and toSet()?

+4
source share
1 answer

Because you do not know how the implementation will be implemented List:

  • Will it be changed?
  • Could you change your size?
  • etc.

Collectors.toList() :

, , ; , toCollection ().

, .toList() , . , , Collectors.toSet().

: , .

+5

Source: https://habr.com/ru/post/1598579/


All Articles