共同学习Java源代码-数据结构-HashSet(二)
来源:互联网 发布:linux网页编程工具 编辑:程序博客网 时间:2024/05/21 11:27
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out HashMap capacity and load factor
s.writeInt(map.capacity());
s.writeFloat(map.loadFactor());
// Write out size
s.writeInt(map.size());
// Write out all elements in the proper order.
for (E e : map.keySet())
s.writeObject(e);
}
这个方法是序列化输出方法,就是调用参数输出流的方法依次输出默认对象,输出底层map的容量,输出底层map的载荷,输出底层map的大小,输出每个元素。
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read capacity and verify non-negative.
int capacity = s.readInt();
if (capacity < 0) {
throw new InvalidObjectException("Illegal capacity: " +
capacity);
}
// Read load factor and verify positive and non NaN.
float loadFactor = s.readFloat();
if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
throw new InvalidObjectException("Illegal load factor: " +
loadFactor);
}
// Read size and verify non-negative.
int size = s.readInt();
if (size < 0) {
throw new InvalidObjectException("Illegal size: " +
size);
}
// Set the capacity according to the size and load factor ensuring that
// the HashMap is at least 25% full but clamping to maximum capacity.
capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),
HashMap.MAXIMUM_CAPACITY);
// Create backing HashMap
map = (((HashSet<?>)this) instanceof LinkedHashSet ?
new LinkedHashMap<E,Object>(capacity, loadFactor) :
new HashMap<E,Object>(capacity, loadFactor));
// Read in all elements in the proper order.
for (int i=0; i<size; i++) {
@SuppressWarnings("unchecked")
E e = (E) s.readObject();
map.put(e, PRESENT);
}
}
这个是序列化输入的方法,步骤其实和上面类似,就是生成底层map的时候会判断是否是linkedhashmap还是hashmap。
- 共同学习Java源代码-数据结构-HashSet(二)
- 共同学习Java源代码-数据结构-HashSet(二)
- 共同学习Java源代码-数据结构-HashSet(一)
- 共同学习Java源代码-数据结构-TreeSet(二)
- 共同学习Java源代码-数据结构-HashMap(二)
- 共同学习Java源代码-数据结构-LinkedHashMap(二)
- 共同学习Java源代码--数据结构--AbstractCollection抽象类(二)
- 共同学习Java源代码--数据结构--AbstractList抽象类(二)
- 共同学习Java源代码--数据结构--ArrayList类(二)
- 共同学习Java源代码--数据结构--LinkedList类(二)
- 共同学习Java源代码-数据结构-AbstractMap抽象类(二)
- 共同学习Java源代码-数据结构-HashMap(二十)
- 共同学习Java源代码--数据结构--开篇语
- 共同学习Java源代码--数据结构--Collection接口
- 共同学习Java源代码--数据结构--List接口
- 共同学习Java源代码--数据结构--AbstractSequentialList类
- 共同学习Java源代码--数据结构--Set接口
- 共同学习Java源代码-数据结构-LinkedHashSet
- __attribute__((packed))详解
- VC执行批处理文件并获取结果输出到RichEdit控件
- 9.5.1
- css揭秘系列之半透明边框
- 1029. 旧键盘(20)
- 共同学习Java源代码-数据结构-HashSet(二)
- 用双向最大匹配法进行中文分词
- C#90课的主要内容
- 1023. Have Fun with Numbers (20)
- 替换WebView加载网页失败时的页面
- response中添加cookie时IllegalArgumentException错误
- 9.5.2
- 最新2016年传智播客前端开发视频教程1期
- So hard so brave --y\