Reputation: 113
Hi I am having trouble in adding the integer value of my map and merging the string value of it what I want to do is for example I have the following code:
List<Object[]> results = customerRepository.findByAddress();
for(Object[] object : results) {
Map<Object, Object> location = new LinkedHashMap<>();
location.put("Region", object[0]);
location.put("Tally", object[1]);
for(String obj : regionOne) {
if(object[0].equals(obj)) {
location.replace("Region", "Region I");
}
}
}
and this is the following data:
Region: Pasig
Tally: 2
Region: Taguig
Tally: 2
what I want is to add the value of Tally so 2 + 2 = 4 and merge and replace Pasig and Taguig to Region I
so the results should be
Region: Region I
Tally: 4
I have looked at the merge function and it seems that it is not what I'm looking for or I just didn't quite get it Thanks in advance.
Upvotes: 0
Views: 65
Reputation: 418
If you are using Java 8 or bigger, Map::computeIfPresent could be useful for your question.
And by the way, I think it is better to separate the business part from your code when asking questions.
Upvotes: 1