Question: I am getting these errors for getcountgroupbymedia Cannot infer type argument(s) for flatMap(Function super T,? extends Stream extends R>>) gettotalfeesgroupbymedia has errors too and gettotalitemsgroupbymedia

I am getting these errors for getcountgroupbymedia Cannot infer type argument(s) for flatMap(Function super T,? extends Stream extends R>>)

gettotalfeesgroupbymedia has errors too and gettotalitemsgroupbymedia kindly could you solve it and replace it with the correct code

package adminSite;

import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors;

import yourPrime.*;

public class FuncUtil { private Map userDb = new HashMap(); public FuncUtil(Map userDb) { this.userDb = userDb; } public Map getUserDb() { return userDb; } public void addSubscriber(Subscriber subscriber) { Supplier userId = () -> generateId().toString(); String id = userId.get(); subscriber.setUserID(id); userDb.putIfAbsent(id, subscriber); } public Integer generateId() { return new Random().nextInt(80000); } public boolean modifyPassword(String userId, String newPassword) { Subscriber subscriber = userDb.get(userId); subscriber.setPassword(newPassword); userDb.replace(userId, subscriber); if (userDb.get(userId).getPassword().equals(newPassword)) return true; else return false; } public boolean deleteSubscriber(String userId) { userDb.remove(userId); if (userDb.get(userId) == null) return true; else return false; } // TODO refactor the implementation of the searchSubscriber() method using Java 8 stream // and lambda expression. You can also use the existing interfaces if you prefer (but not required). // public List searchSubscriber(String keyword) { return userDb.values().stream() .filter(subscriber -> subscriber.getName().contains(keyword) || subscriber.getUserID().contains(keyword)) .collect(Collectors.toList()); } // TODO refactor the implementation of the total sum of fees calculation using Java 8 streams // and lambda expression. You can also use the exisiting interfaces if you prefer (but not required). // public double calculateOverdueFees() { return userDb.values().stream() .mapToDouble(Subscriber::getFees) .sum(); } // TODO refactor the implementation of the printAllSubscriber() method here with Java 8 stream and method // reference. You MUST use the function interface already defined here. The method will also print out the // details according to the sort by option - name of the subscriber, and the outstanding fees. // public void printAllSubscribers(String sortBy) { Function details = p -> p.getName() + " with outstanding amount = " + String.format("%.2f", p.getFees());

Comparator comparator; if (sortBy.equals("name")) { comparator = Comparator.comparing(Subscriber::getName); } else if (sortBy.equals("fees")) { comparator = Comparator.comparing(Subscriber::getFees); } else { throw new IllegalArgumentException("Invalid sort by option"); }

userDb.values().stream() .sorted(comparator) .map(details) .forEach(System.out::println); }

// TODO create a method to return the average outstanding fees from all accounts using Java 8 stream and lambda expression. // public double getAverageOutstanding() { return userDb.values().stream() .mapToDouble(Subscriber::getFees) .average() .orElse(0); } // TODO create a method to return the outstanding fees from all accounts group by the media type. // You should make use of Java 8 Streams and lambda expression to do this - return map // public Map getTotalFeesGroupByMedia() { // Create a new Map to store the total fees for each media type Map totalFees = new HashMap();

// Loop through each media type for (MyMedia.Media media : MyMedia.Media.values()) {

// Compute the total fees for the current media type Double fees = userDb.values().stream() .flatMap(subscriber -> subscriber.getMedia().get(media).stream()) .mapToDouble(MyMedia::getFees) .sum();

// Store the total fees for the current media type in the Map totalFees.put(media.toString(), fees); }

// Return the Map of total fees for each media type return totalFees; } public Map getCountGroupByMedia() { // Create a new Map to store the item counts for each media type Map itemCounts = new HashMap();

// Loop through each media type for (MyMedia.Media media : MyMedia.Media.values()) {

// Count the number of items for the current media type Long count = userDb.values().stream() .flatMap(subscriber -> subscriber.getMedia().get(media).stream()).count();

// Store the item count for the current media type in the Map itemCounts.put(media.toString(), count); }

// Return the Map of item counts for each media type return itemCounts; }

public Map getTotalItemsGroupByMedia() { return accounts.stream() .flatMap(account -> account.getItems().stream()) .collect(Collectors.groupingBy(List::getMediaType, Collectors.counting())); }

// TODO create a method to return the total number of items from all accounts using Java 8 streams and lambda expression. // group by the media type. Use the programming logic (idioms) that you've implemented in the getTotalFeesGroupByMedia() // method as a source of inspiration in completing this method. // }

I am getting these errors for getcountgroupbymedia Cannot infer type argument(s) forflatMap(Function super T,? extends Stream extends R>>) gettotalfeesgroupbymedia has errors too andgettotalitemsgroupbymedia kindly could you solve it and replace it with the correct

@override

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!