I want to add all key/value of a Map to a two dimensional array.
Does any body know a better way? I did as below but i search for a better way.
Map<String, String> memoMap = AvoCollection.getMemoMap();
String[][] data = new String[memoMap.size()][2];
int ii =0;
for(Map.Entry entry : memoMap.entrySet()){
data[ii][0] = entry.getKey().toString();
data[ii][1] = entry.getValue().toString();
ii++;
}
final DefaultTableModel model = new DefaultTableModel(data,new String[]{"Memo","ID"});
This may be marginally better, but not by much:
Map<String,String> memoMap = AvoCollection.getMemoMap();
String[][] data = new String[memoMap.size()][];
int ii =0;
for(Map.Entry<String,String> entry : memoMap.entrySet()){
data[ii++] = new String[] { entry.getKey(), entry.getValue() };
}
final DefaultTableModel model = new DefaultTableModel(data,new String[]{"Memo","ID"});
After declaring Map.Entry<String,String> you can stop calling toString; you can also create a two-element array with an aggregate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With