0

For a test I need to create a set of numeric values. This can really be anything but for the sake of example let's say it is the numbers from 1 to 10,000 and I'll use a HashSet but anything implementing the Set interface is fine.

To do this I've done:

Set<Integer> set= new HashSet<>();

for (int i = 0; i < 10000; i++) {
  set.add(i);
}

This gets me what I'm after but I'm wondering if there is a more efficient way. If the numbers were small I'd probably add them to an array by hand but that does not work here as 10,000 is too large. Thanks for any help.

John Crux
  • 11
  • 2
  • This is the most performant code. Define what you mean by efficient way. Also, are you sure you need a `Set` instead of `List` (of course, they serve different purposes)? – Arvind Kumar Avinash Jun 10 '20 at 17:51
  • Sorry, I guess by efficient I meant best performance. The function under test requires a set so I believe I'm stuck with only certain data structures. – John Crux Jun 10 '20 at 17:58
  • Your code is already most performant. Do not change it unless you need to write it in a different style. – Arvind Kumar Avinash Jun 10 '20 at 18:00

0 Answers0