Java容器——Collection

一、容器:

Java API所提供的一系列类的实例,用于在程序中存放对象。

JDK所提供的容器API位于java.util包内。

容器API的类图结构:

Collection接口定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。

     Set中的数据对象无序且不可重复;

     List中的数据对象有序且可重复。

Map接口定义了存储“key-value”映射对的方法。

二、Collection接口方法

	boolean add(Object element)
	boolean addAll(Collection c)
	void clear()
	boolean contains(Object o)
	boolean containsAll(Collection c)
	boolean equals(Object o)
	int hashCode()
	boolean isEmpty()
	Iterator iterator()
	boolean remove(Object o)
	boolean removeAll(Collection c)
	boolean retainAll(Collection c)
	int size()
	Object[] toArray()

容器类对象在调用remove、contains等方法时需要比较对象是否相等,这会涉及对象类型的equals()和hashCode()方法。对于自定义的类型,需要重写equals()和hashCode()方法以实现自定义的对象相等规则。(注意:相等的对象应该具有相等的hash codes)

示例:

import java.util.*
public class TestCollection {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		//可以放入不同类型的对象
		c.add("hello");
		c.add(new Name("f1", "l1"));
		c.add(new Integer(100));	//可以添加对象,不可以添加基本数据类型
		c.remove("hello");
		boolean isOk = c.remove(new Name("f1", "l1"));
		System.out.println(isOk);	//false;重写后为true
		System.out.println(c);	//[f1 l1, 100];重写后为[100]
	}
}

class Name {
	private String firstName;
	private String lastName;
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString() {
		return firstName + " " + lastName;
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof Name) {
			Name name = (Name)obj;
			return (firstName.equals(name.firstName)) 
				&& (lastName.equals(name.lastName));
		}
		return super.equals(obj);
	}
	
	public int hashCode() {
		return firstName.hashCode();
	}
}


三、Iterator接口

所有实现了Collection接口的容器类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象。

(不论是List还是Set,是LinkedList还是ArrayList,没有人比它自身更知道如何遍历自己,所有实现了Collection接口的类都有iterator()方法,返回一个Iterator类型的对象,就在于此。)

Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作。

Iterator接口只有3个方法,仅用于遍历:

	boolean hasNext();
	Object next();
	void remove();

四、Set接口

Set接口是Collection的子接口,Set接口没有提供额外的方法。实现Set接口的容器类中的元素是无序且不可重复的。(集合)

JDK所提供的Set容器类有HashSet,TreeSet等。

五、List接口

List接口是Collection的子接口,实现List接口的容器类中的元素是有序且可重复的。

List容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素。

JDK所提供的List容器类有ArrayList,LinkedList等。

方法:

	void add(int index, Object element)
	boolean addAll(int index, Collection c)
	Object get(int index);
	int indexOf(Object o);
	int lastIndexOf(Object o);
	ListIterator listIterator();
	Object remove(int index);
	Object set(int index, E element);
	List subList(int fromIndex, int toIndex);


java.util.Collections类提供了一些静态方法实现了基于List容器的一些常用算法。java.util.Collections类提供了一些静态方法实现了基于List容器的一些常用算法。

	void sort(List)		//排序
	void shuffle(List)	//随机排列
	void reverse(List)	//逆序排列
	void fill(List, Object)	//用一个特定对象重写整个List容器
	void copy(List dest, List src)
	int binarySearch(List, Object)	//对于顺序的List容器,采用二分法查找特定对象


示例:

public class TestList {
	public static void main(String[] args) {
		List l1 = new LinkedList();
		List l2 = new LinkedList();
		for (int i=0; i<=9; i++) {
			l1.add("a" + i);
		}
		System.out.println(l1);
		Collections.shuffle(l1);	//随机排列
		System.out.println(l1);
		Collections.reverse(l1);	//逆序
		System.out.println(l1);
		Collections.sort(l1);	//先排序,
		System.out.println(l1);
		System.out.println(Collections.binarySearch(l1, "a5"));	//再二分法查找
	}
}


六、Comparable接口

问题:Collections中的算法根据什么确定容器中对象的“大小”顺序?

所有可以“排序”的类都实现了java.lang.Comparable接口,Comparable接口中只有一个方法:

	int compareTo(Object o)

实现了Comparable接口的类通过实现compareTo()方法确定该类对象的排序方式。

示例:

public class TestCompare {
	public static void main(String[] args) {
		List l1 = new LinkedList();
		l1.add(new Name("Karl", "M"));
		l1.add(new Name("Steven", "Lee"));
		l1.add(new Name("John", "O"));
		l1.add(new Name("Tom", "M"));
		System.out.println(l1);
		Collections.sort(l1);
		System.out.println(l1);
	}
}


class Name implements Comparable {
	private String firstName;
	private String lastName;
	public Name(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public String toString() {
		return firstName + " " + lastName;
	}
	
	public boolean equals(Object obj) {
		if(obj instanceof Name) {
			Name name = (Name)obj;
			return (firstName.equals(name.firstName)) 
				&& (lastName.equals(name.lastName));
		}
		return super.equals(obj);
	}
	
	public int hashCode() {
		return firstName.hashCode();
	}
	
	public int compareTo(Object o) {
		Name n = (Name)o;
		int lastCmp = lastName.compareTo(n.lastName);
		return 
			(lastCmp != 0 ? lastCmp : 	//先比较姓氏
			firstName.compareTo(n.firstName));
	}
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值