java集合框架整理

Collection:
set(接口)
AbstractCollection(抽象类)
AbstractList->List
AbstractSequentialList
LinkedList(实现了Deque双端队列接口)
Vetor<-Stack
ArrayList
AbstractSet(实现了Set接口)
TreeSet:实现了NavigableSet->SortedSet->Set
List(接口)
Queue(接口)
接口
Collection Set List Queue SortedSet NavigableSet
分析:
1:所有的类或接口都是实现了Collection接口
2:TreeSet,HashSet,LinkedHashSet实现了Set接口,其中TreeSet实现了SortedSet
3:ArrayList,Vector,LinkedList实现了List接口
java.util.Collection
所有集合的根接口
增加:
add(o:E):boolean
addAll(c:Collection):boolean
删除
remove(o:E):boolean
removeAll(c:Collection):boolean
clear():boolean
修改:
查:
contains(o:Object):boolean
containsAll(c:Collection):boolean
其他:
equal(o:Object)
hashCode();int //返回集合的散列码
size():int
toArray():Object[] //返回集合的数组形式
isEmpty()
iterator():Interator //返回一个迭代器
备注:Iterator接口
hasNext()
Next()
Set集合
Set接口扩展了Collection接口。它没有引入新的方法或常量,只是规定set的实例不包含重复的元素
1,Set接口依靠hashCode和equals()完成重复元素的判断,关注这点在日后的map接口中也有体现
2,TreeSet依靠Comparable接口来完成排序的操作
3,Set方法使用时,元素为自定义类时,自定义类要腹泻Object类中的equals(),hashCode(),toString()方法
4,具体实现类HashSet,TreeSet,LinkedHashSet
5,特别注意,规则集中不存在重复元素,所以添加重复元素时会返回false,但是不会有异常
散列集java.util.HashSet
初始容量为16,客座率为0.75
当元素个数超过容量*客座率的乘积,容量就会自动翻倍
初始化
HashSet()
HashSet(c:Collection)
HashSet(initialCapacity:int)
HashSet(initialCapacity:int,loadFactor:float)
方法:
实现了Collection接口中的方法
链式散列表LinkedHashSet
用一个链表来扩展HashSet,支持对规则集内的元素排序(所谓的排序就是按照添加的顺序存储,访问的时候也是按照添加的顺序)。HashSet中的元素是没有被排序的,而LinkedHashSet中的元素可以按照他们插入规则集的顺序提取。
初始化:
LinkedHashSet()
LinkedHashSet(c:Collection)
LinkedHashSet(initialCapacity:int)
LinkedHashSet(initialCapacity:int,loadFactor:float)
方法:
实现了Collection中的方法
Set方法没有访问索引的方法,只有add,remove,contains类方法
树形集TreeSet
SortedSet是Set的一个子接口,可以确保规则集中的元素时有序的。另外,它还提供方法first,last方法以返回规则集中的第一个和最后一个,以及方法headSet((toElement))和tail(fromElement)返回规则集中小

于toElement或大于等于fromElement的部分
NavigableSet扩展了SortedSet,并提供导航方法lower(e),floor(e),ceiling(e),higher(e)以分别返回小于,小于等于,大于等于以及大于给定元素的元素。如果没有就返回null。pollFirst,pollLast会分别删除和返回树形集中的第一个元素和最后一个元素
TreeSet是SortedSet的一个具体类,元素必须是可比较的,即元素实现了comparable接口或被指定了比较器
总结一下方法
TreeSet普通方法:
Set自我感觉提供的是一种存储的服务,并不擅长找出这个位置,所以他的存储是无序的,因此方法上与list就不一样了,不需要有index参数
初始化
TreeSet()
TreeSet(c:Collection)
TreeSet(comparator:Comparator)
TreeSet(s:SortedSet)
增加:
add()
addAll()
删除:
remove()
removeAll()
clear()
修改:
查找:
contains()
containsAll()
其他(长度,迭代器,哈希code等):
size();
toArray()
isEmpty()
iterator()
equals()
hashCode()
TreeSet的特殊方法(实现了接口SortedSet和NavigableSet的方法):
注意:使用这些方法时,定义对象的类型必须是SortedSet
增加:

删除:
pollFirst()
pollLast()
查找:
导航方法
首个元素: first() //空集和时会引发异常
尾元素: last()
小于e的集合 headSet():SortedSet //不存在返回空集
大于等于e的集合 tailSet():SotedSet //不存在返回空集
大于e higher(e) //不存在返回null
大于等于 ceiling(e) //不存在返回null
小于 lower(e) //不存在返回null
小于等于 floor(e) //不存在返回null
修改:
其他:
小技巧:
1、当定义hashset时可以使用Set set = new HashSet()定义
2、定义TreeSet时,最后直接使用TreeSet set = new TreeSet()
Comparable接口
位于https://www.360docs.net/doc/7b10426544.html,ng
public interface Companrable{
public int compareTo(T o);
}
Comparator接口
有时候希望将元素插入一个树集合中,这些元素可能不是https://www.360docs.net/doc/7b10426544.html,parable的实例,这时可以定义一个比较器来比较这些元素。要做到这一点创建一个https://www.360docs.net/doc/7b10426544.html,parator接口的类。
comparator接口有两个方法:
public int compare(Object e1,Object e2)
public boolean equals(Object e)




线性表
规则集中只能存储不重复的值,为了存储重复的值需要使员工线性表。
List接口扩展了Collection接口,以定义一个允许重复的有序集合。List接口增加了面向位置的操作,并且增加了一个能够双向遍历线性表的迭代器
方法:
1、可以使用Collection中的方法
2、操作中分为面向元素和面向位置两种方式
增加:
add(index:int):boolean
add(index:int,c:Collection)
add(E) //末尾添加

addAll(c:Collection); //末尾添加
删除:
remove(index:int)
remove(o)
removeAll(c)
修改:
set(index:int,e):E
查找:
//查找元素
get(index:int):E
contains(o):boolean
contains(c):boolean
//面向索引
indexOf(e)
lastIndexOf(e)
其他
listIterator():ListIterator
listIterator(startIndex:int):ListIterator
subList(fromIndex:int,toIndex:int):List //返回从fromIndex到toIndex-1的子列表
java.util.ListIterator
add(o:E):void
向后:
hasNext():boolean
Next():E
nextIndex():int //返回下一个元素的下标
remove():void
向前:
hasPrevious()
previous()
previousIndex():index //返回前一个元素的下标
set(E):void //使用指定的元素替换previous或next返回的最后一个元素
数组线性表ArrayList
java.util.ArrayList:
初始化:
List list = new ArrayList();
ArrayList()
ArrayList(c:Collection)
ArrayList(capacity:int)
增加:
add(o:E):void
add(index:int,o:E):void//插入元素
删除:
remove(o:E):boolean
remove(index:int):boolean//索引必须合理,否则有异常出现
clear()
修改:
set(index:int,o:E):E //索引必须合理,否则有异常出现
查:
get(index:int):E
indexOf(o:E):int //不存在返回-1
lastIndexOf(o:E):int //不存在返回-1
contains(o:E):boolean
isEmpty():boolean
长度:
size()
自动减小
线性表不会自动减小,只会自动增加
trimToSize() //容量减小到这个列表的当前大小
链表线性表LinkedList
实现了List的接口和Queue接口
初始化
LinkedList()
LinkedList(c:Collection)
增加:
addFist(o:E) //添加到列表头
addLast(o:E) //添加到列表尾
//List中的方法
add(index)
add(index,e)
addAll(c)
删除
removeFirst()
removeLast()
//List
remove(index)
查找
getFirst()
getLast()
//List
get(index)
indexOf(e)
lastIndexOf(e)
其他
//List
listIterator():ListIterator
listIterator(index):ListIterator()
subList(start,end); //start~end-1
Collection静态方法
排序(改进归并排序)
sort(list:List):void
sort(list:List,c:Comparator):void
查找
binarySearch(list,key)
binarySearch(list,key,comparator)
颠倒
reverse(list)
倒序
reverseOrder();//返回逆序的比较器
随机打乱
shuffle(list)
shuffle(list,random) //用随机对象打乱
复制
copy(des:List,src:List):void
nCopies(n:int,o:Object):List //返回o的n个副本的列表
填充
fill(list,o) //用指定元素替换线性表中的所有元素
最大值
max(c:Collection):Object
max(c:Collection,c:comparator):Object
最小值
min(c):Object
min(c1,c2):Object
某元素出现次数
frequency(c:Collection,o:Object):int
向量类Vector
与ArrayList几乎一样,只是添加了同步方法

实现了List方法
初始化
Vector()
Vector(c:Collection)
Vector(initialCapacity:int);
Vector(initialCapacity:int,CapacityIncr:int)
增加:
addElement(o:E)
insertElementAt(o:E,index:int)
删除
removeAllElements()
removeElement(o)
removeElementAt(index)
修改:
setElementAt(o,index)
查找
firstElement()
lastElement()
ElementAt(index)
Stack
Stack()
empty()
pop()
push()
peek():返回栈顶元素
search(o): 返回栈中指定元素的位置
Queue
offer(e):向队列中插入一个元素
poll(): 出队,空时返回null
remove():出队,空时出现异常
peek(): 获取队首元素,为空时返回null
element():获取队首元素,为空时出现异常
Deque 双端队列
LinkedList 实现了Deque接口,因此可以使用LinkedList创建一个双端队列
addFirst()
addLast()
removeFirst()
removeLast()
getFirst()
getLast()


Map
用于存储键值对形式的数据
map接口提供的方法
查询
get(key):V
values():Collection
keySet():Set
entrySet():Set>
containsKey(key):boolean
containsValue(value):boolean
isEmpty()
size()
增加
put(key,value)
put(map)
删除
remove(key)
clear()
修改
HashMap

LinkedHashMap
使用linked list实现来扩展HashMap
TreeMap
在遍历排好序的键值时高效
有新的导航方法
firstKey()
lastKey()
headMap(toKey)
tailMap(fromKey)

floorKey(key)
ceillingKey(key)
lowerKey(key)
higherKey(key)
附:map的遍历方法
Set> set = map.entrySet();
for(Map.Entry m : set){
key=m.getKey();
value=m.getValue();
}

相关文档
最新文档