java集合类整理

Collection:set(接口)AbstractCollection(抽象类)AbstractList->ListAbstractSequentialListLinkedList(实现了Deque双端队列接口)Vetor<-StackArrayListAbstractSet(实现了Set接口)TreeSet:实现了NavigableSet->SortedSet->SetList(接口)Queue(接口) 接口Collection Set List Queue SortedSet NavigableSet分析:1:所有的类或接口都是实现了Collection接口2:TreeSet,HashSet,LinkedHashSet实现了Set接口,其中TreeSet实现了SortedSet3:ArrayList,Vector,LinkedList实现了List接口java.util.Collection所有集合的根接口增加:add(o:E):booleanaddAll(c:Collection<? extends E>):boolean删除remove(o:E):booleanremoveAll(c:Collection<?>):booleanclear():boolean修改:查:contains(o:Object):booleancontainsAll(c:Collection<?>):boolean其他:equal(o:Object)hashCode();int //返回集合的散列码size():inttoArray():Object[] //返回集合的数组形式isEmpty()iterator():Interator<E>//返回一个迭代器备注:Iterator接口hasNext()Next()Set集合Set接口扩展了Collection接口。它没有引入新的方法或常量,只是规定set的实例不包含重复的元素1,Set接口依靠hashCode和equals()完成重复元素的判断,关注这点在日后的map接口中也有体现2,TreeSet依靠Comparable接口来完成排序的操作3,Set方法使用时,元素为自定义类时,自定义类要腹泻Object类中的equals(),hashCode(),toString()方法4,具体实现类HashSet,TreeSet,LinkedHashSet5,特别注意,规则集中不存在重复元素,所以添加重复元素时会返回false,但是不会有异常散列集java.util.HashSet初始容量为16,客座率为0.75当元素个数超过容量*客座率的乘积,容量就会自动翻倍初始化HashSet()HashSet(c:Collection<? extends E>)HashSet(initialCapacity:int)HashSet(initialCapacity:int,loadFactor:float)方法:实现了Collection接口中的方法链式散列表LinkedHashSet用一个链表来扩展HashSet,支持对规则集内的元素排序(所谓的排序就是按照添加的顺序存储,访问的时候也是按照添加的顺序)。HashSet中的元素是没有被排序的,而LinkedHashSet中的元素可以按照他们插入规则集的顺序提取。初始化:LinkedHashSet()LinkedHashSet(c:Collection<? extends E>)LinkedHashSet(initialCapacity:int)LinkedHashSet(initialCapacity:int,loadFactor:float)方法:实现了Collection中的方法Set方法没有访问索引的方法,只有add,remove,contains类方法树形集TreeSetSortedSet是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<? extends E>)TreeSet(comparator:Comparator<? super E>)TreeSet(s:SortedSet<E>)增加: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> //不存在返回空集大于等于e的集合 tailSet():SotedSet<E> //不存在返回空集大于ehigher(e) //不存在返回null大于等于 ceiling(e) //不存在返回null小于lower(e) //不存在返回null小于等于 floor(e) //不存在返回null修改:其他:小技巧:1、当定义hashset时可以使用Set<E> set = new HashSet<E>()定义2、定义TreeSet时,最后直接使用TreeSet<E> set = new TreeSet<E>() Comparable接口位于java.langpublic interface Companrable<T>{public int compareTo(T o);}Comparator接口有时候希望将元素插入一个树集合中,这些元素可能不是java.lang.comparable的实例,这时可以定义一个比较器来比较这些元素。要做到这一点创建一个java.util.Comparator接口的类。comparator接口有两个方法:public int compare(Object e1,Object e2)public boolean equals(Object e)线性表规则集中只能存储不重复的值,为了存储重复的值需要使员工线性表。List接口扩展了Collection接口,以定义一个允许重复的有序集合。List接口增加了面向位置的操作,并且增加了一个能够双向遍历线性表的迭代器方法:1、可以使用Collection中的方法2、操作中分为面向元素和面向位置两种方式增加:add(index:int):booleanadd(index:int,c:Collection<? extends E>)add(E) //末尾添加addAll(c:Collection<? extends E>); //末尾添加删除:remove(index:int)remove(o)removeAll(c)修改:set(index:int,e):E查找://查找元素get(index:int):Econtains(o):booleancontains(c):boolean//面向索引indexOf(e)lastIndexOf(e)其他listIterator():ListIterator<E>listIterator(startIndex:int):ListIterator<E>subList(fromIndex:int,toIndex:int):List<E> //返回从fromIndex到toIndex-1的子列表java.util.ListIteratoradd(o:E):void向后:hasNext():booleanNext():EnextIndex():int//返回下一个元素的下标remove():void向前:hasPrevious()previous()previousIndex():index //返回前一个元素的下标set(E):void //使用指定的元素替换previous或next返回的最后一个元素数组线性表ArrayListjava.util.ArrayList<E>:初始化:List<E> list = new ArrayList<E>();ArrayList<E>()ArrayList<E>(c:Collection<? extends E>)ArrayList<E>(capacity:int)增加:add(o:E):voidadd(index:int,o:E):void//插入元素删除:remove(o:E):booleanremove(index:int):boolean//索引必须合理,否则有异常出现clear()修改:set(index:int,o:E):E //索引必须合理,否则有异常出现查:get(index:int):EindexOf(o:E):int //不存在返回-1lastIndexOf(o:E):int //不存在返回-1contains(o:E):booleanisEmpty():boolean长度:size()自动减小线性表不会自动减小,只会自动增加trimToSize() //容量减小到这个列表的当前大小链表线性表LinkedList实现了List的接口和Queue接口初始化LinkedList()LinkedList(c:Collection<? extends E>)增加:addFist(o:E) //添加到列表头addLast(o:E) //添加到列表尾//List中的方法add(index)add(index,e)addAll(c)删除removeFirst()removeLast()//Listremove(index)查找getFirst()getLast()//Listget(index)indexOf(e)lastIndexOf(e)其他//ListlistIterator():ListIterator<E>listIterator(index):ListIterator<E>()subList(start,end);//start~end-1Collection静态方法排序(改进归并排序)sort(list:List):voidsort(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):voidnCopies(n:int,o:Object):List //返回o的n个副本的列表填充fill(list,o) //用指定元素替换线性表中的所有元素最大值max(c:Collection):Objectmax(c:Collection,c:comparator):Object最小值min(c):Objectmin(c1,c2):Object某元素出现次数frequency(c:Collection,o:Object):int向量类Vector与ArrayList几乎一样,只是添加了同步方法实现了List方法初始化Vector()Vector(c:Collection<? extends E>)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)StackStack()empty()pop()push()peek():返回栈顶元素search(o): 返回栈中指定元素的位置Queueoffer(e):向队列中插入一个元素poll(): 出队,空时返回nullremove():出队,空时出现异常peek(): 获取队首元素,为空时返回nullelement():获取队首元素,为空时出现异常Deque 双端队列LinkedList 实现了Deque接口,因此可以使用LinkedList创建一个双端队列addFirst()addLast()removeFirst()removeLast()getFirst()getLast()Map用于存储键值对形式的数据map接口提供的方法查询get(key):Vvalues():Collection<V>keySet():Set<K>entrySet():Set<Map.Entry<K,V>>containsKey(key):booleancontainsValue(value):booleanisEmpty()size()增加put(key,value)put(map)删除remove(key)clear()修改HashMapLinkedHashMap使用linked list实现来扩展HashMapTreeMap在遍历排好序的键值时高效有新的导航方法firstKey()lastKey()headMap(toKey)tailMap(fromKey)floorKey(key)ceillingKey(key)lowerKey(key)higherKey(key)附:map的遍历方法Set<Map.Entry<K,V>> set = map.entrySet();for(Map.Entry<K,V> m : set){key=m.getKey();value=m.getValue();}

往事是尘封在记忆中的梦,而你是我唯一鲜明的记忆,

java集合类整理

相关文章:

你感兴趣的文章:

标签云: