问题
public class Call {
private String status;
private String callName;
}
I have a list of calls and i have to create a summary, like this:
public class CallSummary {
private String callName;
private List<ItemSummary> items;
}
public class itemSummary {
private String status;
private Integer percentage;
}
My goal is show a percentage of calls with some status like :
INBOUND_CALL : {
FAILED = 30%
SUCCESS = 70%
}
how can i do it using java 8 stream and Collectors ?
回答1:
The idea behind the grouping would be to nest is in such a way that you have a call name and then status based count lookup available. I would also suggest using an enumeration for the status
enum CallStatus {
FAILED, SUCCESS
}
and adapting it in other classes as
class Call {
private CallStatus status;
private String callName;
}
Then you can implement a nested grouping and start off with an intermediate result such as:
List<Call> sampleCalls = List.of(new Call(CallStatus.SUCCESS,"naman"),new Call(CallStatus.FAILED,"naman"),
new Call(CallStatus.SUCCESS,"diego"), new Call(CallStatus.FAILED,"diego"), new Call(CallStatus.SUCCESS,"diego"));
Map<String, Map<CallStatus, Long>> groupedMap = sampleCalls.stream()
.collect(Collectors.groupingBy(Call::getCallName,
Collectors.groupingBy(Call::getStatus, Collectors.counting())));
which would give you an output of
{diego={FAILED=1, SUCCESS=2}, naman={FAILED=1, SUCCESS=1}}
and you can further evaluate the percentages as well. (though representing them in Integer
might lose precision depending on how you evaluate them further.)
To solve it further, you can keep another Map
for the name-based count lookup as:
Map<String, Long> nameBasedCount = calls.stream()
.collect(Collectors.groupingBy(Call::getCallName, Collectors.counting()));
and further, compute summaries of type CallSummary
in a List
as :
List<CallSummary> summaries = groupedMap.entrySet().stream()
.map(entry -> new CallSummary(entry.getKey(), entry.getValue().entrySet()
.stream()
.map(en -> new ItemSummary(en.getKey(), percentage(en.getValue(),
nameBasedCount.get(entry.getKey()))))
.collect(Collectors.toList()))
).collect(Collectors.toList());
where percentage
count be implemented by you using the signature int percentage(long val, long total)
aligned with the datatype chosen in ItemSummary
as well.
Sample result:
[
CallSummary(callName=diego, items=[ItemSummary(status=FAILED, percentage=33), ItemSummary(status=SUCCESS, percentage=66)]),
CallSummary(callName=naman, items=[ItemSummary(status=FAILED, percentage=50), ItemSummary(status=SUCCESS, percentage=50)])
]
回答2:
The following collects to a status -> percent map which you can then convert to you output model. This code assumes a getStatus
method.
List<Call> calls;
Map<String,Double> statusPercents = calls.stream()
.collect(Collectors.groupingBy(Call::getStatus,
Collectors.collectingAndThen(Collectors.counting(),
n -> 100.0 * n / calls.size())));
I realise this code is a bit hard to read. The chain of collectors group the calls by status and then counts each group and finally converts to a percent. You could (arguably) make it more readable by having interim variables for the collectors:
var percentFunction = n -> 100.0 * n / calls.size();
var collectPercent = collectingAndThen(count(), percentFunction);
var collectStatusPercentMap = groupingBy(Call::getStatus, collectPercent);
You also want to group by call name but that's really just the same thing - using groupingBy
and then reducing the list of calls to a CallSummary
.
来源:https://stackoverflow.com/questions/60330244/how-to-use-java-stream-to-group-fields-and-create-a-summary