我有一个PaymentPreference类,该类具有一个哈希表来存储消费者在创建个人资料时选择的所有付款方式。这个PaymentMethod是另一个类,其对象作为键存储在此PaymentPreference哈希图中,而布尔值作为其值存储。所有这些都是出于某些特定目的而完成的。
出现此问题,我遇到一种情况,我需要序列化此PaymentPreference,该支付首选项具有将此Hashmap与PaymentMethod对象作为键。因此,在序列化之前,我使用containsKey检查Hashmap中是否存在特定密钥,并且该密钥返回true。之后,我进行了序列化。但是,当我反序列化PaymentPreference对象并尝试使用Contains Key方法检查hashmap中是否存在相同的键时,我得到了错误。当我知道Hashmap中存在密钥时,这看起来很奇怪。但是以某种方式,在序列化之后,我无法使用containsKey方法找到该键的匹配项。
因为这都是关于匹配对象的,所以我尝试在序列化前后打印哈希映射键的哈希码值,就可以了。序列化前后,哈希映射键的哈希码值有所不同。这就是我的containsKey方法反序列化后未返回true的原因。我不知道为什么会这样发生。我在序列化方面还没有做很多工作,只是在某些特定情况下,我不得不这样做。我在这个已经存在的线程中找不到任何令人信服的答案。有人可以阐明为什么会发生这种情况,我应该怎么做才能防止这种情况发生?
PFB我的课程
PaymentMethods类
public class PaymentMethods implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 3398741949646654368L;
private String paymentMethod;
public static final PaymentMethods CASH = new PaymentMethods("CashPayment");
public static final PaymentMethods BITCOIN = new PaymentMethods("BitCoin");
public static final PaymentMethods CARD = new PaymentMethods("Card");
public static final PaymentMethods CHEQUE= new PaymentMethods("Cheque");
public static final PaymentMethods MPAY= new PaymentMethods("MobPay");
public static final PaymentMethods PAYME= new PaymentMethods("PayMe");
public PaymentMethods(String paymentMethod) {
this.paymentMethod=paymentMethod;
}
/**
* @return the paymentMethod
*/
public String getPaymentMethod() {
return this.paymentMethod;
}
}
PaymentPreferences类
import java.util.HashMap;
import java.util.Map;
public class PaymentPreferences implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 7475358849630530148L;
//preferences with on/off
private Map<PaymentMethods,Boolean> paymentPreferenceMap = new HashMap<PaymentMethods,Boolean> ();
/**
* @return the paymentPreferenceMap
*/
public Map<PaymentMethods, Boolean> getPaymentPreferenceMap() {
return paymentPreferenceMap;
}
public void addSelectedPreferences(PaymentMethods paymentMethod){
paymentPreferenceMap.put(paymentMethod, Boolean.TRUE);
}
public boolean isCashPaymentPreferred(PaymentMethods inputPaymentMethod){
return paymentPreferenceMap.containsKey(inputPaymentMethod);
}
@Override
public String toString() {
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("Is CashPayment Preferred ? -> "+isCashPaymentPreferred(PaymentMethods.CASH));
return sBuffer.toString();
}
}
要测试序列化/反序列化的类
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;
public class TestSerialize {
private PaymentPreferences paymentPreference;
public static void main(String[] args) {
TestSerialize testObj=new TestSerialize();
testObj.initializeData();
testObj.serializeData();
testObj.deSerializeData();
}
private void deSerializeData() {
Object object=null;
System.out.println("Deserializing ");
try {
FileInputStream fis = new FileInputStream("D:\\TestPref.dat");
ObjectInputStream ois= new ObjectInputStream(fis);
object = ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("After Deserialization, Cash Preference check -> "+(PaymentPreferences)object);
System.out.println("After Deserialization,hash code value for map key ->");
printHashCodeForKeys(((PaymentPreferences)object));
}
private void serializeData() {
System.out.println("Before Serialization, Cash Preference check -> "+paymentPreference);
System.out.println("Before Serialization,hash code value for map key ->");
printHashCodeForKeys(paymentPreference);
try {
FileOutputStream fos = new FileOutputStream("D:\\TestPref.dat");
ObjectOutputStream ous= new ObjectOutputStream(fos);
ous.writeObject(paymentPreference);
ous.close();
fos.close();
System.out.println("Serialization done");
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void printHashCodeForKeys(PaymentPreferences paymentPreference) {
Set<PaymentMethods> keySet=paymentPreference.getPaymentPreferenceMap().keySet();
for(PaymentMethods methodObj:keySet) {
System.out.println(methodObj.hashCode());
}
System.out.println("Hashcode for PaymentMethods.CASH object is "+PaymentMethods.CASH.hashCode());
}
private void initializeData() {
paymentPreference = new PaymentPreferences();
paymentPreference.addSelectedPreferences(PaymentMethods.CASH);
}
}
输出:
Before Serialization, Cash Preference check -> Is CashPayment Preferred ? -> true
Before Serialization,hash code value for map key ->
1096192577
Hashcode for PaymentMethods.CASH object is 1096192577
Serialization done
Deserializing
After Deserialization, Cash Preference check -> Is CashPayment Preferred ? -> false
After Deserialization,hash code value for map key ->
428583986
Hashcode for PaymentMethods.CASH object is 1096192577