Q: Find the count of students who have failed (average < 50) and who have passed with distinction (average > 80)
input ->
[
[“Michael”, “70”],
[“Adam”, “85”]
[“George”, “80”]
[“Silva”, “71”]
[“George”, “37”]
[“Michael”, “21”]
]
output ->
total = 4
failed = 1
distinction = 1
public class Demo {
public static void main(String[] args) {
String[][] data = new String[][] { { "Michael", "70" }, { "Adam", "85" }, { "George", "80" }, { "Silva", "71" },
{ "George", "37" }, { "Michael", "21" } };
Map<String, List<String>> map = new HashMap<>();
for (int i = 0; i < data.length; i++) {
String[] names = data[i];
if (!map.containsKey(names[0])) {
map.put(names[0], Arrays.asList(names[1]));
} else {
List<String> values = new ArrayList<>();
values.addAll(map.get(names[0]));
values.addAll(Arrays.asList(names[1]));
map.put(names[0], values);
}
}
long count = map.entrySet().stream().count();
System.out.println("Total Candidate : "+ count);
for (Entry<String, List<String>> entry : map.entrySet()) {
Long x = evalute(entry.getValue());
if(x < 50)
System.out.println(entry.getKey() +" has failed");
if(x > 80)
System.out.println(entry.getKey() +" has distinction");
}
}
private static Long evalute(List<String> list) {
return (long) list.stream().mapToLong(Long::parseLong).average().getAsDouble();
}
}