14px;">publicclassVectorTest{
publicstaticvoidmain(String[]args){
Vectorvector=newVector();
for(inti=0;i<10;i++){
vector.add(i);
}
//直接打印
System.out.println(vector.toString());
//size()
System.out.println(vector.size());
//contains
System.out.println(vector.contains
(2));
//总结:
对比Vector的遍历方式,使用索引的随机访问方式最快,使用迭代器最慢
//iterator遍历
Iteratoriterator=vector.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+"");
}
//Enumeration遍历
Enumerationenu=vector.elements();
while(enu.hasMoreElements()){
System.out.println((Integer)enu.nextElement());
}
//toArray
Object[]objArr=vector.toArray();
System.out.println("\nobjArr:
"+Arrays.asList(objArr));
Integer[]intArr=vector.toArray(newInteger[vector.size()]);
System.out.println("intArr:
"+Arrays.asList(intArr));
//add
vector.add(5);
//remove
vector.remove(5);
System.out.println(vector);
//containsAll
System.out.println(vector.containsAll(Arrays.asList(5,6)));
//addAll
vector.addAll(Arrays.asList(555,666));
System.out.println(vector);
//removeAll
vector.removeAll(Arrays.asList(555,666));
System.out.println(vector);
//addAll方法
vector.addAll(5,Arrays.asList(666,666,6));
System.out.println(vector);
//get方法
System.out.println(vector.get(5));
//set方法
vector.set(5,55);
System.out.println(vector.get(5));
//add方法
vector.add(0,555);
System.out.println(vector);
//remove方法
vector.remove(0);
System.out.println(vector);
//indexof方法
System.out.println(vector.indexOf(6));
//lastIndexOf方法
System.out.println(vector.lastIndexOf(6));
//listIterator方法
ListIteratorlistIterator=vector.listIterator();
System.out.println(listIterator.hasPrevious());
//listIterator(index)方法
ListIteratoriListIterator=vector.listIterator(5);
System.out.println(iListIterator.previous());
//subList方法
System.out.println(vector.subList(5,7));
//clear
vector.clear();
System.out.println(vector);
}
}
List接口实现类Stack
栈类,是Java2之前引入的,继承自类Vector。
同样是线程同步的
[html]viewplaincopy
14px;">publicclassStackTest{
publicstaticvoidmain(String[]args){
Stackstack=newStack();
for(inti=0;i<10;i++){
stack.add(i);
}
System.out.println(stack);
System.out.println(stack.peek());
stack.push(555);
System.out.println(stack);
System.out.println(stack.pop());
System.out.println(stack);
System.out.println(stack.empty());
System.out.println(stack.search(6));
System.out.println("stack遍历:
");
while(!
stack.empty()){
System.out.print(stack.pop()+"");
}
}
}
List接口总结:
实际使用中我们需要根据特定的需求选用合适的类,如果除了在末尾外不能在其他位置插入或者删除元素,那么ArrayList效率更高,如果需要经常插入或者删除元素,就选择LinkedList。
Set接口实现类HashSet:
HashSet是Set接口最常见的实现类,其底层是基于hash算法进行存储相关元素的。
HashSet中存储元素的位置是固定的(由hashCode决定),并且是无序的。
Set集合中的去重和hashCode与equals方法相关。
[html]viewplaincopy
14px;">publicclassNumimplementsComparable{
privateintnum;
publicNum(intnum){
this.num=num;
}
@Override
publicintcompareTo(Objecto){
//TODOAuto-generatedmethodstub
Numx=(Num)o;
if(num>x.num)return1;
elseif(num==x.num)return0;
elsereturn-1;
}
publicStringtoString(){
return"num="+num;
}
}
[html]viewplaincopy
14px;">publicclassHashSetTest{
publicstaticvoidmain(String[]args){
SethashSet=newHashSet();
hashSet.add("hello");
hashSet.add("world");
hashSet.add("world");
//使用数组的方法遍历HashSet集合
String[]strArray=newString[hashSet.size()];
strArray=hashSet.toArray(strArray);
for(Stringstr:
strArray){
System.out.println(str);
}
//使用HashSet集合直接遍历
for(Stringstr:
hashSet){
System.out.println(str);
}
//用迭代器遍历HashSet集合
Iteratoriterator=hashSet.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//无重写hashCode跟equals方法的类,不会自动根据类中的值进行去重
Setset2=newHashSet();
set2.add(newNum
(1));
set2.add(newNum(3));
set2.add(newNum
(2));
set2.add(newNum(3));
set2.add(newNum(3));
set2.add(newNum(6));
System.out.println(set2.size());
Iteratoriterator2=set2.iterator();
while(iterator2.hasNext()){
System.out.println(iterator2.next());
}
}
}
Set接口实现类LinkedHashSet:
LinkedHashSet继承HashSet,是用一个链表实现来扩展HashSet类,它支持对规则集内的元素排序。
HashSet中的元素是没有被排序的,而LinkedHashSet中的元素可以按照它们插入规则集的顺序提取。
[html]viewplaincopy
14px;">publicclassLinkedHashSetTest{
publicstaticvoidmain(String[]args){
SetlinkedHashSet=newLinkedHashSet();
linkedHashSet.add("hello");
linkedHashSet.add("world");
linkedHashSet.add("world");
//使用数组的方法遍历HashSet集合
String[]strArray=newString[linkedHashSet.size()];
strArray=linkedHashSet.toArray(strArray);
for(Stringstr:
strArray){
System.out.println(str);
}
//使用HashSet集合直接遍历
for(Stringstr:
linkedHashSet){
System.out.println(str);
}
//用迭代器遍历HashSet集合
Iteratoriterator=linkedHashSet.iterator();
while(tor.hasNext()){
System.out.println(iterator.next());
}
}
}
Set接口实现类TreeSet:
TreeSet实现了Set接口,它与HashSet的区别主要在于TreeSet中的元素会按照相关的值进行排序。
HashSet是通过HashMap实现的,TreeSet是通过TreeMap实现的,只不过Set用的只是Map的key。
由于TreeMap需要排序,所以需要一个Comparator为键值进行大小比较.当然也是用Comparator定位的.如果创建时没有确定,那么就会使用pareTo()方法,这就要求key必须实现Comparable接口.TreeMap是使用Tree数据结构实现的,所以使用compare接口就可以完成定位了.
注意:
TreeSet是根据对象的CompareTo方法来去重的,如果CompaerTo返回0说明两个对象相等,不能同时存在于TreeSet中。
[html]viewplaincopy
14px;">publicclassTreeSetTest{
publicstaticvoidmain(String[]args){
pre">SettreeSet=newTreeSet();
pre">treeSet.add("d");
pre">pre">treeSet.add("c");
pre">pre">treeSet.add("b");
pre">pre">treeSet.add("a");
pre">//String实体类中实现Comparable接口,所以在初始化TreeSet的时候,
pre">//无需传入比较器
pre">Iteratoriterator=treeSet.iterator();
pre">while(iterator.hasNext()){
pre">System.out.println(iterator.next());
pre">}
pre">SettreeSet2=newTreeSet();
pre">treeSet2.add(newNum
(1));
pre">treeSet2.add(newNum(3));
pre">treeSet2.add(newNum
(2));
pre">treeSet2.add(newNum(3));
pre">treeSet2.add(newNum(3));
pre">treeSet2.add(newNum(6));
System.out.println(treeSet2.size());
pre">Iteratoriterator2=treeSet2.iterator();
pre">while(iterator2.hasNext())
pre">{pre">
pre">System.out.println(iterator2.next());
pre">}
pre">TreeSetset=newTreeSet<>();
pre">set.add(1111);
pre">set.add(2222);
pre">set.ad