java开发中的List的使用
2009-01-09 16:54
232 查看
Java容器类包含List、ArrayList、Vector及map、HashTable、HashMap
ArrayList和HashMap是异步的,Vector和HashTable是同步的,所以Vector和HashTable是线程安全的,而 ArrayList和HashMap并不是线程安全的。因为同步需要花费机器时间,所以Vector和HashTable的执行效率要低于 ArrayList和HashMap。
List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。
和下面要提到的Set不同,List允许有相同的元素。
除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个 ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加,删除,设定元素, 还能向前或向后遍历。
实现List接口的常用类有LinkedList,ArrayList,Vector和Stack。
ArrayList类
ArrayList实现了可变大小的数组。它允许所有元素,包括null。ArrayList没有同步。
size,isEmpty,get,set方法运行时间为常数。但是add方法开销为分摊的常数,添加n个元素需要O(n)的时间。其他的方法运行时间为线性。
每个ArrayList实例都有一个容量(Capacity),即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加,但是增长算法 并没有定义。当需要插入大量元素时,在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。
和LinkedList一样,ArrayList也是非同步的(unsynchronized)。
如果定义了一个List , 想要比较List里面的内容, 我们需要重写类中和hastcode方法和equals 方法 .
重写hascode方法是比较简单的,或者说是个模式性的东西 , 只需要在类中添加一个 public int hashCode()方法 ,并将类中的所有变量进行一个序列化即可.
如:
public int hashCode()
{
return Name.hashCode()*11+method.hashCode()*7+start;
}
注: 其中的变量Name 、method是String类型的变量, 而start则是int型. 这三个变量是这个类中定义的三个私的有变量.
重写 public int hashCode()方法也是一个固定的模式
如:
public boolean equals (Object obj)
{
if (this == obj)
return true;
if ((obj == null) || !(obj.getClass() == this.getClass()))
return false;
Location other = (Location) obj;
return this.Name.equals(other.className)
&& this.method.equals(other.methodName)
&& this.start==(other.startLine)
}
注:Location是这个类的名字, 把这个程序完整的放到一个类中可以表示为:
public class Location
{
:
private String Name = null;
private String method = null;
private int start = -1;
:
:
public List<Location> getMeaasge()
{
List<Losation> message = new ArrayList<Location>();
:
:
//判断加到list的内容是否有与Location同名的对外对象, 如果想等则要加入
if(!message .contains(methLoca))
// 如果不在程序重写hascode方法和equal方法则这个判断是无效的
message .add(methLoca);
return message
}
public int hashCode()
{
return Name.hashCode()*11+method.hashCode()*7+start;
}
public boolean equals (Object obj)
{
if (this == obj)
return true;
if ((obj == null) || !(obj.getClass() == this.getClass()))
return false;
Location other = (Location) obj;
return this.Name.equals(other.className)
&& this.method.equals(other.methodName)
&& this.start==(other.startLine)
}
}
比较List就是这么简单
,基本上是个模板化的东西
ArrayList和HashMap是异步的,Vector和HashTable是同步的,所以Vector和HashTable是线程安全的,而 ArrayList和HashMap并不是线程安全的。因为同步需要花费机器时间,所以Vector和HashTable的执行效率要低于 ArrayList和HashMap。
List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下标)来访问List中的元素,这类似于Java的数组。
和下面要提到的Set不同,List允许有相同的元素。
除了具有Collection接口必备的iterator()方法外,List还提供一个listIterator()方法,返回一个 ListIterator接口,和标准的Iterator接口相比,ListIterator多了一些add()之类的方法,允许添加,删除,设定元素, 还能向前或向后遍历。
实现List接口的常用类有LinkedList,ArrayList,Vector和Stack。
ArrayList类
ArrayList实现了可变大小的数组。它允许所有元素,包括null。ArrayList没有同步。
size,isEmpty,get,set方法运行时间为常数。但是add方法开销为分摊的常数,添加n个元素需要O(n)的时间。其他的方法运行时间为线性。
每个ArrayList实例都有一个容量(Capacity),即用于存储元素的数组的大小。这个容量可随着不断添加新元素而自动增加,但是增长算法 并没有定义。当需要插入大量元素时,在插入前可以调用ensureCapacity方法来增加ArrayList的容量以提高插入效率。
和LinkedList一样,ArrayList也是非同步的(unsynchronized)。
如果定义了一个List , 想要比较List里面的内容, 我们需要重写类中和hastcode方法和equals 方法 .
重写hascode方法是比较简单的,或者说是个模式性的东西 , 只需要在类中添加一个 public int hashCode()方法 ,并将类中的所有变量进行一个序列化即可.
如:
public int hashCode()
{
return Name.hashCode()*11+method.hashCode()*7+start;
}
注: 其中的变量Name 、method是String类型的变量, 而start则是int型. 这三个变量是这个类中定义的三个私的有变量.
重写 public int hashCode()方法也是一个固定的模式
如:
public boolean equals (Object obj)
{
if (this == obj)
return true;
if ((obj == null) || !(obj.getClass() == this.getClass()))
return false;
Location other = (Location) obj;
return this.Name.equals(other.className)
&& this.method.equals(other.methodName)
&& this.start==(other.startLine)
}
注:Location是这个类的名字, 把这个程序完整的放到一个类中可以表示为:
public class Location
{
:
private String Name = null;
private String method = null;
private int start = -1;
:
:
public List<Location> getMeaasge()
{
List<Losation> message = new ArrayList<Location>();
:
:
//判断加到list的内容是否有与Location同名的对外对象, 如果想等则要加入
if(!message .contains(methLoca))
// 如果不在程序重写hascode方法和equal方法则这个判断是无效的
message .add(methLoca);
return message
}
public int hashCode()
{
return Name.hashCode()*11+method.hashCode()*7+start;
}
public boolean equals (Object obj)
{
if (this == obj)
return true;
if ((obj == null) || !(obj.getClass() == this.getClass()))
return false;
Location other = (Location) obj;
return this.Name.equals(other.className)
&& this.method.equals(other.methodName)
&& this.start==(other.startLine)
}
}
比较List就是这么简单
,基本上是个模板化的东西
相关文章推荐
- Java开发基础——数组、List与Map的使用
- 【Android开发】用户界面设计-使用XML和Java代码混合控制UI界面
- 使用二进制代表状态(JAVA开发巧用二进制)
- 使用JAVA和C#开发Ribbon界面
- atitit.提升开发效率---使用服务器控件生命周期 asp.net 11个阶段 java jsf 的6个阶段比较
- JavaWeb开发-MD5加密算法(直接使用java中的MessageDigest)
- java中List集合的使用
- Java中使用webservice,简化开发(xfire的webservice)
- 搭建java开发环境+配置tomcat+使用eclipse______软件开发-JAVA
- 使用GCC开发动态库供java调用
- 实战突击:Java Web项目整合开发光盘使用
- 使用Eclipse-Maven-git做Java开发(11)--egit环境准备
- Symbian S60开发,在SettingList中使用RadioButton的问题及解决方法。
- 使用java开发的一些技巧
- 使用Java开发定制标记库遇到的小问题——<body-content>
- 机器学习|如何使用Spark 开发Java电子邮件垃圾分类应用程序?
- Java界面开发工具(WindowBuilder)配置使用
- 使用实时 Java 进行开发,第 1 部分: 探索实时 Java 的独特功能
- java之mongodb客户端开发(maven工程,使用mongodb jar包)
- Android开发使用Java8新特性