Tutorial Save and load HashMap to/from config

Discussion in 'Resources' started by FisheyLP, Mar 10, 2015.

Thread Status:
Not open for further replies.
  1. Because so many people didn't know how to save & load HashMaps, I made this tutorial.

    This is your example HashMap:
    Code:Java
    1. HashMap<Object, Object> hm = new HashMap<Object, Object>();

    ("Hey, my HashMap is not <Object, Object>!" --> Read the last bit of this tutorial)

    This will be our method to save the HashMap:
    Code:Java
    1. public void saveHashMap(HashMap<Object, Object> hm) {
    2. }

    Inside it, we loop through every key in the HashMap and set the key + value to the config:
    Code:Java
    1. for (Object key : hm.keySet()) {
    2. config.set("HashMap."+key, hm.get(key));
    3. }
    4. //important: save the config!
    5. saveConfig();


    Our next method is to load the HashMap:
    Code:Java
    1. public HashMap<Object, Object> loadHashMap() {
    2. HashMap<Object, Object> hm = new HashMap<Object, Object>();
    3.  
    4.  
    5. return hm;
    6. }

    Before we return the HashMap, this bit is needed to load the keys + values.
    Loop through the ConfigurationSection where you saved the HashMap (in this case it's "HashMap").
    Next put the key + value in the Hashmap.
    Code:Java
    1. for (String key : config.getConfigurationSection("HashMap").getKeys(false)) {
    2. hm.put(key, config.get("HashMap."+key));
    3. }

    Done :D


    Last bit:
    HashMap is not <Object, Object>

    If your HashMap is for example <String, Integer>
    then just replace all <Object, Object> with <String, Integer>.
    And you need to cast/load the keys + values in the loadHashMap method differently. Something like this:
    Code:Java
    1. hm.put(key, config.getInt("HashMap."+key));


    All methods used (open)
    Code:java
    1. public void saveHashMap(HashMap<Object, Object> hm) {
    2. for (Object key : hm.keySet()) {
    3. config.set("HashMap."+key, hm.get(key);
    4. }
    5. saveConfig();
    6. }
    7.  
    8. public HashMap<Object, Object> loadHashMap() {
    9. HashMap<Object, Object> hm = new HashMap<Object, Object>();
    10. for (String key : config.getConfigurationSection("HashMap").getKeys(false)) {
    11. hm.put(key, config.get("HashMap."+key));
    12. }
    13. return hm;
    14. }
     
    Last edited: Mar 10, 2015
    Casinator, PDKnight, ChipDev and 2 others like this.
  2. Offline

    Skionz

    @FisheyLP Isn't this kind of pointless since I am assuming SnakeYAML does this for you?
     
    mine-care likes this.
  3. Offline

    ProStriker123

    Nice, Its very helpfuly for new developers
     
  4. Offline

    lycano

    @FisheyLP Well ... this is about storing data in a HashMap okay =) Then i need to correct something.

    When you define the object please use its Interface.

    In your case use

    Code:java
    1.  
    2. Map<String, String> hm = new HashMap<String, String>();
    3.  


    instead of HashMap<String, String> hm = ...

    Also always use them when you define getter or setter.

    As already pointed out the Configuration Class has many setter and getter for almost any Object like getAsInt, getAsString etc ...
     
  5. Offline

    Totom3

    1. How to load the Map from a FileConfiguration:
    Code:
    // If you want to set to the root, remove getConfigurationSection()
    Map<String, ?> map = config.getConfigurationSection("path").getValues(true or false);
    2. How to save the Map to a FileConfiguration:
    Code:
    config.createSection("path.or.leave.empty.for.root", map); // Will in fact override any existing values
     
  6. Offline

    mrCookieSlime

    Cleared Offtopic conversation.
    This is a Bukkit Resource, not a place where you can discuss the complexity and value of a totally different Thread which was locked for a valid reason.
     
  7. Offline

    xTrollxDudex

    Code:
    for (Object key : hm.keySet()) {
        config.set("HashMap."+key, hm.get(key));
    }
    Efficiency -1
     
  8. Offline

    RingOfStorms

    Why not tell him the right way to do it rather than just saying that?

    The reason why it is less efficient is because you're iterating over the map and then iterating over it again to find the values, you should just use an EntrySet.
    Code:
    Map<String, Object> someMap = new HashMap<String, Object>();
    for(Map.Entry<String, Object> entry : someMap.entrySet()) {
        config.set("HashMap."+entry.getKey(), entry.getValue());
    }
    
     
  9. Offline

    xTrollxDudex

    No use if he doesn't care.
     
  10. Offline

    RingOfStorms

    Then there is no use commenting on his thread. Or you can just continue being hypocritical. But the latter isn't a very respectable or valuable in any way, so you probably should just buzz off.
     
    mrCookieSlime likes this.
  11. Offline

    Doubtstand

    When using a <UUID, String> HashMap, getConfigurationSection gives this error: Type mismatch: cannot convert from element type String to UUID.

    Code:java
    1.  
    2. public HashMap<UUID, String> loadHashMap(){
    3. HashMap<UUID, String> rank = new HashMap<UUID, String>();
    4. for(UUID key : getConfig().getConfigurationSection("HashMap.").getKeys(false){
    5. rank.put(key, getConfig().getString("HashMap."+key));
    6. }
    7. return rank;
    8. }
    9.  


    When changing it to a String, you can't put it in the rank HashMap, because The method put(UUID, String) in the type HashMap<UUID,String> is not applicable for the arguments (String, String).

    How can I fix this?
     
Thread Status:
Not open for further replies.

Share This Page