I want to get a list of hashmap that grouped by year and find the customer_id that doesn't contain a group based on a list of customer_id.
This is an example of the dataset.
List tagList = new ArrayList<>();
# Customer
HashMap<String, Object> customerMap = new HashMap<>();
## feeding data example
customerMap.put("date", "2018");
customerMap.put("name", "John");
customerMap.put("custemer_no", "1a");
tagList.add(customerMap);
customer_id_list = ['1a', '2b', '3c']
customer_list = [
{
"date": "2019",
"name": "John",
"customer_id": "1a"
},
{
"date": "2019",
"name": "David",
"customer_id": "2b"
},
{
"date": "2020",
"name": "John",
"customer_id": "1a"
},
{
"date": "2020",
"name": "Alex",
"customer_id": "3c"
},
{
"date": "2021",
"name": "John",
"customer_id": "1a"
}
]
This is a sample output that I want.
missing_customer_list = [
{
"date": "2019",
"name": "Alex",
"customer_id": "3c"
},
{
"date": "2020",
"name": "David",
"customer_id": "2b"
},
{
"date": "2021",
"name": "David",
"customer_id": "2b"
},
{
"date": "2021",
"name": "Alex",
"customer_id": "3c"
}
]
Do you have any ideas how I can get this sample output using stream comprehension?
If I cannot filter the list directly using stream comprehension, using a for loop is fine too.
I found the way how to group by year for now, but don't know how to handle the rest of the filtering..
Please help me! thanks in advance
List<Customer> result = customer_list.stream()
.collect(Collectors.groupingBy(Customer::getDate))
UPDATE
Referring to the current answers,
I ve got stuck at the point where I couldn't create the new instance of customer.
So, Im planning to use for-loops to find the missing_customer_id during iteratation of customer_id_list and filtered_customer_list. Once I get the missing_customer_id in a list, I will try to re-create the Customers manually and add them into a new list to print