What does keySet () do. ToArray (new Double [0])?

What does the next comeback do? And how can I use this value:

private Map<Double, String> theLabels = new HashMap<Double, String>(); public Double[] getTheLabels() { return theLabels.keySet().toArray(new Double[0]); } 

Is it correct?

 Double[] aD = theClassInQuestion.getTheLabels(); 

Thanks in advance.

Hjw

+6
source share
2 answers

It converts a set of keys into an array of Double s. The new Double[0] parameter is required for the / JVM compiler to output the correct type for the returned array. (If the array parameter is long enough to store all the key values, it will be used, otherwise a new array of the same runtime type with the required length will be created.)

Here is the Javadoc .

The correct specification of the return value.

+4
source

This is correct, however, unless the labels are empty, redistributing the array is mandatory. Here is an alternative:

 public Double[] getTheLabels() { return theLabels.keySet().toArray(new Double[theLabels.size()]); } 
+6
source

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


All Articles