Loading Hashmap from Config with ArrayList as value

Discussion in 'Plugin Development' started by Ethan, Oct 27, 2013.

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

    Ethan

    So, I've seen the countless threads asking how to load and save hashmaps into and from configs but I haven't found how to load a hashmap when the value is an arraylist.

    My hashmap:
    Code:java
    1. public HashMap<Integer, ArrayList<String>> myMap = new HashMap<Integer, ArrayList<String>>();


    How I save hashmap to config:
    Code:java
    1. for(int x : myMap.keySet()){
    2. for(ArrayList<String> lloc: myMap.values()){
    3. for(String loc: lloc){
    4. Bukkit.broadcastMessage(loc);
    5. y+= 1;
    6. getConfig().set(String.valueOf(x) + ".location" + y, loc.toString());
    7. }
    8. }
    9. }

    ^^I have this properly indented in my code but for some reason it keeps deleting the indentations in the editor...

    And this is how the config looks:
    HTML:
    '1':
      location1: -707,14,1062
      location2: -706,15,1053
    '2':
      location1: -693,15,1081
    So, my question is how do I then load the config back into the hashmap?
     
  2. Offline

    RealDope

    Do your locations need names? Why are you storing them "key: value" instead of in an actual ArrayList?

    Assuming they DO need names for some unknown reason (sometimes you need to get a specific location?..)
    I don't understand your saving method. It's very inefficient. You're looping over ALL the values for ALL the keys. Just do this:
    Code:JAVA
    1.  
    2. public void saveLocations(HashMap<Integer, ArrayList<String>> map) {
    3. int y = 1;
    4. for(int x : map.keySet()) {
    5. for(String s : map.get(x)) {
    6. getConfig().set(x + ".location" + y, s);
    7. y++;
    8. }
    9. }
    10. }
    11. public HashMap<Integer, ArrayList<String>> loadLocations() {
    12. HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
    13. /*
    14. I assume your entire config is JUST these lists of locations
    15. Use this line if your locations are all stored in one specific section of your config instead
    16. If so, replace all getConfig().getKeys(false) with section.getKeys(false)
    17. ConfigurationSection section = getConfig().getConfigurationSection("Locations");
    18. */
    19. for(String s : getConfig().getKeys(false)) {
    20. ArrayList<String> list = new ArrayList<String>();
    21. int i = Integer.parseInt(s); // This will only work if all the names of your lists of locations are integers, NO EXCEPTIONS
    22. for(String s1 : getConfig().getConfigurationSection(s).getKeys(false)) {
    23. list.add(getConfig().getString(s + "." + s1);
    24. }
    25. map.put(i, list);
    26. }
    27. return map;
    28. }
    29.  
     
  3. Offline

    MrSparkzz

    Ethan
    You should change the way your locations are held in the config... Maybe
    Locations:
    1: x,y,z
    2: x,y,z
    Also, change your HashMap to
    Code:java
    1.  
    2. public HashMap<Integer, Location> myMap = new HashMap<Integer, Location>();
    3.  

    If you do that, then you could do this
    Code:java
    1.  
    2. public void createLocations() {
    3. for (int i = 1; i != -1; i++) {
    4. String location = getConfig().getString("location." + i);
    5.  
    6. if (location != null) {
    7. String[] args = location.split(",");
    8.  
    9. if (args.length > 2) {
    10. double x = Double.parseDouble(args[0]);
    11. double y = Double.parseDouble(args[1]);
    12. double z = Double.parseDouble(args[2]);
    13.  
    14. Location loc = new Location();
    15. loc.setX(x);
    16. loc.setY(y);
    17. loc.setZ(z);
    18.  
    19. myMap.put(i, loc);
    20. }
    21. }
    22. }
    23. }
    24.  

    I'm pretty sure that will work, but I didn't create it using an IDE, so there may be some problems... If there are, just let me know (;
     
  4. Offline

    RealDope

    MrSparkzz
    Why would he change his HashMap? He's trying to store lists of strings, not single locations..
     
  5. Offline

    MrSparkzz

    RealDope
    If he were to change his HashMap to an integer and a location, then when retrieving the information would be much easier. You could do something like this
    Code:java
    1.  
    2. myMap.get(/*int*/);
    3.  

    and that would return a location and inside that location if he wanted to get a specific part he could do
    Code:java
    1.  
    2. Location location = myMap.get(1);
    3.  
    4. double x = location.getX();
    5.  
     
  6. Offline

    RealDope

    He's storing a list of locations as strings... He's not trying to associate one integer with one location, it's one integer with many locations. Hence the use of an ArrayList..
     
Thread Status:
Not open for further replies.

Share This Page