您的位置:首页 > 其它

自己封装的一个泛型集合基类

2009-03-11 21:22 525 查看
自己封装的一个泛型集合基类

自己揣摩着写的,希望路过的大虾指正为感!最好能附上写得更完善的例程,谢谢!

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
* BaseCollection
* 实体集合基类
* @author CodingMouse
* @version 0.1
* 2009.2.28
* @param <T> 实体集合类存取的类型限定
* 说明:实现Iterable<T>接口即可通过Foreach语法遍历你的底层序列
* 	    不过该接口要求提供一个标准的Iterator迭代器接口实现。
*      在本类中,已经使用匿名内部类为其提供了标准的Iterator实现。
*/
public class BaseCollection<T> implements Serializable, Iterable<T>, Comparator<T> {

/**
* 序列版本号
*/
private static final long serialVersionUID = 1L;
/**
* 实体集合类内部数据存取载体
*/
protected List<T> collection = new ArrayList<T>();

/**
* 向列表的尾部追加指定的元素
* @param item 要追加到列表的元素
* @return 元素添加是否成功
*/
public boolean add(T item) {

return this.collection.add(item);

}

/**
* 在列表的指定位置插入指定元素
* @param index 要在其中插入指定元素处的索引
* @param item 要插入的元素
* @return 元素添加是否成功
*/
public boolean add(int index, T item) {

int size = this.size();
this.collection.add(index, item);
return this.size() == (size + 1);

}

/**
* 追加指定 collection 中的所有元素到此列表的结尾
* @param items 其元素要被添加到此列表的 collection
* @return 如果此列表随调用的结果发生改变,则返回 true
*/
public boolean addAll(List<T> items) {

return this.collection.addAll(items);

}

/**
* 将指定 collection 中的所有元素都插入到列表中的指定位置
* @param index 将指定 collection 的第一个元素所插入位置的索引
* @param items 要插入到列表中的元素
* @return 如果此列表随调用的结果发生改变,则返回 true
*/
public boolean addAll(int index, List<T> items) {

return this.collection.addAll(index, items);

}

/**
* 从列表中移除所有元素
* @return 列表是否清空
*/
public boolean clear() {

this.collection.clear();
return this.isEmpty();

}

/**
* 如果列表包含指定的元素,则返回 true
* @param o 要测试列表中是否存在的元素
* @return 如果列表包含指定的元素,则返回 true
*/
public boolean contains(Object o) {

return this.collection.contains(o);

}

/**
* 如果列表包含指定 collection 的所有元素,则返回 true
* @param items 要在列表中检查其包含性的 collection
* @return 如果列表包含指定 collection 的所有元素,则返回 true
*/
public boolean containsAll(List<T> items) {

return this.collection.containsAll(items);

}

/**
* 比较指定的对象与列表是否相等
* @param o 要与此列表进行相等性比较的对象
* @return 如果指定对象与此列表相等,则返回 true
*/
public boolean equals(Object o) {

return this.collection.equals(o);

}

/**
* 获取集合中指定位置的元素
* @param index 位置索引下标
* @return 元素
*/
public T get(int index) {

return this.collection.get(index);

}

/**
* 返回列表的哈希码值
* @return 此列表的哈希码值
* 说明:列表的哈希码定义为以下计算的结果:
*		hashCode = 1;
*		Iterator i = list.iterator();
*		while (i.hasNext()) {
* 			Object obj = i.next();
* 			hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
*		}
*		这确保了 list1.equals(list2) 意味着对于任何两个列表 list1 和 list2 而言,可实现 list1.hashCode()==list2.hashCode(),正如 Object.hashCode 的常规协定所要求的。
*/
public int hashCode() {

return this.collection.hashCode();

}

/**
* 返回列表中首次出现指定元素的索引,如果列表不包含此元素,则返回 -1
* @param o 要搜索的元素
* @return 返回列表中手册出现指定元素的索引,如果列表不包含该元素,则返回 -1
*/
public int indexOf(Object o) {

return this.collection.indexOf(o);

}

/**
* 如果列表不包含元素,则返回 true
* @return 如果列表不包含元素,则返回 true
*/
public boolean isEmpty() {

return this.collection.isEmpty();

}

/**
* 使用匿名内部类实现Iterator<T>迭代器接口
* @return Iterator<T>迭代器实现类实例
*/
public Iterator<T> iterator() {

//		/**
//		 * 匿名内部类
//		 */
//		return new Iterator<T>() {
//
//			/**
//			 * 保存集合当前指针
//			 */
//			private int index = 0;
//
//			/**
//			 * 如果仍有元素可以迭代,则返回 true
//			 * @return 是否仍有元素可以迭代
//			 */
//			public boolean hasNext() {
//
//				/**
//				 * 检查指针位置
//				 */
//				return index < collection.size();
//
//			}
//
//			/**
//			 * 返回迭代的下一个元素
//			 * @return 迭代的下一个元素
//			 */
//			public T next() {
//
//				/**
//				 * 返回当前指针指向的元素并将指针向下移动一位
//				 */
//				return collection.get(index++);
//
//			}
//
//			/**
//			 * 删除当前元素
//			 * 从迭代器指向的集合中移除迭代器返回的最后一个元素(可选操作)
//			 * 每次调用 next 只能调用一次此方法
//			 */
//			public void remove() {
//
//				/**
//				 * 移除当前元素
//				 */
//				collection.remove(index);
//
//			}
//
//		};

return this.collection.iterator();

}

/**
* 返回列表中最后出现指定元素的索引,如果列表不包含此元素,则返回 -1
* @param o 要搜索的元素
* @return 返回列表中最后出现指定元素的索引,如果列表不包含此元素,则返回 -1
*/
public int lastIndexOf(Object o) {

return this.collection.lastIndexOf(o);

}

/**
* 返回列表中元素的列表迭代器(以正确的顺序)
* @return 列表中元素的列表迭代器(以正确的顺序)
*/
public ListIterator<T> listIterator() {

return this.collection.listIterator();

}

/**
* 返回列表中元素的列表迭代器(以正确的顺序),从列表的指定位置开始
* @param index 从列表迭代器返回的首个元素的索引(通过调用 next 方法)
* @return 列表中元素的列表迭代器(以正确的顺序),从列表中的指定位置开始
*/
public ListIterator<T> listIterator(int index) {

return this.collection.listIterator(index);

}

/**
* 移除列表中指定位置的元素
* @param index 要移除的元素的索引
* @return 以前在指定位置的元素
*/
public T remove(int index) {

return this.collection.remove(index);

}

/**
* 移除列表中出现的首个指定元素
* @param o 要从该列表中移除的元素,如果存在的话
* @return 如果列表包含指定的元素,则返回 true
*/
public boolean remove(Object o) {

return this.collection.remove(o);

}

/**
* 从列表中移除指定 collection 中包含的所有元素
* @param items 定义将从此列表中移除哪些元素的 collection
* @return 如果此列表随调用的结果发生改变,则返回 true
*/
public boolean removeAll(List<T> items) {

return this.collection.removeAll(items);

}

/**
* 仅在列表中保留指定 collection 中所包含的元素
* @param items 定义此 set 将保留哪些元素的 collection
* @return 如果此列表随调用的结果发生改变,则返回 true
*/
public boolean retainAll(List<T> items) {

return this.collection.retainAll(items);

}

/**
* 用指定元素替换列表中指定位置的元素
* @param index 要替换的元素的索引
* @param item 要在指定位置存储的元素
* @return 以前在指定位置的元素
*/
public T set(int index, T item) {

return this.collection.set(index, item);

}

/**
* 获取集合所包含元素个数
* @return 集合元素个数
*/
public int size() {

return this.collection.size();

}

/**
* 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图
* @param fromIndex subList 的低端(包括)
* @param toIndex subList 的高端(不包括)
* @return 列表中指定范围的视图
*/
public List<T> subList(int fromIndex, int toIndex) {

return this.collection.subList(fromIndex, toIndex);

}

/**
* 返回以正确顺序包含列表中的所有元素的数组
* @return 以正确顺序包含该列表中所有元素的数组
*/
public Object[] toArray() {

return this.collection.toArray();

}

/**
* 返回以正确顺序包含列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型
* @param <T> 包含列表中元素的数组类型限定
* @param arr 要存储列表中元素的数组,如果它足够大的话;否则为此目的分配一个运行时类型相同的新数组
* @return 包含列表中元素的数组
*/
@SuppressWarnings("hiding")
public <T> T[] toArray(T[] arr) {

return this.collection.toArray(arr);

}

/**
* 元素排序
*/
public void sort() {

/**
* 使用自定义排序规则调用集合的排序方法实现元素排序
*/
Collections.sort(this.collection, this);

}

/**
* 元素倒转
*/
public void reverse() {

// 倒转元素
Collections.reverse(this.collection);

}

/**
* 重写toString方法
*/
public String toString() {

// 保存结果
String content = "";

// 遍历拼接集合元素内容
for (T tmp : this.collection) {

content += tmp.toString() + "/r/n";

}

// 返回结果
return content;

}

/**
* 元素比较方法
* 默认比较hashCode哈希码
* @param parmFirst 第一个元素
* @param parmSecond 第二个元素
*/
public int compare(T parmFirst, T parmSecond) {

/**
* 如果参数类型不相同则不进行比较
*/
if (parmFirst.getClass() != parmFirst.getClass()) {
return 0;
}

/**
* 默认比较hashCode哈希码
*/
return parmFirst.hashCode() > parmSecond.hashCode() ? 1
: (parmFirst.hashCode() == parmSecond.hashCode() ? 0 : -1);

}

}


CodingMouse

2009年3月11日
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: