动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ Java教程 ]的信息

本月文章推荐
.浅析Java的“克隆”方法.
.SUN ONE架构.
.必须执行清除.
.name 属性.
.Spring中的Template和Callback模.
.Apache Tomcat 5.5.16.
.Beans入门必读之无状态会话bean基.
.Java基础:Date和Calendar类的使.
.JAVA程序员必读:基础篇(5).
.j2ee性能调优之最小化资源压力测.
.Java的封装类.
.Java做一个最简单的Socket通话程.
.Peer-to-Peer Sockets 工程入门.
.Java开发工具与开发环境问答集.
.JBOSS-QL中的like、order by的实.
.java/vc单点登录的简单实现.
.21天学通J2EE3.
.Java网络加载协议(JNLP)及Java W.
.快报:Microsoft Visual J# .NET.
.正则表达式在网络编程中的运用.

正确使用ArrayList和LinkedList—性能的改进

发表日期:2008-1-5 |



  USING ARRAYLIST AND LINKEDLIST
  
  ArrayList and LinkedList are two Collections classes used for storing lists of object references. For example, you could have an ArrayList of Strings, or a LinkedList of Integers. This tip compares the performance of ArrayList and LinkedList, and offers some suggestions about which of these classes is the right choice in a given situation.
  
  The first key point is that an ArrayList is backed by a primitive Object array. Because of that, an ArrayList is mUCh faster than a LinkedList for random Access, that is, when accessing arbitrary list elements using the get method. Note that the get method is implemented for LinkedLists, but it requires a sequential scan from the front or back of the list. This scan is very slow. For a LinkedList, there's no fast way to access the Nth element of the list.
  
  Consider the following example. Suppose you have a large list of sorted elements, either an ArrayList or a LinkedList. Suppose too that you do a binary search on the list. The standard binary search algorithm starts by checking the search key against the value in the middle of the list. If the middle value is too high, then the upper half of the list is eliminated. However, if the middle value is too low, then the lower half of the list is ignored. This process continues until the key is found in the list, or until the lower bound of the search becomes greater than the upper bound.
  
  Here's a program that does a binary search on all the elements in an ArrayList or a LinkedList:
  
  import Java.util.*;
  
  public class ListDemo1 {
  static final int N = 10000;
  
  static List values;
  
  // make List of increasing Integer values
  
  static {
  Integer vals[] = new Integer[N];
  
  Random rn = new Random();
  
  for (int i = 0, currval = 0; i < N; i++) {
  vals[i] = new Integer(currval);
  currval += rn.nextInt(100) + 1;
  }
  
  values = Arrays.asList(vals);
  }
  
  // iterate across a list and look up every
  // value in the list using binary search
  
  static long timeList(List lst) {
  long start = System.currentTimeMillis();
  
  for (int i = 0; i < N; i++) {
  
  // look up a value in the list
  // using binary search
  
  int indx = Collections.binarySearch(
  lst, values.get(i));
  
  // sanity check for result
  // of binary search
  
  if (indx != i) {
  System.out.println(
  "*** error ***\n");
  }
  }
  
  return System.currentTimeMillis() - start;
  }
  
  public static void main(String args[]) {
  
  // do lookups in an ArrayList
  
  System.out.println("time for ArrayList = " +
  timeList(new ArrayList(values)));
  
  // do lookups in a LinkedList
  
  System.out.println(
  "time for LinkedList = " +
  timeList(new LinkedList(values)));
  }
  }
  
  The ListDemo1 program sets up a List of sorted Integer values. It then adds the values to an ArrayList or a LinkedList. Then Collections.binarySearch is used to search for each value in the list.
  
  When you run this program, you should see a result that looks something like this:
  
  time for ArrayList = 31
  
  time for LinkedList = 4640
  
  ArrayList is about 150 times faster than LinkedList. (Your results might differ depending on your machine characteristics, but you should see a distinct difference in the result for ArrayList as compared to that for LinkedList. The same is true for the other programs in this tip.) Clearly, LinkedList is a bad choice in this situation. The binary search algorithm inherently uses random access, and LinkedList does not support fast random access. The time to do a random access in a LinkedList is proportional to the size of the list. By comparison, random access in an ArrayList has a fixed time.
  
  You can use the RandomAccess marker interface to check whether a List supports fast random access:
  
  void f(List lst) {
  if (lst instanceof RandomAccess) {
  // supports fast random access
  }
  }
  
  ArrayList implements the RandomAccess interface, and LinkedList. does not. Note that Collections.binarySearch does take advantage of the RandomAccess property, to optimize searches.
  
  Do these results prove that ArrayList is always a better choice? Not necessarily. There are many cases where LinkedList does better. Also note that there are many situations where an algorithm can be implemented efficiently for LinkedList. An example is reversing a LinkedList using Collections.reverse. The internal algorithm does this, and gets reasonable performance, by using forward and backward iterators.
  
  Let's look at another example. Suppose you have a list of elements, and you do a lot of element inserting and deleting to the list. In this case, LinkedList is the better choice. To demonstrate that, consider the following "worst case" scenario. In this demo, a program repeatedly inserts elements at the beginning of a list. The code looks like this:
  
  import java.util.*;
  
  public class ListDemo2 {
  static final int N = 50000;
  
  // time how long it takes to add
  // N objects to a list
  
  static long timeList(List lst) {
  long start = System.currentTimeMillis();
  
  Object obj = new Object();
  
  for (int i = 0; i < N; i++) {
  lst.add(0, obj);
  }
  
  return System.currentTimeMillis() - start;
  }
  
  public static void main(String args[]) {
  
  // do timing for ArrayList
  
  System.out.println(
  "time for ArrayList = " +
  timeList(new ArrayList()));
  
  // do timing for LinkedList
  
  System.out.println(
  "time for LinkedList = " +
  timeList(new LinkedList()));
  }
  }
  
  When you run this program, the result should look something like this:
  
  time for ArrayList = 4859
  
  time for LinkedList = 125
  
  These results are pretty much the reverse of the previous example.
  
  When an element is added to the beginning of an ArrayList, all of the existing elements must be pushed back, which means a lot of eXPensive data movement and copying. By contrast, adding an element to the beginning of a LinkedList simply means allocating an internal record for the element and then adjusting a couple of links. Adding to the beginning of a LinkedList has fixed cost, but adding to the beginning of an ArrayList has a cost that's proportional to the list size.
  
  So far, this tip has looked at speed issues, but what about space? Let's look at some internal details of how ArrayList and LinkedList are implemented in Java 2 SDK, Standard Edition v 1.4. These details are not part of the external specification of these classes, but are illustrative of how such classes work internally.
  
  The LinkedList class has a private internal class defined like this:
  
  private static class Entry {
  Object element;
  Entry next;
  Entry previous;
  }
  
  Each Entry object references a list element, along with the next and previous elements in the LinkedList -- in other Words, a doubly-linked list. A LinkedList of 1000 elements will have 1000 Entry objects linked together, referencing the actual list elements. There is significant space overhead in a LinkedList structure, given all these Entry objects.
  
  An ArrayList has a backing Object array to sto
上一篇:Reference 不为人知的一面 人气:333
下一篇:垃圾自动收集系统指导 (1) 人气:364
浏览全部Java的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-9-6 Movie34电影搜索引擎 v3.0
2008-9-6 wap2.0仿帝国建站喜用 v2.0
2008-9-6 免费人才招聘网 宽屏版 v3.01
2008-9-6 喜喔喔视频采集程序 v1.0 beta
2008-9-6 ASP客户管理系统
2008-9-6 主流驿站中秋祝福程序
2008-9-6 php实现msn协议的类
2008-9-5 Coppermine Photo Gallery v1.4.
2008-9-5 清松网络日记本 v2.4
2008-8-23 Mini WinMount V0.4
2008-8-23 Vista优化大师3.11正式版
2008-8-23 Wine 1.13
2008-8-23 KlipFolio 5.0 Build 5899-80
2008-8-23 Windows Sysinternals Desktops
2008-8-23 OneTap Movies1.2破解版
2008-8-23 AnnotaterPDF阅读1.1.503 破解版
2008-8-23 SoundMeter分贝测量仪 v1.0汉化破
2008-8-23 iDrum音乐节拍1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | Html转换js | js/vbs加密 | md5加密 | 进制转换
实用工具:汉字翻译拼音 | 符号对照表 | 个税计算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号