Saving Hashmap To File

Discussion in 'Plugin Development' started by Live2Pwn, Feb 13, 2013.

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

    Live2Pwn

    Hey guys, I have looked through some tutorials, cant seem to understand this...

    I am trying to save a hashmap containing a String and a int variable to a file, where onEnable can read the file and set the hashmap to the one from the file, and onDisable will save the new hashmap to the file.

    Here is the hashmap I am trying to save:
    Code:
    HashMap<String, Integer> credits = new HashMap<String, Integer>();
    And I guess a .yml file would be the one I need to save to? Not too sure... The name is going to be something like "creditlog".

    Thanks!
     
  2. Offline

    chasechocolate

  3. Offline

    gomeow

    chasechocolate
    HashMaps are not ConfigurationSerializable

    Found this with a simple search:
     
  4. Offline

    lenis0012

    Its actualy much easier when you just save 1 hashmap:
    Code:java
    1.  
    2. public void saveCredits() {
    3. for(String credit : credits.keySet()) {
    4. int value = credits.get(credit);
    5. getConfig().set("credits."+credit, value);
    6. }
    7. saveConfig();
    8. }
    9.  
    10. public void loadCredits() {
    11. credits = new HashMap<String, Integer>(); //reset the credits
    12. if(getConfig().getConfigurationSection("credits") != null ) {
    13. //the credits have been saved before, lets load them
    14. Set<String> set = getConfig().getConfigurationSection("credits").getKeys(false);
    15. for(String credit : set) {
    16. int value = getConfig().getInt(credit);
    17. credits.put(credit, value);
    18. }
    19. }
    20. }
    21.  
     
  5. Offline

    Live2Pwn

    lenis0012
    Alright, now how would I do this with an array? For example, saving a bunch of locations to a file then re-loading them to the array?
     
  6. Offline

    lenis0012

    String arrays can be saved using this:
    Code:java
    1. List<String> list = new ArrayList<String>();
    2. list.add("example");
    3. getConfig().set("test-list", list);
    4. saveConfig();


    result:
    Code:
    test-list:
    - example
    
    to load it:
    Code:java
    1. List<String> loaded = getConfig().getStringList("test-list");


    Saving locations is a little more complicated.
    Do you need to save locations or did you just say that as example?
     
  7. Offline

    Live2Pwn

    lenis0012
    I do actually need to save locations, if that isn't too much trouble :3
     
  8. Offline

    lenis0012

    Ill write something for you when im back from school
     
  9. Offline

    LucasEmanuel

    If you are looping through a map and are going to use both the value and key, I'd recommend using the entryset. :)

    Code:
    Map<Key, Value> map = new Map<Key, Value>();
     
    for(Entry<Key, Value> entry : map.entrySet()) {
        getConfig().set("credits." + entry.getKey(), entry.getValue());
    }
     
     
Thread Status:
Not open for further replies.

Share This Page