0% found this document useful (0 votes)
253 views4 pages

Java Treemap for Cricket Data Analysis

The document defines a TreemapHandson class with methods to process cricket match data stored as strings. It includes methods to: 1) Create TreeMaps to store player positions and scores from a cricket data string 2) Create a TreeMap of matches with player names mapped to match details objects 3) Implement a getQuery method to return filtered player data based on different query parameters

Uploaded by

Siva Karthick
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
253 views4 pages

Java Treemap for Cricket Data Analysis

The document defines a TreemapHandson class with methods to process cricket match data stored as strings. It includes methods to: 1) Create TreeMaps to store player positions and scores from a cricket data string 2) Create a TreeMap of matches with player names mapped to match details objects 3) Implement a getQuery method to return filtered player data based on different query parameters

Uploaded by

Siva Karthick
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package com.

fresco;
import [Link].*;
import [Link].*;
import [Link].*;

public class TreemapHandson {


int noofmatches = 0;
int score;
TreemapHandson()
{
}
TreemapHandson(int noofmatches,int score)
{
[Link]=score;
[Link]=noofmatches;
}
int maximum = 0;
public TreeMap<Integer, String> createPlayerPositionMap(String cricketDataset)
{

TreeMap<Integer,String> listofPlayers=new TreeMap<Integer,String>();


String[] playerslist = [Link]("\\|");
for(int i=0;i<[Link];i++)
{
String[] players = playerslist[i].split(",");
[Link]([Link](players[0]),players[1]);
}
//[Link](listofPlayers);
return listofPlayers;

public TreeMap<String, Integer> createPlayerScoreMap(String cricketDataset) {

TreeMap<String,Integer> listofPlayers=new TreeMap<String,Integer>();


String[] playerslist = [Link]("\\|");
for(int i=0;i<[Link];i++)
{
String[] players = playerslist[i].split(",");
[Link](players[1],[Link](players[2]));
}
//[Link](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 = [Link]("\n");
//[Link]([Link](cricketarray));
for(int f=0;f<[Link];f++)
{
int count = 0;

TreeMap<Integer,String> list =
createPlayerPositionMap(cricketarray[f]);
TreeMap<String,Integer> list1 =createPlayerScoreMap(cricketarray[f]);

// [Link](list);
// [Link](list1);
// [Link]([Link](1));
// [Link]([Link]([Link](1)));
if([Link]([Link](1)))
{
max = [Link]([Link](1)) + [Link]([Link](1));
[Link]([Link](1),max);
if([Link]([Link](1)))
{
count++;
}
[Link]([Link](1),new TreemapHandson(count,max));
}
else
{ count++;
[Link]([Link](1), [Link]([Link](1)));
[Link]([Link](1),new
TreemapHandson(count,[Link]([Link](1))));
}

return cricketdetails;
}
public String getQuery(String cricketDataset, String query) {
String resultoutput = "";
if([Link]("3"))
{
//[Link]("HI");
TreeMap<String,TreemapHandson> crickdetails =
createMatchesMap(cricketDataset);
//[Link](crickdetails);
int max = 0;
for([Link]<?, ?> entry: [Link]()) {
//[Link]([Link]([Link]()).score);
//[Link]([Link]([Link]()).noofmatches);
if([Link]([Link]()).score > max)
{
max = [Link]([Link]()).score;
}
}
Set<String> set1 = [Link]();
for(String key : set1)
{
if([Link](key).score == max)
{
resultoutput = resultoutput + "The Efficient Opener is "+ key;

}
}
return resultoutput;
}
String [] threshold = [Link](" ");
TreeMap<Integer,String> listofPlayers =
createPlayerPositionMap(cricketDataset);
TreeMap<String,Integer> playersscore =
createPlayerScoreMap(cricketDataset);

if(threshold[0].equals("1"))
{ resultoutput = "";
String [] startend = [Link](" ");

int startposition = [Link](startend[1]);


int endposition = [Link](startend[2]);
// [Link](startposition);
// [Link](endposition);

// Get entry set of the TreeMap using entrySet


// method
Set<[Link]<Integer, String> > entrySet = [Link]();
// Convert entrySet to Array using toArray method
[Link]<Integer, String>[] entryArray = [Link](new
[Link][[Link]()]);

// For loop for iteration and printing


for (int i = startposition -1; i < endposition; i++)
{
// Get Key using index and print
//[Link](entryArray[i].getKey() + " " +
entryArray[i].getValue());
resultoutput = resultoutput + entryArray[i].getKey() + " " +
entryArray[i].getValue();
resultoutput = resultoutput +"\n";
}
//[Link](resultoutput);
return resultoutput;
}
else if(threshold[0].equals("2"))
{ resultoutput = "";
int thresholdscore = [Link](threshold[1]);
TreeMap<Integer,String> result=new TreeMap<>();
// getting keySet() into Set
Set<String> set1 = [Link]();
// for-each loop
for(String key : set1)
{
if([Link](key) > thresholdscore)
{
for([Link]<Integer, String> entry: [Link]())
{
if([Link]().equals(key)) {
[Link]([Link](),key);
}
}
}
}
Set<Integer> resultset = [Link]();
for(Integer key1 : resultset)
{
resultoutput = resultoutput + key1 + " "+[Link](key1) +"\n";
}
return resultoutput;
}

return null;
}
}

Common questions

Powered by AI

The `createMatchesMap` method determines the most efficient opener by iterating through the cricket dataset to compile a map of players, associating them with their total scores and count of matches played using helper maps for position and score mapping. It accumulates the score and match count for each player, updating these metrics whenever a player is reencountered as an opener. The `getQuery` method uses this map to identify the player with the highest score among openers by iterating through the entries. When the query number '3' is received, it identifies the player as 'The Efficient Opener' having the maximum score .

The `TreemapHandson` class could be refactored to improve modularity by separating data processing logic from data storage and retrieval into distinct classes. One approach is to create a separate `Player` class encapsulating player attributes like name, score, and matches, allowing `TreemapHandson` to focus solely on aggregation and querying. Additionally, introducing an interface for dataset operations would facilitate extensibility and testing. Extracting utility functions for string manipulation or data parsing into helper classes would further enhance separation of concerns, leading to cleaner, maintainable code structures .

The methods in `TreemapHandson` assume well-formed input, lacking explicit error handling for malformed data or invalid queries, risking exceptions such as `NumberFormatException` or `ArrayIndexOutOfBoundsException`. Improvements include adding input validation checks to ensure data conforms to expected formats before processing. Try-catch blocks could be used to gracefully handle exceptions and log errors or return meaningful messages. For queries, validation logic could ensure arguments are within valid ranges and format, enhancing robustness in handling diverse input scenarios .

To adapt `TreemapHandson` to handle additional player statistics like batting average, the class would need an additional field to store this statistic. Constructors would require adjustments to initialize this new field, and additional logic would need to be added to compute or update the batting average based on existing scores and matches data. Existing methods would need updates to compute batting average during data aggregation, potentially requiring dividing total score by the number of matches. Methods that query data could be extended to include batting average in their output, increasing the class's functionality for comprehensive statistical analysis .

The `createPlayerPositionMap` method processes the cricket dataset by first splitting the dataset string at each '|' character to obtain individual player details. Each player's data string is then further split by ',' to extract the position and player name. These details are stored in a `TreeMap` where the player's position (as an integer) is the key and the player's name (as a string) is the value. This `TreeMap<Integer, String>` provides a sorted mapping of player positions to player names .

The use of `TreeMap` ensures efficiency in managing player data because it provides a sorted map implementation, automatically ordering keys based on their natural ordering or by a specified comparator. This is particularly useful for operations within `TreemapHandson`, such as maintaining ordered access to player positions and efficiently finding players above a score threshold. Additionally, `TreeMap` supports logarithmic time complexity for insertion, deletion, and lookup operations, contributing to both correctness in maintaining the order and efficiency in data retrieval and processing, vital for data integrity and performance in handling cricket datasets .

Splitting strings is crucial in `TreemapHandson` to parse and break down the cricket dataset into meaningful components such as player names and scores. The dataset is separated by delimiters like '|' and ','. This technique allows efficient transformation of raw dataset strings into structured data pieces for storage in maps. However, its limitations include reliance on fixed delimiters, which may fail if the dataset format changes or if delimiters appear within data values, potentially leading to incorrect parsing unless robust validation and exception handling are implemented .

The `maximum` attribute in the `TreemapHandson` class appears to serve as a member variable, but it is not effectively utilized within the provided class methods. Its presence suggests potential use for tracking maximum values, perhaps in comparing scores or matches, though its lack of usage indicates it is either a placeholder or intended for future implementation. To give it significance, methods would need to explicitly set and utilize this variable to track the maximum score or number of matches for a player, thereby providing meaningful insights from the dataset .

Within the `createMatchesMap` method, the `TreemapHandson` constructor is used to create objects that encapsulate the number of matches played and the total score for each player. These objects are instances of the `TreemapHandson` class, initialized with the count of matches and cumulative scores. When adding or updating entries in the `cricketdetails` map, these `TreemapHandson` objects allow the method to efficiently aggregate and associate a player's performance data (match count and total score) with their name as a key in the `TreeMap<String, TreemapHandson>` .

The `getQuery` method retrieves players who have scored above a specific threshold by first parsing the threshold score from the query string. It then iterates over the entries of a pre-created `TreeMap<String, Integer>` that maps player names to their scores. For each player surpassing the threshold, it identifies their positional keys from another `TreeMap<Integer, String>` that maps positions to player names and populates a result `TreeMap<Integer, String>`. Finally, it assembles the set of players, ordered by their position, who exceed the threshold score and formats this into a result string .

You might also like