Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

map

What does the following return do? And how can I use that value:

private Map<Double, String> theLabels = new HashMap<Double, String>();

public Double[] getTheLabels() {
  return theLabels.keySet().toArray(new Double[0]);
  }

Is this correct?

Double[] aD = theClassInQuestion.getTheLabels();

Thanks in advance.

HJW

like image 385
Harald Wilhelm Avatar asked Apr 18 '11 11:04

Harald Wilhelm


1 Answers

It is correct, however, unless the theLabels is empty, the reallocation of the array is forced. Here is an alternative:

public Double[] getTheLabels() {
    return theLabels.keySet().toArray(new Double[theLabels.size()]);
}
like image 176
Maurice Perry Avatar answered Oct 06 '22 00:10

Maurice Perry