You can use Collections.nCopies
.
Note that the returned list is immutable. In fact, the docs say that "the newly allocated data object is tiny (it contains a single reference to the data object)."
If you need a modified list, you would do something like
List<String> hellos = new ArrayList<String>(Collections.nCopies(1000, "Hello"));
If you want 1000 individual objects, you can use
List<YourObject> objects = Stream.generate(YourObject::new) .limit(1000) .collect(Collectors.toList());
Again, there are no guarantees regarding the possibilities of implementing the resulting list. If you need, say ArrayList
, you would do
... .collect(ArrayList::new);
source share