In Java, I need with a sorting method that Find the top ten customers (by spending) from the hashmap and return a string array of the customers' ID/
public Order[] findTopCustomers(String date) {
Order[] customers = new Order[10];
HashMap<String,Order> date_orders = findDailyOrders(date);
return customers;
}
Assuming that you have support code ready with you and only need
to implement findTopCustomers() function. Order object has customer
info and amount spent on the basis of which we have to sort the
HashMap.
public Order[] findTopCustomer(String date) {
Order[] customers = new Order[10];
HashMap<String,Order> date_orders = findDailyOrders(date);
// findDailyOrders return the above Hashmap which will be sorted.
//Before Sorting
Map<String, Order> mp = new HashMap<String, Order>();
TreeMap<String, Order> tm = new TreeMap<String, Order>(date_orders);
//After Sorting
}
Now use Iterator to iterate through TreeMap(Sorted HashMap) as follows:
Iterator itr=tm.keySet().iterator();
while(itr.hasNext()){
String key=(String)itr.next();
System.out.println("Customer ID: "+key+" Spending: "+hm.get(key));
}
Get Answers For Free
Most questions answered within 1 hours.