TheNickmaster21
TheNickmaster21

Reputation: 275

How should I use a Hashmap to store objects?

I am developing a plugin that will store clans (Clan class objects) in a hashmap to be used during operation. The hasmap will be stored in a file (serializing the hashmap as a whole would be ideal but I'm not sure if that is possible) for later and be reloaded.

How should I go about saving the objects? Can I serialize the entire hashmap? Thanks in advance; I'm new to hashmaps :P

Upvotes: 0

Views: 5836

Answers (4)

Sten Roger Sandvik
Sten Roger Sandvik

Reputation: 2536

A HashMap itself implements Serializable so you could serialize the entire HashMap if both keys and values are serializable. First you have to decide what key to use. The key can be any object, but must implement both equals(..) and hashCode() methods so that the key can be identified in the HashMap.

In, your case, you could probably use a Clan name as the key represented as a String object. Also, the Clan should be implement Serializable (or Externalizable).

public class Clan 
    implements Serializable 
{
    ...
}

Creating the HashMap as follows (using Java 1.5 or greater):

Map<String, Clan> clans = new HashMap<String, Clan>();

Serialize it to file like this:

FileOutputStream fos = new FileOutputStream("t.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(clans);

oos.close();

Deserialize it with ObjectInputStream. The Java serialization process is pretty slow so if speed is an issue, you should check out alternative serialization techniques. Kryo is a pretty simple and fast serialization library (https://code.google.com/p/kryo/).

Upvotes: 2

Boris the Spider
Boris the Spider

Reputation: 61158

You can easily use Java's built in serialization as long as all the items in the map implement Serializable, here's a quick example:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    final Map<Serializable, Serializable> myMap = new HashMap<>();
    final File myFile = new File("/path/to/file");
    try (final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(myFile))) {
        oos.writeObject(myMap);
    }
    final Map<Serializable, Serializable> myReadMap;
    try (final ObjectInputStream ois = new ObjectInputStream(new FileInputStream(myFile))) {
        myReadMap = (Map<Serializable, Serializable>) ois.readObject();
    }
}

The Map myMap, which contains Serializable keys and values, is saved to a file and then read from the file as myReadMap.

Upvotes: 1

mishadoff
mishadoff

Reputation: 10789

Sure you can, because HashMap is serializable. Do not forgot make Clan serializable also

Write to file:

FileOutputStream fos = new FileOutputStream("clans");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(clans);
oos.close();

Read from file:

FileInputStream fis = new FileInputStream("clans");
ObjectInputStream ois = new ObjectInputStream(fis);
HashMap<String, Clan> clans = (HashMap) ois.readObject();
ois.close();

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Yes, you can serialize HashMap objects since the class already implements the Serializable interface. From the HashMap documentation:

public class HashMap extends AbstractMap implements Map, Cloneable, Serializable

Note that your Clan class must implement the Serializable interface in order to be serialized as well. If you're going to use the Clan class as key in your map, it must override the hashCode and equals methods.

Upvotes: 1

Related Questions