Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream over empty Collection, adding values

What I would like to do is add values from 1 to 15 to the empty HashSet.

Set<Long> vipSeats = new HashSet<>();

Can I avoid using loop? Does Java have functional support for it e.g. stream ?

like image 264
Rudziankoŭ Avatar asked Jul 22 '26 02:07

Rudziankoŭ


1 Answers

LongStream.rangeClosed(1, 15).boxed().collect(Collectors.toSet());
like image 148
Andy Turner Avatar answered Jul 23 '26 15:07

Andy Turner