Solved Saving a Complex Hashmap to a File

Discussion in 'Plugin Development' started by Crack498, Jul 25, 2015.

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

    Crack498

    I've seen some tutorials about saving hashmaps to a file with like "String, UUID", but mine is
    Code:
    HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>> undo = new HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>>();
    Any help about that? Thanks! :D
     
  2. Offline

    DoggyCode™

    So what exactly are you trying to save to the HashMap?
     
  3. Offline

    farget92

    In which type of file do you wan to save it?
    To YAML ? Like:
    Code:
    uuid:
        arraylist:
             location:
    
    Or to Binary File ?
    I recommend to save it in binary file.
    http://www.tutorialspoint.com/java/java_serialization.htm

    But saved information can't be changed until it's loaded. Like you can't change anything by normal text editor.

    Perhaps can help this:

    Code:
        public boolean save(File file){
           
            HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>> undo = new HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>>();
    
            try{
               
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
               
                oos.writeObject(undo);
                oos.close();
                fos.close();
               
                return true;
           
            }catch(IOException e){
                e.printStackTrace();
                return false;
               
            }
        }
       
        public HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>> load(File file){
           
            HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>> undo;
           
            try{
               
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
               
                undo = (HashMap<UUID, ArrayList<HashMap<Location, HashMap<Material, Byte>>>>) ois.readObject();
               
                ois.close();
                fis.close();
               
                return undo;
               
            }catch(IOException | ClassNotFoundException e){
                e.printStackTrace();
                return null;
               
            }
        }
    One warning on end, never, never forget to close all streams.
     
    Last edited by a moderator: Jul 25, 2015
  4. Offline

    Crack498

    Copy-pasted that, gives errors about things I don't know :S
    I'll look onto this, looks interesting and the way to go. Will reply if everything worked in a few days!
     
  5. And that's why spoonfeeding is a no-no.
     
  6. Offline

    farget92

    Can you please give here error log, what server writes ?
    I'll try to help you. ;)
     
  7. Offline

    mine-care

    @MrBlackIsBack A big nono to be exact!
    @farget92 Spoonfeeding isnt the solution, it is a copy paste practice Also the error is that the Location isnt implementing Serialisable. Locations should be serialized in another way.

    @Crack498 Wouldnt it be simplier to create a custom object to store all the data in it? intead of having nested maps in the way making it had to even access them?
     
    bwfcwalshy likes this.
  8. Offline

    farget92

  9. Offline

    Hawktasard

    @farget92
    It's pretty much just if we do the work for you instead of helping you.
     
  10. Offline

    mine-care

    @farget92 yeah what @Hawktasard said more or less, umm the issue is that when you give someone code they will most of the time copy-paste it and get nothing out of it.
     
  11. Offline

    khave

    Make your own object to store x, y, z, String material and the data byte.
    Then use a Multimap.
    Then you'll be able to get a collection of all the custom objects in the saved key, in this case your UUID, with
    Collection coll = (Collection) MULTIMAPNAME.get(key);
     
  12. Offline

    mythbusterma

    @farget92

    As a general rule, if you have to nest a Collection (any class that extends Collection at some point), you need to rethink your design.

    You should really consider building an Object to contain all the information that needs to be associated with UUID. Plus, you can have this Object implement ConfigurationSerializable to make it extremely easy to save.
     
    Hawktasard likes this.
  13. Offline

    Crack498

    Ok, I think I have got now enough information to solve the issue. Thanks a lot guys! :D
     
Thread Status:
Not open for further replies.

Share This Page