开发者

hashmap custom class key && object saving/loading

开发者 https://www.devze.com 2023-04-02 03:49 出处:网络
Been working on a project for a while now and I\'ve come across a few different complications and solutions that don\'t seem to pan out together.

Been working on a project for a while now and I've come across a few different complications and solutions that don't seem to pan out together.

final public class place implements Serializable {
    private static final long serialVersionUID = -8851896330953573877L;
    String world;
    Double X;
    Double Y;
    Double Z;
}
HashMap<place, Long> blockmap = new HashMap<place, Long>(); // does not work
HashMap<Location, Long> blockmap = new HashMap<Location, Long>(); //works

First, my hashmap is a hashmap containing the time an item was placed (or added) to the world. place is a 'class place {}' containing String world, double x, double y, double z; The problem i've had with this, is that it doesn't work with hashmaps. I can store a new hash key using it, but i cant call to get its value. Using Location instead fixes this problem (hashmap) and works flawlessly.

public void SetBlock(Block block) {
    Location loc = new Location(null, block.getLocation().getX(),block.getLocation().getY(),block.getLocation().getZ());
    //...
    Long time = (long) (System.currentTimeMillis() / 60000);
    //...
    if (blockmap.containsKey(loc)) {
            b开发者_StackOverflowlockmap.remove(loc);
            blockmap.put(loc, time);
            //System.out.println("MyLeveler: Block Existed, Updated");
    } else {
            blockmap.put(loc, time);
            //System.out.println("MyLeveler: Block added to " + loc.getX() + ", " + loc.getY() + ", " + loc.getZ());
            //System.out.println("MyLeveler: total blocks saved: " + blockmap.size());
    }
}

This works without error. Now, for the purpose, this data has to be saved and reloaded when the plugin is disabled, and enabled. To do this, i created a new java class file with a save/load feature.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SLAPI {
    public static void save(Object obj,String path) throws Exception
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
        oos.writeObject(obj);
        oos.flush();
        oos.close();
    }
    public static Object load(String path) throws Exception
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
        Object result = ois.readObject();
        ois.close();
        return result;
    }
}

I typically get "notserializable" errors. Using 'implements Serializable' and ois.defaultReadObject() or oos.defaultWriteObject() which checks the serial on the file results in a clean save/load only when the object is EMPTY! When it contains data, i constantly get "java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException"

This is clearly a problem! One of the recommendations here: ArrayList custom class as HashMap key failed to produce any better results. In fact, creating a custom class was my first issue to begin with >.>

So i guess the questions are:

1) What would i have to alter to use the custom class as a key (and work properly)

2) Why doesn't it recognize that i'm setting it as a serializable class/function/java class

3) Why does it work with an empty hashmap, but not with a filled hashmap?


Basically you need to override hashCode() and equals() in place. Presumably Location already overrides these methods.

Those are the methods that HashMap uses to first narrow down the list of candidate keys very quickly (using the hash code) and then check them for equality (by calling equals).

It's not clear what the serializable problem is - my guess is that although place is serializable, Location isn't. If you could post a short but complete problem demonstrating the problem, that would really help. (It would also be a good idea to start following Java naming conventions, and making your fields private...)

EDIT: Here's an example of the Place class with hash code and equality. Note that I've made it immutable for the sake of avoiding the values changing after it's used as a key in a hash map - I don't know offhand how well that works with serialization, but hopefully it's okay:

public final class Place implements Serializable {
    private static final long serialVersionUID = -8851896330953573877L;

    private final String world;
    // Do you definitely want Double here rather than double?
    private final Double x;
    private final Double y;
    private final Double z;

    public Place(String world, Double x, Double y, Double z) {
        this.world = world;
        this.x = x;
        this.y = y;
        this.z = z;
    }

    @Override public int hashCode() {
        int hash = 17;
        hash = hash * 31 + (world == null ? 0 : world.hashCode());
        hash = hash * 31 + (x == null ? 0 : x.hashCode());
        hash = hash * 31 + (y == null ? 0 : y.hashCode());
        hash = hash * 31 + (z == null ? 0 : z.hashCode());
        return hash;
    }

    @Override public boolean equals(Object other) {
        if (!(other instanceof Place)) {
            return false;
        }
        Place p = (Place) other;
        // Consider using Guava's "Objects" class to make this simpler
        return equalsHelper(world, p.world) &&
               equalsHelper(x, p.x) &&
               equalsHelper(y, p.y) &&
               equalsHelper(z, p.z);
    }

    private static boolean equalsHelper(Object a, Object b) {
        if (a == b) {
            return true;
        }
        if (a == null || b == null) {
            return false;
        }
        return a.equals(b);
    }

    // TODO: Add getters?
}

It's worth noting that this will be comparing Double values for equality, which is almost always a bad idea... but you can't really give a tolerance in something like equals. So long as the values are exactly the same when you come to look them up, it should work fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消