The document contains code for a FuncUtil class that uses Java 8 streams and lambda expressions to perform operations on a Map of Subscriber objects. It includes TODO comments asking to refactor existing methods and implement new methods to calculate statistics by grouping and filtering the Subscriber data. The author is getting errors on some methods and asks for help resolving them.
9548086042 for call girls in Indira Nagar with room service
Solve Java 8 stream errors get count and totals by media
1. I am getting these errors for getcountgroupbymedia Cannot infer type argument(s) for <R>
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<String, Subscriber> userDb = new HashMap<>();
public FuncUtil(Map<String, Subscriber> userDb) {
this.userDb = userDb;
}
public Map<String, Subscriber> getUserDb() {
return userDb;
}
public void addSubscriber(Subscriber subscriber) {
Supplier<String> 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);
2. 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<Subscriber> 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<Subscriber, String> details = p -> p.getName() + " with outstanding amount = " +
String.format("%.2f", p.getFees());
3. Comparator<Subscriber> 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<String, Double> getTotalFeesGroupByMedia() {
// Create a new Map to store the total fees for each media type
Map<String, Double> 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();
4. // 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<String, Long> getCountGroupByMedia() {
// Create a new Map to store the item counts for each media type
Map<String, Long> 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<MediaType, Long> 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.
//
}