HashMap saving

Discussion in 'Spigot Plugin Development' started by pintux98, Apr 29, 2017.

Thread Status:
Not open for further replies.
  1. How to save a HashMap like this:

    HashMap<Integer, List<Location>> stuff;

    Code (Text):
    for(int s: map.keySet()){

    and load it back from config file?

    thanks :D
  2. Well, it depends on how you want to store it. Try opening up Notepad and write down how you want the config to look after a save. Here's a basic idea I came up with:

    Code (Text):
        id: 0
          - world, x, y, z
          - world, x, y, z
  3. In this case id like to store it like that:

    Code (Text):
             - world,x,y,z
             - world,x,y,z
             - world,x,y,z
             - world,x,y,z
  4. I believe there is such thing as a ConfigurationSection, try looking around that.
    Code (Text):
  5. Okay, so you want to go about storing locations as strings in a string list. That's perfectly fine. Like @spammy23 just mentioned, you'll have a ConfigurationSection and several string lists inside of that configuration section.

    Oh, and to add, you'll use String.split when loading up data for the HashMap since you're storing each location's data in a single string separated by commas.
  6. WAS


    You would need to break down your information and save it. Otherwise you will need to serialize and unserialize which won't be useful for editing a config file.


    Store the info in a string separated by comas, and split that string when receiving by commas.

    • Agree Agree x 2
  7. Follow what @WAS just said. Attempt to code something yourself and if you can't get something working, just post again on this thread and either someone else or I will help you.
  8. I've tried to load hashmap like this:

    Code (Text):
    int counter =1;
    for(String str: config.getConfigurationSection("HashMap."+counter).getKeys(false){
    List<Location> loc = new ArrayList<>();
    map.put(counter, loc);
    Do you think is good?
  9. You'd have to loop through the locations and create a string representation of it and store it in a list.
    From there you can save that list under the key value.

    Then to load it you'd have to parse it back to a location.

    Code (Text):
    Location loc = map.get(s);

    StringBuilder stringBuilder = new StringBuilder();
    (so on)

    String stringLoc = stringBuilder.toString();
    Code (Text):

    Location loc = null;

    String stringLoc = getConfig().getString("some.path");
    String split = stringLoc.split

    (so on)
    • Agree Agree x 1
  10. No, because you're trying to add a String to a list of Location objects. When you're loading your location data you need to do what @WAS and I suggested, you need to use #split on the Strings to get the world, x, y, and z data for the location.
    • Agree Agree x 1
  11. WAS


    This is a bit overkill.
  12. I think I'll try to use the @DefNotValid option!

    Thanks also @WAS
  13. You only declared a Location variable here, you haven't initialized it, so this will throw a NullPointerException. It may just be better to collect the data for the location and just use that data to create a new location object at the end.
    • Agree Agree x 1
  14. I'll try that option too and see whitch one is the best
  15. You're right, wasn't meant for actual use but just an example.

    Which part?
  16. I think it's just easier to either use a ConfigurationSection or just something like this:
    Code (YAML):
    : 10
    : 20
    : 30
    : WorldName
    Code (Java):
    getConfig.set("location.1.x", 10);
    Something similar to that, instead of serialising the HashMap yourself
  17. Do you think there is a better way to store multiple locations,so i can access them and load when needed?
    I'm working on a plugin and i need to get just some location each time and not all together, this is why I'm trying to save them under multiple groups
  18. This is a good alternative too because it'll mean that you won't have to split strings. The only downside to this is that it'll result in a much longer config.
  19. That depends on the caliber of your plugin. If it's something simple, I'd just go the flat-file route and just store them in a config and reload them. If this is a larger and more complex plugin, I'd look into something like Redis or MySQL to store the locations.
  20. Minigame, but i think I'll split the arenas in multiple config files so everything will look better
    I just have that problem and i still need to find the best way to fix it...
Thread Status:
Not open for further replies.