您的位置:首页 > 编程语言 > Java开发

Java常用对象API 二 集合框架 (一)

2014-04-02 16:37 381 查看
集合框架



A.

由来:

对象用于封装特有数据,不确定个数的对象,可以用集合容器存储。

集合特点:

1、用于存储对象的容器;

2、集合的长度式可变的;

3、集合不可以存储基本数据类型。

B.

集合容器依据数据结构不同,有多种具体容器。不断向上抽取,就形成了集合框架。

框架的顶层Collection接口:

Collection常见方法:

1、增加:add(Object);addAll(Collection);

2、删除:remove();removeAll();clear();

3、判断:contains(Object),containsAll(Collection);isEmpty();

4、获取:int length();size();Iterator iterator();迭代器。该对象必须依赖于具体容器,因为内部数据结构不同。所以该迭代器对象实在容器内部实现的。

Iterator接口就是对所有Collection容器进行元素取出的公共接口。

5、其他:

boolean retainAll(Collection);求交集。

Object toArray();将集合转为数组。

C.

迭代器:

使用Collection中的iterator方法,是为了得到Collection中的迭代器对象。

next()

hasNext()

降低耦合性

*实现了Iterator接口的容器内的内部类

D.

Collection:

|___List:有序(存入取出顺序一致),元素都有索引,元素可以重复。

|___Set:

元素不可以重复,无序,可以有序

List:

|___Vector:内部是数组数据结构,是同步的,增删查询都较慢;

|___ArrayList:内部是数组数据结构,是不同步的,替代了Vector,查询的速度很快;

|___LinkedList:内部是链表数据结构,非同步,增删元素的效率很高。

List:特有的常见方法:有一个共性就是都可以操作角标。

(1) 添加:

void add(index,element);

void add(index,collection);

(2) 删除:

Object remove(index);

(3)修改:

Object set(index,element);

(4) 获取:

Object get(index);

Int indexOf(Object);

Int lastIndexOf(object);

List subList(from,to);

*集合与迭代器并发修改时会出现异常。这时可以用listIterator()列表迭代器。

*Enumeration枚举功能和迭代器重合:

Enumeration en = v.elements();

hasMoreElements();

nextElement();

$ LinkedList:

addFirst();

addLast();

getFirst();//获取但不移除,链表为空抛出异常。

peekFirst();//获取但不移除,链表为空返回null.

peekLast();

removeFirst();//获取并移除,链表为空抛出异常。

removeLast();//获取并移除,链表为空返回null。

*LinkedList面试题:

package com.hyace;
/**
* LinkedList面试题:模拟一个堆栈或者队列:
* @author Hyace
*
*/
import java.util.*;

class Que{
private LinkedList link;
Que(){
link = new LinkedList();
}
public void push(Object obj){
link.addFirst(obj);
}
public Object pop(){
return link.removeLast();
}

}

class Sta{
private LinkedList link;
Sta(){
link = new LinkedList();
}
public void push(Object obj){
link.addFirst(obj);
}
public Object pop(){
return link.removeFirst();
}

}
public class LinkedTest {
public static void main(String[] args){
List li = new ArrayList();
for(int i=0;i<26;i++){
li.add((char)('a'+i));
}
printList(li);

Que q = new Que();
System.out.print("队列操作:");
for(int i=0;i<26;i++){
q.push((char)('A'+i));
}
for(int i=0;i<26;i++){
System.out.print(q.pop()+",");
}
System.out.println();

Sta s = new Sta();
System.out.print("栈操作:");
for(int i=0;i<26;i++){
s.push((char)('A'+i));
}
for(int i=0;i<26;i++){
System.out.print(s.pop()+",");
}
System.out.println();

}

public static void printList(List li) {
for(Iterator it = li.iterator();it.hasNext();){
System.out.print(it.next()+",");
}
System.out.println();

}

}




$ ArrayList:

*默认容量为10;

add();

$Set:元素不可以重复,无序,可以有序.方法和Collection一致。

|___hashset

HashSet集合数据结构是哈希表,所以存储元素的时候,是使用hashcode方法来确定位置,如果位置相同,通过equals来判断。

*重写hashCode时,可以以某成员变量乘一数,以使hashcode尽量不同。

*hashSet实例:

public class HashTest {
public static void main(String[] args) {
HashSet h = new HashSet();

for(Iterator it=h.iterator();it.hasNext();){
System.out.print(it.next()+",");
}
h.add(new Person("Adam0",0));
h.add(new Person("Adam0",0));
h.add(new Person("Adam5",5));
h.add(new Person("Adam2",2));
h.add(new Person("Adam3",3));
h.add(new Person("Adam4",4));

for(Iterator it = h.iterator();it.hasNext();){
Person p = (Person)it.next();
System.out.println(p);
}
}

}


输出是:



重写hashcode()和equals()方法后,hashSet中不存在重复元素:



当将其中的hashSet换为LinkedHashSet时,就可以有序存储。

$ LindedHashSet类

存储有序

B.

TreeSet:

可以对set集合中的元素进行排序,不同步。

对于自定义类排序,需要实现Comparable接口

判断元素唯一性的方式,就是根据比较方法的返回结果。

TreeSet对元素进行排序的方式一:

放元素自身具备比较功能,实现Comparable接口,覆盖compareTo方法。

*重写compareTo方法,使TreeSet有序输出:

public int compareTo(Object obj) {
Person o = (Person) obj;
int temp = this.name.compareTo(o.name);
return temp==0?this.age-o.age:temp;
}




如果不要按照对象中的自然顺序排序,或者无自然顺序可以使用:

TreeSet对元素进行排序的方式二:

让集合具备此功能,定义一个实现Comparator,把此对象当做参数传入TreeSet的构造方法中。

可以根据具体对象创建一个比较器:

implements Comparator{}

其中要重写compare方法:

public int compare(o1,o2){}

比较器比自然顺序优先级高,常用此法。

如果用比较器重写以上程序:

public class ComparatorOfPerson implements Comparator {

@Override
public int compare(Object o1, Object o2) {
Person p1 = (Person)o1;
Person p2 = (Person)o2;

int temp = p2.getName().compareTo(p1.getName());

return temp==0?p2.getAge()-p1.getAge():temp;
//return 1;
}
}


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