您的位置:首页 > 编程语言 > Java开发

2.Java集合总结系列:List接口及其实现

2017-03-29 21:14 507 查看
在介绍List接口之前,我们先来看看 Collection 接口,因为Collection接口是 List / Set / Queue 接口的父接口,List / Set / Queue 的实现类中很多的操作方法其实还是调用Collection类定义的方法。


一、Collection接口

在Collection接口中,定义了如下的方法:



其中方法可以分为以下几类:

数据操作类方法:add/addAll/remove/removeAll/clear/retainAll/iterator

判断类方法:contains/containsAll/equals/hashcode/isEmpty/size

所有继承 Collection 接口的集合都可以用 Collection 中的方法进行元素操作,而具体的集合类有根据其特性增加了一些其特有的方法。

1、数据操作类方法:add/addAll/remove/removeAll/clear/retainAll/iterator

Collection接口-数据操作类方法MARK

2、判断类方法:contains/containsAll/equals/hashcode/isEmpty/size


 Collection接口-判断类方法


二、List接口

List接口在Collection接口的基础上拓展了一些方法,增加了一些自己独有的方法,主要是增加了这样的几类方法:

1、在 add / remove / addAll 中增加了 index 参数,使其可以在任意位置插入删除

add(int index, E element) / addAll(int index, E element)

可以在特定位置插入删除元素,而不只是插入集合末尾

remove(int index)  

删除集合中特定下标的元素(removeAll() 是移除集合所有元素,暂时没有移除一个区间的元素的 API。但 ArrayList 类实现了,对应的方法是: removeRange(int fromIndex, int toIndex) )


 View
Code

2、增加了获取元素以及设置元素值的方法

get(int index) / set(int index, E element)      

可以获取(设置)特定位置上的元素


 View
Code

3、其他方法

subList(int fromIndex, int toIndex)  获取子集合

indexOf(Object o) / lastIndexOf(Object o)  获取特定元素下标

listIterator() / listIterator(int index)  获取一个ListIterator对象(ListIterator对象具有比Iterator更强大的遍历集合功能。Iterator只能向后遍历,但ListIterator扩展了Iterator接口,不仅可以向后遍历也可以向前遍历)


 View
Code

 三、ArrayList实现类



在ArayList实现类中,我们通常用Collection接口提供的add/addAll/remove/removeAll/iterator方法进行数据的增删改查:


 View
Code

然而ArrayList实现类比起List接口,ArrayList实现类拥有了以下几个特有的方法:

clone()  复制一个集合实例

removeRange(int fromIndex, int toIndex)   移除指定范围内的元素

ensureCapacity(int minCapacity)   扩大集合大小,使其至少容纳minCpacity个元素

trimToSize()   缩小集合大小到其现在的大小



package com.chanshuyi.collection.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
* ArrayList 有序允许重复  非线程同步
* @author Administrator
*/
public class ArrayListTest{

public static void main(String[] args) {
//增加
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("Tom");
nameList.add("Marry");
nameList.add("Henry");
nameList.add("Jensus");

//1.复制集合
@SuppressWarnings("unchecked")
ArrayList<String> copyList = (ArrayList<String>) nameList.clone();
//Tom Marry Henry Jensus
printCol(nameList);
//Tom Marry Henry Jensus
printCol(copyList);

//2.扩大集合大小
nameList.ensureCapacity(8);

//3.缩小集合大小
//集合大小:4
System.out.println("集合大小:" + nameList.size());
nameList.trimToSize();
//集合大小:4
System.out.println("集合大小:" + nameList.size());
}

public static <E> void printCol(Collection<E> col){
Iterator<E> it = col.iterator();
while(it.hasNext()){
System.out.print(it.next().toString() + " ");
}
System.out.println();
}
}




removeRange(int fromIndex, int toIndex) 方法是protected方法,只能在包内或子类中使用,因此如果要使用removeRange方法,只能用继承ArrayList类的方法构造一个类,然后用这个类去调用removeRange()方法,如:



package com.chanshuyi.collection.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

@SuppressWarnings("serial")
public class ArrayListTest2<E> extends ArrayList<E> {

public static void main(String[] args) {
ArrayListTest2<String> list = new ArrayListTest2<String>();
list.add("1");
list.add("2");
list.add("3");

list.removeRange(1, 2);
printCol(list);    //1 3
}
public static <E> void printCol(Collection<E> col){
Iterator<E> it = col.iterator();
while(it.hasNext()){
System.out.print(it.next().toString() + " ");
}
System.out.println();
}
}




四、LinkedList实现类



LinkedList同样实现了Collection和List接口,所以同样可以用这两个接口中的方法进行数据操作:


 View
Code

但其实LinkedList类也实现了DQueue接口,所以LinkedList也可以使用push/pop等方法进行堆栈结构的操作,但这不属于Collection和List的范围,因此我们将在Queue接口时进行介绍。

五、Vector实现类



Vector实现类与ArrayList实现类的区别就是Vector是线程安全的,而ArrayList是线程不安全的,因此Vector效率较低。因此在非多线程的情况下推荐用ArrayList,而在多线程环境下适合用Vector。

一般操作Vector时也是使用Collection接口和List接口提供的方法:


 View
Code

六、Stack实现类



Stack类是Vector类的子类,因此它也可以用Collection和List接口提供的add/remove等方法。

Stack类是堆栈结构的一个模拟,实现了自己独有的进栈、出栈等方法。



package com.chanshuyi.collection.list;

import java.util.Stack;

public class StackTest {

public static void main(String[] args) {
//入栈 push()
Stack<String> stack = new Stack<String>();
stack.push("1");
stack.push("2");
stack.push("3");

//出栈  pop()
stack.pop();

//取得栈顶元素 peek()
System.out.println(stack.peek());

//通过堆栈是否为空来迭代 isEmpty()
while(!stack.isEmpty()){
System.out.print(stack.pop() + " ");
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据 java 继承 arraylist