29

Why do we need the argument new String[0] inside toArray?

saved = getSharedPreferences("searches", MODE_PRIVATE);
String[] mystring = saved.getAll().keySet().toArray(new String[0]);
xlm
  • 6,854
  • 14
  • 53
  • 55
user2580401
  • 1,840
  • 9
  • 28
  • 35

3 Answers3

39

So that you get back a String[]. The one without any argument gives back to you an Object[].

See you have 2 versions of this method:

By passing String[] array, you are using the generic version.


A better way to pass the String[] array would be to initialize it with the size of the Set, and not with size 0, so that there is not need to create a new array in the method:

Set<String> set = saved.getAll().keySet();
String[] mystring = set.toArray(new String[set.size()]);
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 4
    I personally have always found found this requirement kind of odd, it would make more sense to me to pass the `Class.class` – StormeHawke Aug 08 '13 at 21:28
  • 1
    @StormeHawke That would involve reflection, which carries a higher overhead than creating an array. – nanofarad Aug 08 '13 at 21:29
  • I suppose that makes sense. Still, it's not very intuitive this way. Seems to me they could have hidden this complexity somehow – StormeHawke Aug 08 '13 at 21:32
  • 4
    Ideally you should initialize the array to the correct size, so it can be filled in. Otherwise, a new array is created. – Sam Barnum Aug 08 '13 at 21:56
  • why some people use it to iterate over a hashmap? @RohitJain – Shilan Apr 05 '19 at 06:38
  • 2
    @SamBarnum Does anybody know, why Intellij IDEA (2018.1, 2019.2) [offers](https://pastenow.ru/74XPE) exactly an **empty** array? – Dan Brandt Nov 01 '19 at 15:40
  • 7
    @DanBrandt I believe with Java 8 it's actually more efficient to use the empty array. https://stackoverflow.com/a/29444594/14467 – Sam Barnum Nov 01 '19 at 16:46
  • what is the 0 represent for? will 0 be automatically replaced with set.size() event if I pass in 0? – Robin Jun 03 '20 at 18:01
6

To add some more details to the accepted answer, some IDEs (IntelliJ here) give helpful comments and explanations to why they rise a warning on an active inspection when using new String[c.size()] versus using new String[0] as an argument of toArray method:

There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation. This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).

Hope it helps to further understand why things are done a certain way.

Arnaud JOLLY
  • 171
  • 1
  • 3
  • NetBeans 18 just warned about `toArray(new Predicate[]{})` and suggested `toArray(Predicate[]::New)` instead. It would be interesting to know why. – Mark Wood Jul 02 '23 at 13:15
5

It's to provide a type for the return and prevent any compile-time ambiguity.

the signiture for that method call is: <T> T[] toArray(T[] a)

wheras the empty parameter one is Object[] toArray()

AdamSpurgin
  • 951
  • 2
  • 8
  • 28