package com.
fresco;
import java.util.*;
import java.lang.System.*;
import java.lang.*;
public class TreemapHandson {
int noofmatches = 0;
int score;
TreemapHandson()
{
}
TreemapHandson(int noofmatches,int score)
{
this.score=score;
this.noofmatches=noofmatches;
}
int maximum = 0;
public TreeMap<Integer, String> createPlayerPositionMap(String cricketDataset)
{
TreeMap<Integer,String> listofPlayers=new TreeMap<Integer,String>();
String[] playerslist = cricketDataset.split("\\|");
for(int i=0;i<playerslist.length;i++)
{
String[] players = playerslist[i].split(",");
listofPlayers.put(Integer.parseInt(players[0]),players[1]);
}
//System.out.println(listofPlayers);
return listofPlayers;
public TreeMap<String, Integer> createPlayerScoreMap(String cricketDataset) {
TreeMap<String,Integer> listofPlayers=new TreeMap<String,Integer>();
String[] playerslist = cricketDataset.split("\\|");
for(int i=0;i<playerslist.length;i++)
{
String[] players = playerslist[i].split(",");
listofPlayers.put(players[1],Integer.parseInt(players[2]));
}
//System.out.println(listofPlayers);
return listofPlayers;
}
public TreeMap<String,TreemapHandson > createMatchesMap(String cricketDataset)
{
int max=0;
TreeMap<String, TreemapHandson> cricketdetails = new TreeMap<String,
TreemapHandson>();
TreeMap<String,Integer> cricket =new TreeMap<>();
String[] cricketarray = cricketDataset.split("\n");
//System.out.println(Arrays.asList(cricketarray));
for(int f=0;f<cricketarray.length;f++)
{
int count = 0;
TreeMap<Integer,String> list =
createPlayerPositionMap(cricketarray[f]);
TreeMap<String,Integer> list1 =createPlayerScoreMap(cricketarray[f]);
// System.out.println(list);
// System.out.println(list1);
// System.out.println(list.get(1));
// System.out.println(list1.get(list.get(1)));
if(cricket.containsKey(list.get(1)))
{
max = cricket.get(list.get(1)) + list1.get(list.get(1));
cricket.replace(list.get(1),max);
if(cricketdetails.containsKey(list.get(1)))
{
count++;
}
cricketdetails.put(list.get(1),new TreemapHandson(count,max));
}
else
{ count++;
cricket.put(list.get(1), list1.get(list.get(1)));
cricketdetails.put(list.get(1),new
TreemapHandson(count,list1.get(list.get(1))));
}
return cricketdetails;
}
public String getQuery(String cricketDataset, String query) {
String resultoutput = "";
if(query.equals("3"))
{
//System.out.println("HI");
TreeMap<String,TreemapHandson> crickdetails =
createMatchesMap(cricketDataset);
//System.out.println(crickdetails);
int max = 0;
for(Map.Entry<?, ?> entry: crickdetails.entrySet()) {
//System.out.println(crickdetails.get(entry.getKey()).score);
//System.out.println(crickdetails.get(entry.getKey()).noofmatches);
if(crickdetails.get(entry.getKey()).score > max)
{
max = crickdetails.get(entry.getKey()).score;
}
}
Set<String> set1 = crickdetails.keySet();
for(String key : set1)
{
if(crickdetails.get(key).score == max)
{
resultoutput = resultoutput + "The Efficient Opener is "+ key;
}
}
return resultoutput;
}
String [] threshold = query.split(" ");
TreeMap<Integer,String> listofPlayers =
createPlayerPositionMap(cricketDataset);
TreeMap<String,Integer> playersscore =
createPlayerScoreMap(cricketDataset);
if(threshold[0].equals("1"))
{ resultoutput = "";
String [] startend = query.split(" ");
int startposition = Integer.valueOf(startend[1]);
int endposition = Integer.valueOf(startend[2]);
// System.out.println(startposition);
// System.out.println(endposition);
// Get entry set of the TreeMap using entrySet
// method
Set<Map.Entry<Integer, String> > entrySet = listofPlayers.entrySet();
// Convert entrySet to Array using toArray method
Map.Entry<Integer, String>[] entryArray = entrySet.toArray(new
Map.Entry[entrySet.size()]);
// For loop for iteration and printing
for (int i = startposition -1; i < endposition; i++)
{
// Get Key using index and print
//System.out.println(entryArray[i].getKey() + " " +
entryArray[i].getValue());
resultoutput = resultoutput + entryArray[i].getKey() + " " +
entryArray[i].getValue();
resultoutput = resultoutput +"\n";
}
//System.out.print(resultoutput);
return resultoutput;
}
else if(threshold[0].equals("2"))
{ resultoutput = "";
int thresholdscore = Integer.parseInt(threshold[1]);
TreeMap<Integer,String> result=new TreeMap<>();
// getting keySet() into Set
Set<String> set1 = playersscore.keySet();
// for-each loop
for(String key : set1)
{
if(playersscore.get(key) > thresholdscore)
{
for(Map.Entry<Integer, String> entry: listofPlayers.entrySet())
{
if(entry.getValue().equals(key)) {
result.put(entry.getKey(),key);
}
}
}
}
Set<Integer> resultset = result.keySet();
for(Integer key1 : resultset)
{
resultoutput = resultoutput + key1 + " "+result.get(key1) +"\n";
}
return resultoutput;
}
return null;
}
}