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.
Why don’t you use a Hashmap?
– Sorack
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<>;
– Luiz
Ah tah, but then you have to save the object as Hashmap too
– Sorack
I don’t get it. ;
public static HashMap<Pair<String, String>> quiz = new HashMap<Pair<String, String>>();
– Luiz
No, you have to use Hashmap<String, String> = new Hashmap<String, String>
– Sorack
If you use the hashmap as suggested, you no longer need to use the pair class.
– prmottajr