Android: Difference between Parcelable and Serializable?
为什么Android提供2个用于序列化对象的接口?可序列化对象是否与android
在android中,我们不能只将对象传递给活动。为此,对象必须实现
可序列化
EDCOX1 0是标准的Java接口。您只需实现
请看下面的示例(可序列化):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | // MyObjects Serializable class import java.io.Serializable; import java.util.ArrayList; import java.util.TreeMap; import android.os.Parcel; import android.os.Parcelable; public class MyObjects implements Serializable { private String name; private int age; public ArrayList<String> address; public MyObjects(String name, int age, ArrayList<String> address) { super(); this.name = name; this.age = age; this.address = address; } public ArrayList<String> getAddress() { if (!(address == null)) return address; else return new ArrayList<String>(); } public String getName() { return name; } public String getAge() { return age; } } |
1 2 3 4 5 6 7 | // MyObjects instance MyObjects mObjects = new MyObjects("name","age","Address array here"); // Passing MyObjects instance via intent Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putExtra("UniqueKey", mObjects); startActivity(mIntent); |
号
1 2 3 | // Getting MyObjects instance Intent mIntent = getIntent(); MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey"); |
Parcelable公司
请看下面的示例(parcelable):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | // MyObjects Parcelable class import java.util.ArrayList; import android.os.Parcel; import android.os.Parcelable; public class MyObjects implements Parcelable { private int age; private String name; private ArrayList<String> address; public MyObjects(String name, int age, ArrayList<String> address) { this.name = name; this.age = age; this.address = address; } public MyObjects(Parcel source) { age = source.readInt(); name = source.readString(); address = source.createStringArrayList(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(age); dest.writeString(name); dest.writeStringList(address); } public int getAge() { return age; } public String getName() { return name; } public ArrayList<String> getAddress() { if (!(address == null)) return address; else return new ArrayList<String>(); } public static final Creator<MyObjects> CREATOR = new Creator<MyObjects>() { @Override public MyObjects[] newArray(int size) { return new MyObjects[size]; } @Override public MyObjects createFromParcel(Parcel source) { return new MyObjects(source); } }; } |
。
1 2 3 4 5 6 7 | // MyObjects instance MyObjects mObjects = new MyObjects("name","age","Address array here"); // Passing MyOjects instance Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putExtra("UniqueKey", mObjects); startActivity(mIntent); |
1 2 3 | // Getting MyObjects instance Intent mIntent = getIntent(); MyObjects workorder = (MyObjects) mIntent.getParcelableExtra("UniqueKey"); |
。
您可以按如下方式传递parcelable对象的
1 2 3 4 5 6 7 | // Array of MyObjects ArrayList<MyObjects> mUsers; // Passing MyOjects instance Intent mIntent = new Intent(FromActivity.this, ToActivity.class); mIntent.putParcelableArrayListExtra("UniqueKey", mUsers); startActivity(mIntent); |
。
1 2 3 | // Getting MyObjects instance Intent mIntent = getIntent(); ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey"); |
结论
可序列化是一个标准的Java接口。您只需通过实现接口标记一个可序列化的类,Java就可以在某些情况下自动序列化它。
Parcelable是一个特定于Android的接口,您可以在其中自己实现序列化。它的创建效率更高,可以序列化,并解决了默认Java序列化方案中的一些问题。
我相信binder和aidl可以处理parcelable对象。
但是,可以在intent中使用可序列化对象。
If you want to be a good citizen, take the extra time to implement
Parcelable since it will perform 10 times faster and use less
resources.However, in most cases, the slowness of Serializable won’t be
noticeable. Feel free to use it but remember that serialization is
an expensive operation so keep it to a minimum.If you are trying to pass a list with thousands of serialized objects,
it is possible that the whole process will take more than a second. It
can make transitions or rotation from portrait to lanscape feel very
sluggish.
号
资料来源:http://www.developerphil.com/parcelable-vs-serializable/
parcelable和serializable我指的是这两个。
可序列化,简单
What is Serializable?
Serializable is a standard Java interface. It is not a part of the
Android SDK. Its simplicity is its beauty. Just by implementing this
interface your POJO will be ready to jump from one Activity to
another.
号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class TestModel implements Serializable { String name; public TestModel(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
号
序列化的好处在于,您只需要在类及其子类上实现可序列化接口。它是一个标记接口,意味着没有实现的方法,Java将尽其所能来高效地串行化它。
这种方法的问题在于使用了反射,而且它是一个缓慢的过程。这种机制还倾向于创建大量临时对象,并导致大量垃圾收集。
Parcelable,速度
What is Parcelable?
Parcelable is another interface. Despite its rival (Serializable in
case you forgot), it is a part of the Android SDK. Now, Parcelable was
specifically designed in such a way that there is no reflection when
using it. That is because we are being really explicit for the
serialization process.
号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | public class TestModel implements Parcelable { String name; public TestModel(String name, String id) { this.name = name; } protected TestModel(Parcel in) { this.name = in.readString(); } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.name); } public static final Parcelable.Creator<TestModel> CREATOR = new Parcelable.Creator<TestModel>() { @Override public TestModel createFromParcel(Parcel source) { return new TestModel(source); } @Override public TestModel[] newArray(int size) { return new TestModel[size]; } }; } |
现在,胜利者是
。
Philippe Breault进行的测试结果表明,Parcelable比序列化快10倍以上。其他一些谷歌工程师也支持这一说法。
According to them, the default Serializable approach is slower than
Parcelable. And here we have an agreement between the two parties!
BUT, it is unfair to compare these two at all! Because with Parcelable
we are actually writing custom code. Code specifically created for
that one POJO. Thus, no garbage is created and the results are better.
But with the default Serializable approach, we rely on the automatic
serialization process of Java. The process is apparently not custom at
all and creates lots of garbage! Thus, the worse results.
号
停!停!!!!!,在做出决定之前
Now, there is another approach. The whole automatic process behind Serializable can be replaced by custom code which uses writeObject() &
readObject() methods. These methods are specific. If we want to rely
on the Serializable approach in combination with custom serialization
behavior, then we must include these two methods with the same exact
signature as the one below:
号
1 2 3 4 5 6 7 8 | private void writeObject(java.io.ObjectOutputStream out) throws IOException; private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; private void readObjectNoData() throws ObjectStreamException; |
。
现在,Parcelable和定制序列化之间的比较似乎是公平的!结果可能会令人惊讶!自定义序列化方法的写入速度比Parcelable快3倍以上,读取速度比Parcelable快1.6倍以上。
在Parcelable中,开发人员编写用于封送和解封的自定义代码,以便与序列化相比,它创建的垃圾对象更少。由于这个自定义实现,Parcelable的序列化性能显著提高(大约快两倍)。
序列化是一个标记接口,这意味着用户不能根据自己的要求封送数据。在序列化中,使用Java反射API在Java虚拟机(JVM)上执行封送处理操作。这有助于识别Java对象的成员和行为,但最终也会创建大量垃圾对象。因此,与Parcelable相比,序列化过程是缓慢的。
编辑:编组和解编的含义是什么?
简言之,"编组"是指将数据或对象转换为字节流的过程,"解编"是将字节流beack转换为其原始数据或对象的反向过程。转换是通过"序列化"实现的。
http://www.jguru.com/faq/view.jsp?EID=560072
实际上,我将是唯一一个主张序列化的人。速度差异不再那么剧烈,因为这些设备比几年前要好得多,而且还有其他更细微的差异。有关详细信息,请参阅我在该问题上的博客文章。
1。可序列化
参见http://docs.oracle.com/javase/7/docs/api/java/io/serializable.html
什么界面?- 是一个标准的Java接口
速度
- 比Parcelable慢
2。Parcelable公司
参见http://developer.android.com/reference/android/os/parcelable.html网站
什么界面?- 是android.os界面
- 这意味着谷歌开发了Parcelable,以便在Android上获得更好的性能。
速度
- 更快(因为它是为Android开发而优化的)
>总结
请注意,SerialEntIt是一个标准的Java接口,PARCELLID是Android开发的
对于编组和解组,存在一些性能问题。parcelable比serializable快两倍。
请浏览以下链接:
http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development
如果在android studio中使用paracelable插件,Parcelable的实现会更快。搜索Android Parcelable代码生成器
Parcelable是Android开发中的一种标准。但不是因为速度
Parcelable是数据传输的推荐方法。但是,如果您像这个repo中所示正确使用serializable,您将看到serializable有时甚至比parcelable更快。或者至少时间是可比的。
Parcelable比序列化更快吗?Usual Java serialization on an average Android device (if done right *) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads. Also it proves that Java Serialization (if done right) is fast storage mechanism that gives acceptable results even with relatively large object graphs of 11000 objects with 10 fields each.
* The sidenote is that usually everybody who blindly states that"Parcelable is mush faster" compares it to default automatic serialization, which uses much reflection inside. This is unfair comparison, because Parcelable uses manual (and very complicated) procedure of writing data to the stream. What is usually not mentioned is that standard Java Serializable according to the docs can also be done in a manual way, using writeObject() and readObject() methods. For more info see JavaDocs. This is how it should be done for the best performance.
号所以,如果序列化更快更容易实现,为什么Android有Parcelable呢?
原因是本机代码。Parcelable不仅仅是为进程间通信而创建的。也可用于内部通信。您可以发送和接收来自C++原生层的对象。就这样。
你应该选择什么?两者都会很好地工作。但我认为Parcelable是更好的选择,因为它是由谷歌推荐的,正如您从这个线程中看到的,它更受欢迎。
可序列化接口的使用方式可以与Parcelable接口相同,从而获得(不太多)更好的性能。只需覆盖这两种方法即可处理手动编组和解编过程:
1 2 3 4 | private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException |
不过,在我看来,在开发本机Android时,使用Android API是可行的。
见:
- https://bitback.org/afrishman/androidserializationtest/
- https://docs.oracle.com/javase/7/docs/api/java/io/serializable.html
您可以在意图中使用可序列化对象,但在序列化Parcelable对象时,它可能会给出严重的异常,如NotSerializableException。不建议在parcelable中使用serializable。因此,最好用您想要与bundle和intent一起使用的对象扩展parcelable。因为这个Parcelable是Android特有的,所以它没有任何副作用。:)
parcelable比binder序列化快得多,因为序列化使用反射并导致许多GC。Parcelable的设计是为了优化传递对象。
这是参考资料的链接。http://www.developerphil.com/parcelable-vs-serializable/