Skip to content
Advertisement

How To Store Values in Hashmap (comma Separated) with Same Key using Java Stream API?

I have a object named EntityObjectDTO like below:

JavaScript

This AttributeType is an enum and can have values (RAW,REFERRED,ORIGINAL). I am trying to store all the attributes names and dataType into Hashmap of String key and value. This DataType can be duplicate as well.
Input:

JavaScript

Expected in a HashMap:

{“Vehicle”,”name1,name2″}
{“Person”,”name3″}

This is what I tried:

JavaScript

But by this I am getting : java.lang.IllegalStateException: Message : Duplicate key Vehicle (attempted merging values name1 and name2)

How do I achieve the expected output by doing some modifications in existing code?

Advertisement

Answer

I assume your EntityObjectDTO as below,

JavaScript

and Attributes as below,

JavaScript

Then the below is fine to you,

JavaScript

Please note that i used separate class.

Advertisement