Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert Map<String, String> to Map<MyClass, MyOtherClass>?

I have a simple Map<String, String> scanMap and I want to collect and covert it to a map of type of Map<CustomTag, CustomTagType>.

CustomTag has 2 fields properties – key & value and I want to inject the original map key & value into it. the 2nd CustomTagType is an enum.
I want to do something like this:

Map<String, String> scanMap = new HashMap<>();
scanMap.entrySet()
        .stream()
        .collect(Collectors.toMap(e -> CustomTag.builder().key(e.getKey()).value(e.getValue()).build(),
                        CustomTagType.SCAN_TAG);

but the e.getKey() & e.getValue() get ‘cannot resolve method’ errors.
What am I missing here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Both arguments to Collectors.toMap must be Functions (which are passed an element of the Stream), even if their parameter is unused.

Collectors.toMap(e -> CustomTag.builder().key(e.getKey()).value(e.getValue()).build(),
                 e -> CustomTagType.SCAN_TAG)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading