Map Keys

Discussion in 'Spigot Plugin Development' started by ramidzk, May 4, 2017.

?

What key should I use

  1. java.util.UUID

    7 vote(s)
    100.0%
  2. Bukkit Player

    0 vote(s)
    0.0%
  1. Simple question:
    If I have a map (the Java one), what key should I use? UUID (java.util) or Player?
     
    • Funny Funny x 1
  2. UUID, less stored. Player holds a lot more useless values that you won't need. If you need to get a player from a UUID, you can use Bukkit#getPlayer(uuid) or Bukkit#getOfflinePlayer(uuid)


    Sent from my iPhone using Tapatalk
     
  3. Depends on the use case. If you need to use a Player object again often and/or quickly then store it in the map. It will prevent you from having to getPlayer() later, effectively speeding things up. Just be sure you remove the Player object when the player logs off to prevent a memory leak.

    This is a common misconception. You are not literally copying the Player object and carrying it around in a Map. You are simply holding a reference to the Player object where it's located in memory. Thats why you have to release that reference when the player logs off, otherwise your reference to the object isn't going to let Java garbage collect it.
     
    #3 BillyGalbreath, May 4, 2017
    Last edited: May 4, 2017
    • Agree Agree x 2
    • Like Like x 1
  4. To add on to this you can use a WeakHashMap with Player objects as keys and that should effectively prevent possible memory leak for when a player logs off.
     
  5. I'm going to save these values to a file, so using UUID should be better. Thanks for your help! ;)