Save and load objects inside a list

Asked

Viewed 363 times

2

Hello, can someone tell me why it is only loading value, and the key is blank/void?

Code I use to save and load:

        public static List<Pair<String, String>> cash_player = new ArrayList<>();
        public static void save() {
        File f = new File(plugin().getDataFolder(), "cash.dat");
        if (!(f.exists()))
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
            oos.writeObject(cash_player);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public static void load() {
        File f = new File(Main.m.getDataFolder(), "cash.dat");
        if (f.exists()) {
            try {
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
                cash_player = (List<Pair<String, String>>) ois.readObject();
                ois.close();
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }`

Class Pair:

class Pair<K, V> implements Serializable{
/**
 * 
 */
private static final long serialVersionUID = 6014244905733911500L;
K key;
  V value;

  public Pair(K key, V value) {
    this.key = key;
    this.value = value;
  }

}

How I use to catch the key and the value:

Cash.cash_player.get(0).value; // Retorna o valor salvo no objeto :D 
Cash.cash_player.get(0).key; // Retorna o nada =[

It’s like I’m not saving everything.

  • 2

    Why don’t you use a Hashmap?

  • Da esse erro Incorrect number of Arguments for type Hashmap<K,V>; it cannot be parameterized with Arguments <Pair<String,String> public Static Hashmap<Pair<String, String>> quiz = new Hashmap<>;

  • Ah tah, but then you have to save the object as Hashmap too

  • I don’t get it. ;public static HashMap<Pair<String, String>> quiz = new HashMap<Pair<String, String>>();

  • No, you have to use Hashmap<String, String> = new Hashmap<String, String>

  • If you use the hashmap as suggested, you no longer need to use the pair class.

Show 1 more comment

1 answer

4

Do so:

public static Map<String, String> cash_player = new HashMap<>();

public static synchronized void save() {
    File f = new File(plugin().getDataFolder(), "cash.dat");
    if (!(f.exists())) {
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))) {
        oos.writeObject(cash_player);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@SuppressWarnings("unchecked")
public static synchronized void load() {
    File f = new File(Main.m.getDataFolder(), "cash.dat");
    if (f.exists()) {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
            cash_player = (Map<String, String>) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And with that you can throw away your class Pair.

Also note the use of Try-with-Resources and of Synchronized.

  • That one Try-with-Resources calls the close() in the finally?

  • 3

    @jbueno Yes. In this syntax of try (...) { ... } (note the parentheses), the compiler automatically adds, under the cloths, a block finally who invokes the close(); and handles the corresponding exceptions using the suppressed exceptions. All this was added in the language in Java 7.

  • Nice, thanks for the explanation.

  • Boy, was I had left the string nulla in the code, so that returned nothing, sorry, very lack of attention my.

  • @Luiz if the answer helped in the solution of your problem you can mark as accepted. If you don’t answer, but with code and explanation where the fault was and how you fixed it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.