Java集合框架及相关方法

1.Collection接口
(1)addAll

Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
Integer[] moreInts = {6,7,8,9,10};
collection.addAll(Arrays.asList(moreInts));

(2)public boolean add(E e):把给定的对象添加到当前集合中
(3)public void clear():清空集合中的元素
(4)public boolean remove(E e):把给定的对象在当前集合中删除
public boolean removeAll(Collection c):从集合中删除c集合中所有的元素
(5)public boolean contains(E e):判断当前集合中是否包含给定的对象
public boolean containsAll(Collection c):检查Collection集合中是否包含c的全部对象,全部包含则返回true
(6)public boolean isEmpty():判断当前集合是否为空
(7)public int size():返回集合中元素的个数
(8)pubic Object[] toArray():把集合中的元素,存储到数组中
(9)Object的equal方法:是用"=="实现,即比较地址是否一样

public boolean equals(Object obj){
    return (this == obj);
}

(10)public int hashCode():返回此Collection集合的哈希码值
(11)public void retainAll(Collection c):集合中仅保留c集合中的所有元素(两者的交集)
(12)public Object[] toArray():返回包含此collection集合中所有元素的数组

2.List接口
(1)public void add(int index,E e):将指定元素,添加到该集合中的指定位置上,后面的元素都往后移一个元素
public boolean addAll(int index,Collection c):在指定位置插入c集合全部元素,如果集合中没有元素,则返回false
(2)public E get(int index):返回集合中指定位置的元素
(3)public E remove(int index):移除列表中指定位置的元素,返回的是被移除的元素
(4)public E set(int index,E e):用指定元素替换集合中指定位置的元素,返回值为更新前的元素
(5)public int indexOf(Object[] o):返回该对象在List中所处位置的索引编号,若没有则返回-1
(6)List subList(int fromIndex,int toIndex):返回从索引fromIndex到toIndex的元素集合,包左不包右
3.LinkedList
(1)public void addFirst(E e):将指定元素插入此列表的开头
(2)public void addLast(E e):将指定元素添加到此列表的结尾
(3)public E getFirst():返回此列表的第一个元素
(4)public E getLast():返回此列表的最后一个元素
(5)public E removeFirst():移除并返回此列表的第一个元素
(6)public E removeLast():移除并返回此列表的最后一个元素
(7)public E pop(): 从此列表所表示的堆栈处弹出一个元素
(8)public void push(E e):将元素推入此列表所表示的堆栈
(9)public boolean isEmpty():如果列表不包含元素,则返回true

4.Stack
(1)public void push(num):入栈
(2)public E pop():栈顶元素出栈并返回值
(3)public boolean empty():判定栈是否为空
(4)public E peek():获取栈顶元素
(5)public int search(num):判断元素num是否在栈中,若在返回1,不在返回-1
5.Queue
这组方法,成功返回true,操作失败抛出异常
(1)public void add(E e):添加一个元素到队尾
(2)public E remove():获取队首元素,并从队列中移除
(3)public E element():获取队首的元素,但不从队列中移除
这组方法,成功返回true,失败时返回一个特殊值(NULL或false)
(4)public void offer(E e):添加一个元素到队尾
(5)public E poll():获取队首的元素,并从队列中移除
(6)public E peek():获取队首的元素,但不从队列中移除
6.PriorityQueue
(1)PriorityQueue priorityqueue = new PriorityQueue();自然顺序排列
(2)PriorityQueue priorityqueue = new PriorityQueue(10,Collections.reverseOrder());逆序排列
(3)按自己意愿排序:

PriorityQueue<PriorityObject> queue = new PriorityQueue<PriorityObject>(10,new Comparator<PriorityObject>(){
    @Override  
    public int compare(PriorityObject paramT1,  PriorityObject paramT2) {  
        return  paramT2.mPriority - paramT1.mPriority;  
    }
});  

7.Set集合继承自Collection接口,没有额外的方法
8.HashSet:重写equal和hashcode
9.LinkedHashSet:有顺序,重写equal和hashcode

10.Map接口
(1)public V put(K key,V value):把指定的键与指定的值添加到Map集合中
public void putAll(Map<K,V> m):向map集合中添加hiding集合中的所有元素
(2)public V remove(Object key):把指定的键所对应的键值对元素在Map集合中删除,返回被删除元素的值
(3)public V get(Object key):根据指定的键,在Map集合中获取对应的值
(4)public boolean containsKey(Object key):检查map集合中有没有包含Key为key的元素,如果有则返回true
(5)public boolean containsValue(Object value):检查map集合中有没有包含Value为value的元素,如果有则返回true
(6)public boolean equals(Object o):判断两个map集合的元素是否相同(底层重写了equals方法)
(7)public void clear():把map集合中所有键值删除
(8)public int size():返回map集合中元素个数
(9)public int hashCode():返回map集合的哈希码值
(10)public boolean isEmpty():检查map集合中是否有元素,如果没有则返回true

(11)public Set keySet():返回map集合中所有Key
(12)public Collection values():返回map集合中所有的Value到一个Collection集合
(13)遍历方式:public Set<Map.Entry<K,V>> entrySet():返回map到一个Set集合中,以map集合中的Key=Value的形式返回到set中
(14)对Entry对象获取键值:
public K getKey()
public V getValue()

HashMap<int,String> map = new HashMap<int,String>();
map.put(1,"a");map.put(2,"b");map.put(3,"c");
Set<Entry<int,String>> entrySet = map.entrySet();//获取所有entry对象
//遍历得到每一个entry对象
for(Entry<int,String> entry:entrySet){
    int key = entry.getKey();
    String value = entry.getValue();
    System.out.println(key+":"+value);
}

Map集合遍历的三种方式:
a.使用keySet():获取map集合中的所有键,再通过get方法获取键对应的值
b.通过values获取所有值,不能获取到key对象
c.获取Map.Entry对象集合
11.HashMap应用要复写equals和hashcode方法
12.LinkedHashMap有序

13.Iterator迭代器
(1)public Iterator iterator():获取集合对应的迭代器,用来遍历集合中的元素
(2)public E next():返回迭代的下一个元素
(3)public boolean hasNext():如果仍有元素可以迭代,则返回true
(4)public void remove():删除集合中Iterator指向位置后面的元素

public static void display(Iteration<Pet> it){
    while(it.hasNext()){
        Pet p = it.next();
        System.out.println(p);
    }   
}
display(pets.iterator());

有些地方使用Collection可以有相同的作用

public static void display(Collection<Pet> pets){
    for(Pet p : pets)
        System.out.println(p);
}

14.listIterator迭代器
(1)public void add(E e):将指定元素插入列表,插入位置为迭代器当前位置之前
(2)public boolean hasNext():以正向遍历列表时,如果列表迭代器后面还有元素,则返回true
(3)public boolean hasPrevious():以逆向遍历列表,列表迭代器前面还有元素,则返回true
(4)public E next():返回列表中listIterator指向位置后面的元素
(5)public int nextIndex():返回列表中listIterator所指位置后面元素的索引
(6)public E previous():返回列表中listIterator所指位置前面的元素
(7)public int previousIndex():返回列表中listIterator所指位置前面元素的索引
(8)public void remove():删除集合中Iterator指向位置后面的元素
(9)public void set(E e):从列表中将next()或previous()返回的最后一个元素更改为指定元素e
(10)public ListIterator listIterator(int index):从指定位置开始,返回此列表元素的列表迭代器

15.Collections:
java.utils.Collections集合工具类
(1)public static boolean addAll(Collection c,T… elements):往集合中添加一些元素

Collections.addAll(collection,11,12,13,14,15);
Collections.addAll(collection,moreInts);

(2)public static void shuffle(List<?> list):打乱集合顺序
(3)public static void sort(List list):将集合中元素按照默认规则排序
(4)public static void sort(List list,Comparator<? super T>):将集合中元素按照指定规则排序
(5)Comparable接口和Comparator接口的比较

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值