Loading HashMap from file

Discussion in 'Plugin Development' started by travja, Feb 11, 2013.

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

    travja

    I'm going to save a HashMap<Integer, HashMap<Integer, String>> to a file, how would I go about loading the initial hashmap and the hashmap inside of it??? I know kind of how to load a single hashmap, but not this.

    This is what my config is going to look like.
    Code:
    Spawns:
      1:
        1: world,x,y,z
        2: world,x,y,z
        3: world,x,y,z
      2:
        1: world,x,y,z
        2: world,x,y,z
        3: world,x,y,z
     
  2. Offline

    RealDope

    Loop through all keys of "Spawn", loop through all keys of each of those, adding to HashMap?
     
  3. Offline

    travja

    RealDope That's true, didn't think of that, I'll try that later and tell you what I get!
     
  4. Offline

    lenis0012

    Code:java
    1.  
    2. //save list
    3. for(int i : hashmap.keySet()) {
    4. List<String> list = new ArrayList<String>();
    5. for(int j : hashmap.get(i) {
    6. String k = hashmap.get(i).get(j);
    7. list.add(k);
    8. }
    9. getConfig().set("data."i+"/"+j, k);
    10. }
    11. saveConfig();
    12.  
    13. //load list
    14. hashmap = new HashMap<Integer, HashMap<Integer, String>();
    15. if(getConfig().getConfigurationSection("data") != null) {
    16. Set<String> set = getConfig().getConfigurationSection("data").getKeys(false);
    17. for(String i : set) {
    18. String[] j = i.split("/", 1);
    19. int a = Integer.valueOf(j[0]);
    20. int b = Integer.valueOf(j[1]);
    21. HashMap<Integer, String> c = new HashMap<Integer, String>();
    22. List<String> list = getConfig().getStringList(i);
    23. for(String d : list)
    24. c.put(b, d);
    25.  
    26. hashmap.put(a, c);
    27. }
    28. }
    29.  


    line 9 should be:
    getConfig().set("data."+i+"/"+j, k);

    my method makes a messy cofig xD
    it would look like:
    Code:
    spawns:
      1/1:
        - world,x,y,z
        - world,x,y,z
      1/2:
        - world,x,y,z
      2/1:
        - world,x,y,z
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page