Vector 和 HashTable
2017年02月24日 11:09:43 请叫我王老魔 阅读数:329 标签: hashtable vector 更多
个人分类: java
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woshiluoye9/article/details/56835046
其实Vector 和 HashTable现在已经很少使用了,他们是同属于一个时代的,一般我们使用ArrayList 和HashMap 而且建议使用这两个,他们两个是一个时代的。
Vector
Vector 和ArrayList的区别: Vector是线程同步的,而ArrayList不是线程同步的,其他方面的实现两个基本一样,从下面的代码可以看到,它的底层实现也是数组。
protected Object[] elementData;
vector的扩容方式:默认的扩容的大小是变为原来的二倍,如果自己定义了它增加的个数,它会按照我们要求的增长个数进行增长
// 容量的增长模式
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
// 如果capacityIncrement>0,则每次容量增加capacityIncrement, 如果不是则容量增加 一倍 oldCapacity
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
- 那么capacityIncrement这个函数是什么含义呢?这是是Vector定义的数组自增大小,而且提供了一个构造函数,我们可以自己设置它增长的大小。
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
HashTable
HashMap 类与 Hashtable 大致相同,
但是Hashtable是同步的,HashMap是不同步的,
Hashtable不允许使用 null 键,HashMap允许使用 null 键,
Properties 是Hashtable的一个子类,属性类,经常被使用在配置文件中
getProperties(String key),一般读取文件的时候,都是获取字符串
获取系统的环境变量:
System.getProperties获取系统的环境变量