Define a POJO class Country with countryId and CountryName as member variables
- Define a POJO class Player with playerName, matchesPlayed, runs, highestScore, Country as member variables
- Create a class StreamOperationsExercise as below
- Create a List of Players, as a static variable, with some player objects
Define following static methods and provide the implementation using Java 8 streams API as given below
- displayPlayersForCountry(String country)
Display the name of players whose highest score is more than 100 and belong to a particular country
- getPlayerNames
Return a LinkedList containing names of all Players, whose have scored more than 5000 runs, sorted in descending order of names
- getAverageRunsByCountry(String country)
Return the average runs scored by players from a particular Country
- getPlayerNamesSorted
Return a list with names of Players sorted as per country and then by matchesPlayed(descending)
Execute the above methods in the main method.
class Country {
private int countryId;
private String countryName;
Country(int a, String b) {
countryId = a;
countryName = b;
}
}
class Player {
private String playerName;
private int matchedPlayed;
private int runs;
private double highestScore;
private Country country;
Player (String a, int b, int c, double d, Country e) {
playerName = a;
matchedPlayed = b;
runs = c;
highestScore = d;
country = e;
}
}
class StreamOperationsExercise {
private static List<Player> players = new ArrayList<Player>() {
new Playes("a", 3, 45, 43.45, new Country(2334, "Tajikistan"))
};
void displayPlayersForCountry(String country) {
StreamOperationsExercise.players.stream()
.filter(p -> p.country.countryName.equals(country))
.forEach(p -> System.out.println(p));
}
}
Comments
Leave a comment