JDK5.0中一些collection类的使用详解

开发 后端
JDK5.0中collection类的使用对于编程人员来说并不陌生,那么对于初学者掌握JDK5.0中一些collection类的使用是十分有用的,本文就向你介绍相关信息。

在JDK5.0中,collection最大的一个改变就是可以指定它的具体类型,具体有哪些呢,让我们来看看:

List list=new List;

JDK5.0中collection两个最基本的接口:

  1. public interface Collection  {     
  2. boolean add(E element);    
  3.  Iterator iterator();    
  4.  . . .     
  5. }     
  6. public interface Iterator   {     
  7.   E next();    
  8.  boolean hasNext();    
  9.  void remove();   } 

在JDK5.0以前,常用的形式就是:

  1. Collection c = . . .;    
  2.  Iterator iter = c.iterator();     
  3. while (iter.hasNext())   {     
  4. String element = iter.next();    
  5.  do something with element    
  6.  }  
  7.  
  8. 但是在5.0中加入另外一种循环方式,类似于for each:  
  9.  
  10. for (String element : c)   {   
  11.  do something with element    
  12.  } 

这种方式对任何实现了Iterable接口的类都适用。

在使用remove的时候特别要注意的一点是,在调用remove之前必须先调用一次next方法,因为next就像是在移动一个指针,remove删掉的就是指针刚刚跳过去的东西。即使是你想连续删掉两个相邻的东西,也必须在每次删除之前调用next。

对collection排序和查找

JDK5.0中Collections类的sort方法可以对任何实现了List接口的类进行排序。在排序过程中,他默认这些类实现了Comparable接口,如果想用其他方法排序,可以在调用sort方法的时候提供一个Comparator对象:

  1. Comparator itemComparator = new   Comparator()   {   
  2.   public int compare(Item a, Item b)   {    
  3.   return a.partNumber - b.partNumber;    
  4.    }    
  5.  }; 

反向排序:

Collections.sort(items, itemComparator);

Collections.sort(items, Collections.reverseOrder(itemComparator));

查找一个对象:

i = Collections.binarySearch(c, element);  

i = Collections.binarySearch(c, element, comparator);

但是这些list必须是已经排好序了。而且要注意的是这个算法需要随机访问collection,如果不支持随机访问那么这个算法的效率可能会很低。

JDK5.0中几种常用Collection:

ArrayList

An indexed sequence that grows and shrinks dynamically

可以随机访问,但是如果要从中间删除一个对象会影响效率,因为有些未删除的对象要相应的调整位置。非线程安全,但效率会比Vector要高,如果在单线程下,选它而不是Vector。

LinkedList

An ordered sequence that allows efficient insertions and removal at any location

只能按顺序访问,添加删除很方便。虽然提供了get(n)方法,但实际上还是顺序访问的,如果发现在LinkedList里面使用了这个方法,要考虑这个List类型是否选的合适

HashSet

An unordered collection that rejects duplicates

以hashcode为索引,适用于不知道所存对象位置而想寻找某个对象的情况。不可重复

TreeSet

A sorted set

与HashSet类似,但是所存对象是排了序的

LinkedHashSet

A set that remembers the order in which elements were inserted

PriorityQueue

A collection that allows efficient removal of the smallest element

加入Queue的时候会给与一个优先级,从queue中取出的时候先取出优先级最低的

HashMap

A data structure that stores key/value associations

存储key/value对,非线程安全,与HashTable相比效率要高些

treeMap

A map in which the keys are sorted

排序的HashMap

LinkedHashMap

A map that remembers the order in which entries were added

那么JDK5.0中一些collection类的常见使用就是这些了,是不是对你了解JDK5.0中的collection类有帮助呢?
 

【编辑推荐】

  1. JDK的概念、组成及JDK常用包
  2. JDK1.4在Windows下的环境配置
  3. JDK1.6在LINUX下的安装配置
  4. JDK日志框架之实例结合STAF浅析
  5. JDK日志分级作为核心API最佳实践浅析
责任编辑:仲衡 来源: 百度空间
相关推荐

2009-07-08 17:33:46

JDK5.0内置工具

2009-07-09 11:02:37

JDK5.0内置工具

2009-07-07 16:22:13

JDK5.0源代码

2011-07-19 18:11:09

iPhone 开发

2011-06-16 14:28:08

Qt Symbian 文件

2018-09-11 16:15:36

Vue高版本前端

2011-06-24 14:46:23

Qt

2017-05-23 14:33:46

简历求职前端开发

2012-12-24 14:51:02

iOS

2022-05-24 12:50:58

Pandas索引代码

2013-03-29 09:03:59

iOS实用小代码iOS开发

2011-03-16 10:40:42

JavaEEJ2EE

2023-11-13 07:54:54

.NET Core开源框架

2014-03-19 15:41:21

编程语言编程规则

2014-08-08 09:14:43

Linux浏览器

2021-04-23 07:51:56

CSS Container Q Chrome

2013-07-24 09:32:13

Android项目

2009-09-10 09:15:38

监视程序死锁

2016-11-16 21:18:42

android日志

2010-03-25 13:59:52

Python API
点赞
收藏

51CTO技术栈公众号