Question 5
Consider the Java code given below.
You may make use of the method description given below.
getOrDefault(Object key, V defaultValue): Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
import java.util.*;class Player{ String name; String year; int runs; // Constructor to initialize name, year and runs}public class MapTest{ public static void printPlayers(ArrayList<Player> pL) { var map = new TreeMap<String, Integer>(); for(Player p:pL) { map.put(p.name, map.getOrDefault(p.name, 0)+p.runs); } for (Map.Entry<String, Integer> e:map.entrySet()) { System.out.println(e.getKey()+" = "+e.getValue()); }}
public static void main(String[] args) { ArrayList<Player> pList = new ArrayList<Player>(); pList.add(new Player("Dhoni", "2015", 756)); pList.add(new Player("Kohli", "2017", 1050)); pList.add(new Player("Dhoni", "2017", 345)); pList.add(new Player("Kohli", "2016", 675)); printPlayers(pList); }}What will the output be?
Dhoni = 756
Kohli = 1050Dhoni = 345
Kohli = 675Dhoni = 1101
Kohli = 1725Kohli = 1725
Dhoni = 1101