java集合类(二)List学习
接上篇 java集合类(一)
- List接口继承了Collection接口和Iterable接口,即同样含有Collection和 Iterable的特性,还有方法,其基本方法有:
1)有关添加: boolean add(E e):添加元素 void add(int index,E element):在特定位置添加元素
boolean addAll(Collection<? extends E> c):添加集合中所有的元素 boolean addAll(int index,Collection<? extends E> c):在特定位置添加一组元素
2)有关清除:void clear(), E remove(int index),boolean remove(Object o),boolean remove(Collection<?> c)
3)有关检验:boolean contains(Object o),boolean containAll(Collection<?> c),boolean equals(Object o),boolean isEmpty()
4)有关获取:E get(int index),int hashCode(),int indexOf(Object o),int lastIndexOf(Object o),int size(),Lisy<E> subList(int fromindex,int toindex)
5)有关设定:E set(int index, E element),boolean retainAll(Collection<?> c)
6)有关打印:Object[] toArray(), <T>T[] toArray(T[] a)
7)有关迭代:Iterator<E> iterator(),ListIterator<E> listIterator(),ListIterator<E> listIterator(int index)
8)其他:int hashcode()
List接口的实现类有很多,如:AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList,
RoleUnresolvedList, Stack, Vector,下面介绍List的几个基本实现类的用法:
- About ArrayList:
All Implemented Interfaces: Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess extends AbstractList
- Direct Known Subclasses: AttributeList, RoleList, RoleUnresolvedList
实例代码:
class iphone{} public void arraylist(){
//1)Constructs an empty list with an initial capacity of ten
ArrayList al = new ArrayList();
/*
* 2)Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's iterator
*/
ArrayList<Integer> alist = new ArrayList(Arrays.asList(1,2,3));
//3)Constructs an empty list with the specified initial capacity
ArrayList<Double> alist1 = new ArrayList(10);
al.add(new iphone());
al.add(1);
al.add("a");
al.add(alist);
System.out.println(al.hashCode()); //int hashcode() /*
* usage of methods that aren't mentioned within "List basic methods"
*/
//ensure that list can hold at least the number of elements specified by the minimum capacity argument
alist1.ensureCapacity(30);
//Trims the capacity of this ArrayList instance to be the list's current size
alist.trimToSize(); //Returns a shallow copy of this ArrayList instance(The elements themselves are not copied)
Object o = al.clone();
}
- About LinkedList:
All Implemented Interfaces:Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E> extends AbstractSequentialList<E>
从上面可以知道,由于LinkedList实现的接口涉及到队列,所以它也会新增一些有关队列独有操作方法,还有pop(),push()等,下面举例:
public void linkedlist(){
LinkedList linklist = new LinkedList();
ArrayList<Integer> a = new ArrayList(Arrays.asList(1,2,3));
LinkedList linklist2 = new LinkedList(a); Iterator i = linklist2.iterator();
while(i.hasNext()){
System.out.println(i.next()); //1 2 3
}
Iterator ri = linklist2.descendingIterator();
while(ri.hasNext()){
System.out.println(ri.next()); //3 2 1
} linklist.addFirst(1);
linklist.addLast("e");
System.out.println(linklist); //[1,e]
/*linklist.getFirst();
linklist.getLast();*/ //Retrieves, but does not remove, the head (first element) of this list
System.out.println(linklist.element()); //1 linklist.offer("x");
System.out.println(linklist);//[1,e,x]
linklist.offerFirst(0);
linklist.offerLast("y");
System.out.println(linklist);//[0,1,e,x,y] //peek..()--->[Retrieves, but does not remove]
System.out.println(linklist.peek());//0
System.out.println(linklist.peekFirst());//0 ,list is empty,return "null"
System.out.println(linklist.peekLast());//y,list is empty, return "null" //poll..()--->[Retrieves and removes]
System.out.println(linklist.poll()); //0
System.out.println(linklist.pollFirst());//1
System.out.println(linklist.pollLast()); //y
System.out.println(linklist);//[e,x] /*linklist.removeFirst(); //return first element
linklist.removeFirstOccurrence(Object); //boolean
linklist.removeLast(); //return last element
linklist.removeLastOccurrence(Object); //boolean
*/ System.out.println(linklist2); //[1 2 3]
//Pops an element from the stack represented by this list
System.out.println(linklist2.pop()); //1
//Pushes an element onto the stack represented by this list
linklist2.push("m");
System.out.println(linklist2);//[m,2,3]
}
- ArrayList 和LinkedList的比较:
1.ArrayList是基于动态数组,LinkedList基于链表
2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针
3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据
验证:
public void comparearraylistwithlinklist(){
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(f.format(new Date())); ArrayList<Integer> a = new ArrayList<Integer>();
long starttime = new Date().getTime();
for(int i = 0; i<10000; i++){
a.add(i);
}
long finishtime = new Date().getTime();
System.out.println(finishtime-starttime); LinkedList<Integer> l = new LinkedList<Integer>();
long lstarttime = new Date().getTime();
for(int i = 0; i<10000; i++){
l.add(i);
}
long lfinishtime = new Date().getTime();
System.out.println(lfinishtime-lstarttime);
}
输出:
额外说明:以上验证代码是基于较大量数据的,输出也是不稳定的,即答案也不能确定,可能是我用错测试方法,也可能是因为数据量不够大,也可能是因为getTime()获得的是毫秒,程序可能需要更精确的时间单位,这样才有办法比较。另外,如果对于单个数据的插入或删除,是不是LinkedList还优于ArrayList呢?答案也很明显是不一定的,读者可以按照上面的实例验证一下
由于此博文可能有点长了,其他List的学习见“java集合类(三)List学习(续)”,尽请期待!!
### 学习从来都是一个过程,对对错错对对...若文中有错误,还望读者批评指出 ###
java集合类(二)List学习的更多相关文章
- java集合类源码学习一
对于java的集合类,首先看张图 这张图大致描绘出了java集合类的总览,两个体系,一个Collection集合体系一个Map集合体系.在说集合类之前,先说说Iterable这个接口,这个接口在jdk ...
- java集合类源码学习三——ArrayList
ArrayList无疑是java集合类中的一个巨头,而且或许是使用最多的集合类.ArrayList继承自AbstractList抽象类,实现了List<E>, RandomAccess, ...
- java集合类源码学习二
我们查看Collection接口的hierarchy时候,可以看到AbstractCollection<E>这样一个抽象类,它实现了Collection接口的部分方法,Collection ...
- java集合类(二)
第十六天知识点总结 一.泛型 泛型:java jdk 1.5 新特性. 泛型的好处: 1.运行时的错误提前到编译时. 2.避免无谓的强制类型转换 自定义方法泛型:自定义泛型就是一个数据类型的占位或一个 ...
- Java集合类源码学习- Iterabel<T>,Colection<E>,AbstractCollection<E>
Collection<E>接口extends Iteratable<E>接口. Iteratable<T>:实现接口使得对象能够成为“for-each loop”的 ...
- Java集合类综合
Java集合类是JDK学习中的一个经典切入点,也是让初学者最初感受到Java魅力的地方之一,你一定不会忘记不需要关心大小的ArrayList,不用自己实现的Queue,和随处可见的HashMap.面试 ...
- 201871010123-吴丽丽《面向对象程序设计(Java)》第十二周学习总结
201871010123-吴丽丽<面向对象程序设计(Java)>第十二周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 201871010106-丁宣元 《面向对象程序设计(java)》第十二周学习总结
201871010106-丁宣元 <面向对象程序设计(java)>第十二周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...
- 201271050130-滕江南-《面向对象程序设计(java)》第十二周学习总结
201271050130-滕江南-<面向对象程序设计(java)>第十二周学习总结 项 目 内 容 这个作业属于哪个课程 https://www.cnblogs.co ...
随机推荐
- 【转载】看懂SqlServer查询计划
看懂SqlServer查询计划 阅读目录 开始 SQL Server 查找记录的方法 SQL Server Join 方式 更具体执行过程 索引统计信息:查询计划的选择依据 优化视图查询 推荐阅读-M ...
- Android异步下载网络图片
最近新做的一个项目,里面需要下载网络上的图片,并显示在UI界面上,学Android有个常识,就是Android中在主线程中没法直接更新UI的,要想更新UI必须另外开启一个线程来实现,当开启的线程完成图 ...
- part 2 Angular modules and controllers
What is a module in AngularJS? A module is a container for different parts of your application i.e c ...
- C# HttpWebRequest类
HttpWebRequest类与HttpRequest类的区别. HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息.而HttpWebReque ...
- HDU1042 N! 大数的阶乘
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 由于数字特别大, 所以用数组来模拟. 经测试, 数组大小开50000 可过. 附AC代码, 欢迎 ...
- gcc常用命令介绍
GCC 全称是 GNU C Compiler,是gnu中最流行的c & c++编译器,下面我们看一下一些主要的参数使用方法. 对于一个源文件可以直接生成可执行文件 gcc test.c 默认生 ...
- 网易面试题:和为n连续正数序列
题目: 输入一个正数n,输出所有和为n连续正数序列.例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5.4-6和7-8. 继续做些题目,看到这是网易面试题,于是 ...
- 基础学习总结(四)--SQLite
1. SQLiteDatabase 操作SQLite数据库的类.可以执行SQL语句,对数据库进行增.删.查.改的操作.也可以进行transaction的控制.很多类对数据库的操作最终都是通过SQL ...
- DataGridView导入导出excel
DataGridView导出到Excel #region 方法一 DateGridView导出到csv格式的Excel /// <summary> /// 导出数据到Excel.常用方法, ...
- phpStudy 2016 更新下载,新版支持php7.0
目标:让天下没有难配的php环境. phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS7/8/6 『软件简介』该程序包集成 ...