I am getting one list from an AS400 query and another from a SQL Server query. I need to merge these two lists into a single list. The first list is every asset in our inventory and looks like this:
[
{
"assetId": 0,
"type": "OV",
"truckNumber": "L122",
"longitude": 0,
"latitude": 0,
"id": 0
},
{
"assetId": 0,
"type": "PO",
"truckNumber": "SQ46",
"longitude": 0,
"latitude": 0,
"id": 0
}
]
The second list has more detail and looks like this, with both lists having one field in common, truckNumber:
[
{
"trailerGroup": "C",
"assetId": 308,
"loaded": false,
"dedicated": false,
"intermodal": false,
"sealed": false,
"truckNumber": "L122",
"companyOwned": true,
"onSite": false,
"customerId": "KTPH",
"id": 308,
"modified": {
"when": 1546498401156
},
"created": {
"when": 1546498401156
}
},
{
"trailerGroup": "C",
"assetId": 309,
"loaded": false,
"dedicated": false,
"intermodal": false,
"sealed": false,
"truckNumber": "SQ46",
"companyOwned": true,
"onSite": true,
"customerId": "KTPH",
"id": 309,
"modified": {
"when": 1546498401156
},
"created": {
"when": 1546498401156
}
}
]
I need to update any existing values like longitude while adding any that are not in the first query.
I tried some of the merge examples but none work at all or others just append the first list to the second list.
Possible Solution:
Anyone see an issue with this?
List<Trailer> trailers = null;
List<Trailer> trailersAS400 = null;
List<Trailer> trailersSQLServer = null;
try {
trailersAS400 = getTrailerAS400Proxy().getTrailers();
trailersSQLServer = getTrailerSQLServerProxy().getTrailers();
Map<String, Trailer> map = new HashMap<>();
for (Trailer t : trailersAS400) {
map.put(t.getNumber(), t);
}
for (Trailer t : trailersSQLServer) {
String key = t.getNumber();
if (map.containsKey(key)) {
map.get(key).setNumber(t.getNumber());
} else {
map.put(key, t);
}
}
trailers = new ArrayList<>(map.values());
}