Help with hashmaps

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

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

    TheDirtyDan

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

    travja

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

    Phil2812

    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

    TheDirtyDan

    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

    travja

    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

    TheDirtyDan

    travja
    is this good?
    Code:
    if(label.equalsIgnoreCase("bid")){
                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;
                        }else{
                            p.sendMessage(s+ChatColor.GOLD+" Your bid has to be higher than the previous offer");
                        }
                    }
                }
            }
     
  7. Offline

    travja

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

    Courier

    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.
    Code:java
    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

    Flobi

    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