I am getting data in the form of Map<String, Set<String>> from a method. But I want it to be in the form of Map<String, String[]>.
I.e, i want to convert only map values from Set<String> to String[] array for a particular key.
I tried this way , iterating the loaded set map and getting the String[] array of values correctly for a key.
But I just want to know that, is there any better solution than this.
Map<String, Set<String>> testmap = new HashMap<String, Set<String>>();
Map<String, String[]> testmapArray = new HashMap<String, String[]>();
Set<String> testSet = new TreeSet<String>();
testSet.add("1");
testSet.add("2");
testSet.add("3");
testmap.put("A", testSet);
testmapArray.put("A", testSet.toArray(new String[testSet.size()]));
Set<String> testSet1 = new TreeSet<String>();
testSet1.add("4");
testSet1.add("5");
testSet1.add("6");
testmap.put("B", testSet1);
testmapArray.put("A", testSet1.toArray(new String[testSet1.size()]));
System.out.println(testmap);
Iterator it = testmap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
testmapArray.put(pair.getKey().toString(), testmap.get(pair.getKey()).toArray(new String[testmap.get(pair.getKey()).size()]));
}
System.out.println(testmapArray);
Can anyone help me?
Iterator. So add the generic types. You can do that :).