List与Set的区别
转载:
list是列表(接口),是可以允许出现重复值的, sets是集合,不允许出现重复值
- Unlike sets, lists typically allow duplicate elements. More formally,
- lists typically allow pairs of elements e1 and e2
- such that e1.equals(e2), and they typically allow multiple
- null elements if they allow null elements at all. It is not inconceivable
- that someone might wish to implement a list that prohibits duplicates, by
- throwing runtime exceptions when the user attempts to insert them, but we
- expect this usage to be rare.
arraylist(一般类) 实现list接口 据说arraylist与vector是差不多的,只不过arraylist是不同步的
vector ArrayList同样是数组的封装 vector似乎是方法的同步? ArrayList非同步 如果不涉及多线程 使用ArrayList应该会块些
好像List和Map都是接口 不能实例化的 以前这么写List list = new Vector(); 现在这么写List list = new ArrayList(); 用ArrayList 代替了Vector 因为前者的性能比后者好; 但是两个都是实现了List借口的 同理Map map = new HashTable();(以前) Map map = new HashMap();(现在)
-----------------------------------------------2楼---------------------------------------------------------
ArrayList和HashMap是异步的,Vector和HashTable是同步的,所以Vector和HashTable是线程安全的,而ArrayList和HashMap并不是线程安全的。因为同步需要花费机器时间,所以Vector和HashTable的执行效率要低于ArrayList和HashMap。 Collection ├List │├LinkedList │├ArrayList │└Vector │ └Stack └Set Map ├Hashtable ├HashMap └WeakHashMap
List接口 List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。 和下面要提到的Set不同,List允许有相同的元素。 除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加,删除,设定元素,还能向前或向后遍历。 实现List接口的常用类有LinkedList,ArrayList,Vector和Stack。 ArrayList类 ArrayList实现了可变大小的数组。它允许所有元素,包括null。ArrayList没有同步。 size,isEmpty,get,set方法运行时间为常数。但是add方法开销为分摊的常数,添加n个元素需要O(n)的时间。其他的方法运行时间为线性。 每个ArrayList实例都有一个容量(Capacity),即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加,但是增长算法并没有定义。当需要插入大量元素时,在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。 和LinkedList一样,ArrayList也是非同步的(unsynchronized)。 Map接口 请注意,Map没有继承Collection接口,Map提供key到value的映射。一个Map中不能包含相同的key,每个key只能映射一个value。Map接口提供3种集合的视图,Map的内容可以被当作一组key集合,一组value集合,或者一组key-value映射。 HashMap类 HashMap和Hashtable类似,不同之处在于HashMap是非同步的,并且允许null,即null value和null key。,但是将HashMap视为Collection时(values()方法可返回Collection),其迭代子操作时间开销和HashMap的容量成比例。因此,如果迭代操作的性能相当重要的话,不要将HashMap的初始化容量设得过高,或者load factor过低。
----------------------------------------------3楼----------------------------------------------------------
1. List是接口,List特性就是有序,会确保以一定的顺序保存元素. ArrayList是它的实现类,是一个用数组实现的List. Map是接口,Map特性就是根据一个对象查找对象. HashMap是它的实现类,HashMap用hash表实现的Map,就是利用对象的hashcode(hashcode()是Object的方法)进行快速散列查找.(关于散列查找,可以参看<<数据结构>>) 2. 一般情况下,如果没有必要,推荐代码只同List,Map接口打交道. 比如:List list = new ArrayList(); 这样做的原因是list就相当于是一个泛型的实现,如果想改变list的类型,只需要: List list = new LinkedList();//LinkedList也是List的实现类,也是ArrayList的兄弟类 这样,就不需要修改其它代码,这就是接口编程的优雅之处. 另外的例子就是,在类的方法中,如下声明: private void doMyAction(List list){} 这样这个方法能处理所有实现了List接口的类,一定程度上实现了泛型函数. 3. 如果开发的时候觉得ArrayList,HashMap的性能不能满足你的需要,可以通过实现List,Map(或者Collection)来定制你的自定义类. 可以参考The Art Of Computer Programming的Sorting and Searching部分
list是列表(接口),是可以允许出现重复值的, sets是集合,不允许出现重复值
- Unlike sets, lists typically allow duplicate elements. More formally,
- lists typically allow pairs of elements e1 and e2
- such that e1.equals(e2), and they typically allow multiple
- null elements if they allow null elements at all. It is not inconceivable
- that someone might wish to implement a list that prohibits duplicates, by
- throwing runtime exceptions when the user attempts to insert them, but we
- expect this usage to be rare.
arraylist(一般类) 实现list接口 据说arraylist与vector是差不多的,只不过arraylist是不同步的
vector ArrayList同样是数组的封装 vector似乎是方法的同步? ArrayList非同步 如果不涉及多线程 使用ArrayList应该会块些
数据量大的用Vector 数据量小得用ArrayList 要求同步的用Vector 不要求同步的用Arraylist
Vector类中的所有方法都是线程同步的,两个线程并发访问Vector时对象是安全的。但只有一个线程访问Vector对象时,因为源程序仍调用了同步方法,需要额外的监视器检查,运行效率要低些。 ArrayList类中的方法是异步的,所有在没有多线程安全问题的时候,最好用ArrayList,程序的效率会高些。 在有线程安全问题,且我们的程序又没有自己处理的时候,只能用Vector。
补充一下Vector只能容纳object freforences不能容纳基本类型 ArrayList有更高性能Verctors可多层套用 我比较喜欢ArrayList
Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。 当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间。
可能很多人都不知道ArrayList,但是肯定知道Vector,因为Vector比ArrayList早,所以用的 比较多。但是在java1.2之后的Collection框架中,Vector已经被淘汰了,因为要保持兼容型, 这个类会一直存在,但是确被建议不要使用,这就是软件的兼容性。
对于不熟悉Vector的人那最好了,直接用ArrayList就好了,不过习惯使用Vector的最好也转到 ArrayList(虽然Vector可能永远存在JDK中),但是我们没有理由放弃ArrayList使用Vector。
public class Vector extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable 可以看出和ArrayList的声明是一样的,这里不考虑它的实现,来看看主要区别: 1.Vector有而ArrayList没有的方法:addElement,copyInto,elementAt,elements,firstElement,firstElement等等,这里就不一一列举,基本上都是多余的方法,而且还使用了Enumeration(一起被淘汰的)。
2.Vector的实现相对ArrayList稍微复杂,Vector功能并不比ArrayList强大,代码量确是两倍。
3.Vector中的大部分方法都是同步方法,不要认为这是它的优点!同步是要付出代价的,要不然在单例模式中很多人都希望用Double-Check Lock呢(虽然不可行)。因为方法都经过同步,效率自然下降不少。