Java8 convert List to Map


Chung Nguyen

Sample Data


[
    {"uid":"1","name":"阿花","age":38,"gender":"F"},
    {"uid":"2","name":"阿瓜","age":28,"gender":"M"}
]

Entity


@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(value = {
    "uid",
    "name",
    "age",
    "gender"
})
public class Customer {
    // creativeId equals adId
    @JsonProperty("uid")
    private String uid;
    @JsonProperty("name")
    private String name;
    @JsonProperty("age")
    private Integer age;
    @JsonProperty("gender")
    private String gender;
}

Test


@Test
public void test() throws IOException {
    List<Customer> customerList =
        objectMapper.readValue(json, new TypeReference>() {});

    Map customerMap = customerList.stream()
        .collect(Collectors.toMap(
            Customer::getUid,       // key
            customer -> customer)); // value

    System.out.println(objectMapper.writeValueAsString(customerMap));
}

Result


{
  "1": {
    "uid": "1",
    "name": "阿花",
    "age": 38,
    "gender": "F"
  },
  "2": {
    "uid": "2",
    "name": "阿瓜",
    "age": 28,
    "gender": "M"
  }
}


沒有留言:

張貼留言