1

I have a Map and I want to convert it to Map. The keys from the first Map will be equal to the ones in the second. MyObject has a property that I want to use as the value for the second map. So what I have now is a for loop that does:

for (Map.Entry<String, MyObject> entry : originalMap.getTextMessages().entrySet()) {
  newMap.put(entry.getKey(), entry.getValue().getStringValue());
}

Now I've got the feeling it could be easier... can anyone shed a light on this? We're using Java 7 by the way but if you have some clever Java 8 function for this, please share it too.

2
  • 3
    This is 3 lines of code... what's not easy about this? Commented Nov 2, 2015 at 11:54
  • 1
    With Java 7 I think that is the easiest way. Commented Nov 2, 2015 at 11:55

4 Answers 4

3

It's quite straight forward with Java 8 Streams :

Map<String,String> newMap = 
    originalMap.entrySet().stream().collect(Collectors.toMap(e->e.getKey(),e->e.getValue().getStringValue()));

In Java 7 I think you can't do it with less code than you already have.

Sign up to request clarification or add additional context in comments.

Comments

0

Here's another Java 8 example with streams and a BiConsumer, all in a main method:

public static void main(String[] args) throws Exception {
    class MyObject {
        String s;
        MyObject(String s) {
            this.s = s;
        }
        String getStringValue() {
            return s;
        }
    }
    Map<String, MyObject> original = new HashMap<String, MyObject>();
    original.put("original", new MyObject("0"));
    Map<String, String> converted = new HashMap<String, String>();
    // iterates each entry and puts the conversion in the new map
    original.forEach((s,m) -> {converted.put(s, m.getStringValue());});
    System.out.println(converted);
}

Output

{original=0}

2 Comments

This to me is definitely more complicated than the original
@ControlAltDel consider the only relevant line here is the forEach. The rest is just setting up and context.
0

In Java7 you can do it with help of Apache commons collections:

import org.apache.commons.collections4.MapUtils;

Map<String, String> newMap = MapUtils.transformedMap(originalMap, TransformerUtils.<String>nopTransformer(), new Transformer<Object, String>() {
        @Override
        public String transform(final Object objectValue) {
            return objectValue.toString();
        }
    });

Comments

0

you can do it with help of guava:

import org.guava.collect.Maps;

Map<String, String> newMap = Maps.transformEntries(oldMap, new Maps.EntryTransformer<String, Object, String>() {
            @Override
            public String transformEntry(@Nullable String key, @Nullable Object value) {
                return value == null ? null : value.toString();
            }
        });

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.