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:

 public @Data class EntityObjectDTO  {
     @JsonInclude(JsonInclude.Include.NON_NULL)
     @JsonIgnoreProperties(ignoreUnknown = true)
     @Data
     public static class Attributes {

         private String name;

         private AttributeType attributeType;

         private String dataType;

     }
}

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:

    EntityObjectDTO  obj= new EntityObjectDTO ();
    obj.getAttributes().setName("name1");
    obj.getAttributes().setAttributeType(REFERRED);
    obj.getAttributes().setDataType("Vehicle");

    obj.getAttributes().setName("name2");
    obj.getAttributes().setAttributeType(REFERRED);
    obj.getAttributes().setDataType("Vehicle");

    obj.getAttributes().setName("name3");
    obj.getAttributes().setAttributeType(REFERRED);
    obj.getAttributes().setDataType("Person");
 

Expected in a HashMap:

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

This is what I tried:

      Map<String, String> myMap = obj.getAttributes().stream()
            .filter(entity -> AttributeType.REFERRED.equals(entity.getAttributeType()))
            .collect(Collectors.toMap(EntityObjectDTO.Attributes::getDataType, e -> e.getName()));<br/>

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,

public class EntityObjectDTO {

    private Attributes attributes;

    public Attributes getAttributes() {
        return attributes;
    }

    public void setAttributes(Attributes attributes) {
        this.attributes = attributes;
    }
}

and Attributes as below,

public class Attributes {
    private String name;
    private String dataType;
    private AttributeType attributeType;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public AttributeType getAttributeType() {
        return attributeType;
    }

    public void setAttributeType(AttributeType attributeType) {
        this.attributeType = attributeType;
    }
}

Then the below is fine to you,

 public static void main(String[] args) {
            EntityObjectDTO obj= new EntityObjectDTO ();

            Attributes attributes = new Attributes();
            attributes.setName("name1");
            attributes.setDataType("Vehicle");
            attributes.setAttributeType(AttributeType.REFERRED);
            obj.setAttributes(attributes);

            Attributes attributes1 = new Attributes();
            attributes1.setName("name2");
            attributes1.setDataType("Vehicle");
            attributes1.setAttributeType(AttributeType.REFERRED);
            obj.setAttributes(attributes1);


            Attributes attributes2 = new Attributes();
            attributes2.setName("name3");
            attributes2.setDataType("Person");
            attributes2.setAttributeType(AttributeType.REFERRED);
            obj.setAttributes(attributes2);


            List<Attributes> list = List.of(attributes1, attributes2, attributes);

            Map<String, String> collect = list.stream()
                                              .filter(e -> AttributeType.REFERRED.equals(e.getAttributeType()))
                                              .collect(Collectors.groupingBy(Attributes::getDataType,
                                                      Collectors.mapping(Attributes::getName,
                                                              Collectors.joining(","))));

            System.out.println(collect);

}

Please note that i used separate class.

Advertisement