Tutorial Get most votes / int from a Map<K, Integer>

Discussion in 'Resources' started by ChipDev, Mar 8, 2015.

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

    ChipDev

    Hey guys, For all of the minigame maps you would like Voting, right?​

    For example, I will use an Object for my arenas.. This will make it easier and more generic for maps, ​

    (You can use whatever constructor you want, Like Arena, Brawl, etc)​


    First, We want to make our Object method:
    Code:
    public static Object getHighestInteger(Map<Object,Integer> count) {
            return null;
        }
    then, We add some variables inside the method:
    Code:
     int index = 0;
    Object most = null;
    
    Why?
    Index checks in the future if the checking Integer is more than it, Then it updates it.. most will return when the next loop is over, What loop? This one:
    Add this to your code:
    Code:
    for(Object mapkeyset : count.keySet()) {
                if(count.get(mapkeyset) >= index) {
                    index = voteMap.get(arenas);
                    most = mapkeyset;
                }
            }
    This loops through the Map's values, And if it is bigger than the index, It sets the index to it, It also sets the object 'most' To the mapkeyset (Current KEY, not Value!)
    Last thing:
    Code:
    return most;
    And you are done! Thanks for reading, I just wanted to add something new to this section as it was getting a little old :)
    Thanks!


     
    MrDplugins likes this.
  2. @ChipDev
    I don't think that this was necessary. This is REALLY basic logic, checking if one number is higher than other.
     
  3. Offline

    mrCookieSlime

    Locked.

    Java is a requirement for Bukkit.
    And we all know how to use Loops and Relational Operators.
    There is no need for this as this is indeed very basic logic.
     
    Goblom and timtower like this.
Thread Status:
Not open for further replies.

Share This Page