Karthik Cherukunnumal
Karthik Cherukunnumal

Reputation: 43

How to join 2nd list with first list based on a field in 1st list?

I have 2 lists, the 1st list contains a field that can be duplicated and seconds list contain the same field without duplicate with other fields. What I need is to join the two list to one based on that common field.

List 1 :

[
{
    "id" : "1"
    "name : "hello",
    "item_name": "Name"

},
{
    "id" : "1"
    "name : "hi"
    "item_name": "Name2"

},
{
    "id" : "2"
    "name : "hello"
    "item_name": "Name"

}
]

Second List :

[{
    "id" : "1"
    "age" :  "10",
    "place" : "0",
    "date" : "0"
},
{
    "id" : "2"
    "age" :  "12",
    "place" : "1",
    "date" : "0
}]

Expected Result :

[
    {
            "id" : "1"
            "name : "hello",
            "item_name": "Name",
            **"age" :  "10",
            "place" : "0",
            "date" : "0**
    },
    {
            "id" : "1"
            "name : "hi"
            "item_name": "Name2"
            **"age" :  "10",
            "place" : "0",
            "date" : "0"**
    },
    {
            "id" : "2"
            "name : "hello"
            "item_name": "Name"
            **"age" :  "12",
            "place" : "1",
            "date" : "0"**
    }
    ]

Bold areas are joined from the second list Please help in this

I have tried :

Map<String, String> listMap = list2
            .stream()
            .collect(Collectors.toMap(List1Entity::getId,
                    List1Entity::getId));
    list1.forEach(a -> {
        a.setPlace(listMap.get(a.getId()));
    });

But getting error at .collect(),

no instances or type variable exist so that List2Entity conforms to List1Entity

Upvotes: 0

Views: 118

Answers (2)

Qword
Qword

Reputation: 90

Your requirements are a bit unclear when it comes to define the joining part between the two lists.

You can start with something like this and adapt mapFunction to your needs.

String list1 = "[{\"id\":\"1\",\"name\":\"hello\",\"item_name\":\"Name\"},{\"id\":\"1\",\"name\":\"hi\",\"item_name\":\"Name2\"},{\"id\":\"2\",\"name\":\"hello\",\"item_name\":\"Name\"}]";
String list2 = "[{\"id\":\"1\",\"age\":\"10\",\"place\":\"0\",\"date\":\"0\"},{\"id\":\"2\",\"age\":\"12\",\"place\":\"1\",\"date\":\"0\"}]";

ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> l1 = mapper.readValue(list1, new TypeReference<List<Map<String, Object>>>(){});
List<Map<String, Object>> l2 = mapper.readValue(list2, new TypeReference<List<Map<String, Object>>>(){});

final UnaryOperator<Map<String, Object>> mapFunction = map -> {
    final String id = map.get("id").toString();
    l2.stream()
        .filter(map2 -> map2.get("id").toString().equals(id))
        .findAny()
        .ifPresent(map::putAll);
    return map;
};

List<Map<String, Object>> result = l1
        .stream()
        .map(mapFunction)
        .collect(Collectors.toList());

Upvotes: 0

smakosz
smakosz

Reputation: 51

Did you mean List2Entity in the first statement?

Map<String, String> listMap = list2
        .stream()
        .collect(Collectors.toMap(List2Entity::getId,
                List2Entity::getPlace));

Upvotes: 1

Related Questions