1

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?

4
  • What you are doing is descent enough. Goahed. Commented Aug 24, 2015 at 9:09
  • Well, you still should avoid raw-types ;). Commented Aug 24, 2015 at 9:10
  • @Tom yes.. that is the reason i posted this as, I am getting some warnings out of this code..!!! Commented Aug 24, 2015 at 9:11
  • So? Since you already know what generic types are (you already using them), you should be able to not use a raw Iterator. So add the generic types. You can do that :). Commented Aug 24, 2015 at 9:12

2 Answers 2

3

Your solution is nearly working, so you should just consider couple of things:

  1. Iterator is generic type, so you shouldn't do additional casting
  2. There's no point in using Iterator explicitly: for loop is doing that implicitly for every Iterable (which Collection -> Set is)
  3. You're using only key in Map.Entry, but you can use value too, there's no need to call get two additional times

So, the conversion would be:

for (Map.Entry<String, Set<String>> entry: testmap.entrySet()) {
    testmapArray.put(entry.getKey(),
            entry.getValue().toArray(new String[entry.getValue().size()]));
}
Sign up to request clarification or add additional context in comments.

Comments

0

In case you are using Java 8 then you can achieve it using streams:

testmapArray = testmap.entrySet().stream().collect(Collectors.toMap(entry ->  entry.getKey(), entry-> setToArray(entry.getValue())));

The method setToArray is:

private static String[] setToArray(Set<String> set) {
        return set.toArray(new String[set.size()]);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.