Question

In Java, I need with a sorting method that Find the top ten customers (by spending)...

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;

    }

Homework Answers

Answer #1

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));  
}    
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
Java Lab Assignment Write a method named findMax. The method will take an array of doubles...
Java Lab Assignment Write a method named findMax. The method will take an array of doubles as an argument, and it will return the largest value in the array. (Note, you will have to create a new array of doubles) . method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Java, I get a index out of bounds exception with this method, can you spot where...
Java, I get a index out of bounds exception with this method, can you spot where the method goes out of bounds from this code? public void showEarliestAttack(){ String earliestAttack; for (int i = 0; i < attackArray.size(); i++) { earliestAttack= attackArray.get(i).toString(); if(attackArray.get(i+1).getDate().compareTo(attackArray.get(i).getDate())<0){ earliestAttack = attackArray.get(i+1).toString(); } System.out.println(earliestAttack); } }
The AssemblyLine class has a potential problem. Since the only way you can remove an object...
The AssemblyLine class has a potential problem. Since the only way you can remove an object from the AssemblyLine array is when the insert method returns an object from the last element of the AssemblyLine's encapsulated array, what about those ManufacturedProduct objects that are "left hanging" in the array because they never got to the last element position? How do we get them out? So I need to edit my project. Here is my AssemblyLine class: import java.util.Random; public class...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...