第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

序列化雙括號(hào)初始化的 Map

序列化雙括號(hào)初始化的 Map

嚕嚕噠 2022-07-20 20:22:28
任何人都可以在下面的示例中解釋序列化問題。我有兩個(gè)相同的(等于返回真)映射 - 一個(gè)以標(biāo)準(zhǔn)方式初始化,第二個(gè)用雙括號(hào)初始化。第二個(gè)不能序列化(拋出 NotSerializableException)。Map<String, Object> m = new HashMap<String, Object>(){    private static final long serialVersionUID = 1L;{    put("test", "String");}};Map<String, Object> m2 = new HashMap<String, Object>();m2.put("test", "String");       Assert.assertEquals(m, m2); // trueAssert.assertTrue(m.equals(m2)); // trueAssert.assertEquals(Utils.deserialize(Utils.serialize(m2)), m2); // okAssert.assertEquals(Utils.deserialize(Utils.serialize(m)), m); // java.io.NotSerializableException on serialize()實(shí)用類:public class Utils {    static public Object deserialize(byte[] b) throws IOException, ClassNotFoundException {        ObjectInputStream ins = null;        try {            ByteArrayInputStream bais = new ByteArrayInputStream(b);            ins = new ObjectInputStream(bais);            return ins.readObject();        } finally {            if(ins != null) {                ins.close();            }        }    }    static public byte[] serialize(Object o) throws IOException {        ByteArrayOutputStream bos = new ByteArrayOutputStream();        ObjectOutputStream oos = new ObjectOutputStream(bos);        oos.writeObject(o);        oos.flush();        oos.close();        bos.close();        return bos.toByteArray();    }}
查看完整描述

2 回答

?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊

第二個(gè)不能序列化(拋出 NotSerializableException)。


這將是因?yàn)槟诜强尚蛄谢愔械姆庆o態(tài)方法中初始化地圖。


雙括號(hào)初始化實(shí)際上只是用實(shí)例初始化器定義一個(gè)匿名類。非靜態(tài)上下文中的匿名類捕獲對(duì)封閉實(shí)例的引用。


如果該類不可序列化,則無法序列化匿名類實(shí)例??雌饋磉@段代碼在單元測(cè)試類中;這樣的類可序列化是非常不尋常的。


老實(shí)說,最簡(jiǎn)單的解決方案就是避免雙括號(hào)初始化。它是一種過于聰明的句法軟糖。


但是,如果您真的堅(jiān)持使用它,您可以簡(jiǎn)單地在靜態(tài)方法中進(jìn)行初始化。


static Map<String, Object> doubleBrace() {

  return new HashMap<String, Object>(){

    private static final long serialVersionUID = 1L;

    {

      put("test", "String");

    }};

}

但這在某種程度上破壞了首先使用雙括號(hào)初始化的簡(jiǎn)潔性。


查看完整回答
反對(duì) 回復(fù) 2022-07-20
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

在這個(gè)演示中,Map<String, Object> m = new HashMap<String, Object>(){是一個(gè)匿名的內(nèi)部類,你可以System.out.println(m.getClass())用來檢查m的類。


public class Utilt implements Serializable {

    private static final long serialVersionUID = -7271914225876022793L;


    @Test

    public void UtilTest() throws IOException, ClassNotFoundException {

        Map<String, Object> m = new HashMap<String, Object>(){

            private static final long serialVersionUID = 1L;

            {

                put("test", "String");

            }};

        Map<String, Object> m2 = new HashMap<String, Object>();

        m2.put("test", "String");


        Assert.assertEquals(m, m2); // true

        Assert.assertTrue(m.equals(m2)); // true

        Assert.assertEquals(Utils.deserialize(Utils.serialize(m2)), m2); // ok

        Assert.assertEquals(Utils.deserialize(Utils.serialize(m)), m);

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-07-20
  • 2 回答
  • 0 關(guān)注
  • 77 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)