Java类集复习笔记

内存模型

————上图为集合类间接口的实现关系————


1.了解java设置类集合的主要目的。
2.掌握Collection接口的作用和主要方法
3.掌握Collection子接口List Set的区别和常用子类的使用
4.掌握Map接口的作用,及其常用子类
5.SortedSet SortedMap
6.Iterator,Enumeration,foreach
7.掌握properties的使用
8.了解工具类Collection的使用

基本概念:所谓类集就是一个动态的对象数组,不受对象数组长度的限制,类集必须是
容易拓展和修改的。

Collection:public interface Collection extends Iterable
toArray()将集合变成对象数组

List:public interface List extends Collection
可以存放重复的内容
常用方法:add(在指定位置插入元素时,所有其他元素后移一位),addAll(),get(),indexOf(查找自定元素的位置),ListIterator(),set(替换元素)
remove():自定义的类如果要删除就要复写equals,hashCode方法;

ArrayList和Vector类比较:
ArrayList:新操作类,异步处理,性能好,用Iterator,foreach输出
Vector:旧操作类,同步处理,性能差,用Iterator,foreach,Enumeration输出
Vector方法是addElement

LinkedList:数据结构链表实现FIFO,实现了Queue和List接口
addFirst():在链表的开头增加元素
addLast():在链表的结尾增加元素

Set:不能存放重复的内容,所有的重复内容靠hashCode()和equals()方法区分;
HashSet:散列存放
对象删除重复需要对象复写equals方法和hashCode方法
代码;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Person1 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "年齡=" + this.age + "名字=" + this.name;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Person1))
{
return false;
}
Person1 p = (Person1) obj;
if (this.name.equals(p.name) && this.age == this.age) {
return true;
} else {
return false;
}
}
public int hashCode() {
return this.name.hashCode() * this.age;
}
}

TreeSet:有序排列
对象删除重复需要每个对象都实现Comparable接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Person implements Comparable<Person> {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "年齡=" + this.age + "名字=" + this.name;
}
public int compareTo(Person o) {
if (this.age > o.getAge()) {
return 1;
} else if (this.age < o.getAge()) {
return -1;
} else {
return this.name.compareTo(o.name);
}
}
}

Queue:队列接口
peek():找到头结点
poll()找到头节点并删除
element()找到链表的表头

SortedSet:对集合中的数据进行排序

Iterator:迭代输出,是使用最多的输出方式
ListIterator:是Iterator的子接口,专门用于list集合输出,双向输出
Enumeration:旧接口,功能与Iterator类似
foreach:可以输出数组或者集合,jdk1.5之后的功能

Iterator:
Iterator it = list.iterator();
方法:hasNext();是否有下一个值
next();输出当前元素
remove();删除当前元素

注意点:
一个集合把内容交给迭代器删除时候,因为集合也有remove方法,删除的时候调用
了自身的删除方法,迭代器就会出现运行时错误(因为集合本身被破坏掉,所以迭代器
就会中止迭代出现错误)
remove之前必须要执行it.next指向该元素,否则会出现异常

ListIterator
如果想完成双向输出,则首先要完成由前向后的输出。

Enumeration:
无删除操作,类似于Iterator
想要实现此接口只能通过Vector类elements方法

Map

HashMap:无序存放,key不允许重复,key可以为null
Hashtable:无需存放,旧的操作类,key不允许重复,key不可null
TreeMap:可以排序的Map集合,按照key排序,不允许重复
WeakTreeMap:弱引用Map集合,不在使用某些内容时gc回收
IdentityHashMap:key可以重复的Map集合

Map:
keySet()方法,可以将所有的key变成一个Set集合
values()方法返回Collection对象,得到所有value值

TreeMap 按照key排序
对象实现comparable接口

Map迭代:
1.将Map接口的实例通过entrySet()方法变成Set接口对象
2.通过set接口实例为Iterator实例化
3.迭代输出每个对象都是Map.Entry对象
4.通过Map.Entry进行key–value的分离

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Map<String,String> m=new HashMap<String,String>();
m.put("hah0", "heh0");
m.put("hah1", "heh1");
m.put("hah2", "heh2");
m.put("hah3", "heh3");
m.put("hah4", "heh4");
Set<Map.Entry<String, String>> s = m.entrySet();
Iterator<Map.Entry<String, String>> it =s.iterator();
while(it.hasNext()){
Map.Entry<String, String> me =it.next();
System.out.println(me.getKey()+me.getValue());
}
}
}

如果要使用自己的匿名類作為key,則必須要复写其类中的equals和hashCode方法

SortedMap:
同SortedSet类似的原理

文章目录