Help with hashmaps

Discussion in 'Plugin Development' started by TheDirtyDan, Aug 11, 2012.

Thread Status:
Not open for further replies.
  1. Offline


    How do you find the highest value integer in a hashmap, and then clear all the values in that hashmap?
  2. Offline


    What exactly are you saying... Find how big the HashMap is or what?
  3. Offline


    You could loop through the whole hash map with a for-loop like
    for (Entry<type,type> entry : yourHashMap.entrySet()) {...}
    And use an variable to store the key with the highest value or if the integer is the key you could use another type of map such as a sortedmap instead of a hashmap.
  4. Offline


    Well, the purpose of the hashmap is to make bids, and while an auction is currently going on, people are going to add bids (integers) to the hashmap, and the highest one gets the prize, and then resets all the values for the next auction..
  5. Offline


    ok, Make an int i = 0; and then loop through your hashmap and when you find the first number change i to that so i= bignumber; Then everytime you look at a new number you check if it is bigger than i so if(newnumber > i){ i = newnumber;}
    and so on!

    Hopefully you can understand that...
  6. Offline


    is this good?
                int i = 0;
                if(args.length == 1){
                    for(Map.Entry<String, Integer> entry : aucBid.entrySet()){
                        int nn = Integer.parseInt(args[0]);
                        if(nn > i){
                            aucBid.put(p.getName(), nn);
                            i = nn;
                            p.sendMessage(s+ChatColor.GOLD+" Your bid has to be higher than the previous offer");
  7. Offline


    That should work, take it for a test run!!
  8. Offline


    Eh... that should work, but it is kind of silly to loop through the whole HashMap every time a player wants to bid.
    It would be more efficient to just store the highest bid to a variable, then you only need to compare a new bid against that.
    1. aucBid.clear();

    Although it sounds like you might not even need a HashMap at all... if you want to clear it after every bid. Maybe all you need to do is just store a String and and int for the highest-bidding player and the highest bid.
  9. Offline


    I could see how this would be useful for tracking bids if you want to actually have a bid history. All the auction software's I've reviewed the code for only track the current highest bidder (& bid), each time a bid comes in, either fail or replace the current highest bidder.

    Another way to do it would be to only add bids if they exceed the last entry of the HashMap. Then you'd know the last entry was always the highest bidder (because the newly added one goes to the end).
Thread Status:
Not open for further replies.

Share This Page