关于Java中的Collection了解

来源:互联网 发布:交大软件学院 编辑:程序博客网 时间:2024/05/17 22:43
Connection: 可以用“容器”的概念称呼它,其也是容器层次结构的跟接口,容器类中的基本类型包括List,Map,Set,Queue,通常容器会结合泛型一起使用。
容器分类图(不包括Queue的实现):

        List: 它以特定的元素保存一组数据;
                ArrayList: ArrayList是List 接口的大小可变数组(线性表)的实现,它长于随机访问元素,但是从List中间插入和移除相对来说会较慢;
                LinkedList: LinkedList是List 接口的链接列表实现,相反它在List中间的插入和删除会优于ArrayList,而随机访问相对较慢,其包含的操作也多余ArrayList。
                         LinkedList具有实现栈的所有功能的方法,因此可以直接将LinkedList作为栈使用。
                * 可以理解成一个是线性表一个是链表,插入删除当然不同,数据结构就不同。
                以下是源码:
                /*
                 * LinkedList插入方式
                */
                public void add(int index, E element) {
                       checkPositionIndex(index);     //判断插入元素的位置
                       if (index == size)
                               linkLast(element);      //插入在末尾
                       else
                              linkBefore(element, node(index));   //在空节点前插入一个元素element
               }
              /*
               * ArrayList插入方式
              */
             public void add(int index, E element) {
                       rangeCheckForAdd(index);       //判断插入元素的位置
                       ensureCapacityInternal(size + 1);  // Increments modCount!!
                       System.arraycopy(elementData, index, elementData, index + 1,
                                size - index);
                       elementData[index] = element;
                       size++;
              }

      Set: 所保存元素不能重复,正因如此,查找就成为了Set中最主要的操作,HashSet就很好的实现了这个功能,Set具有与Connection完全同样的接口。
                HashSet:  HashSet使用的是散列函数查找,
                        HashSet所维护的顺序与TreeSet和LinkedHashSet都不同,因为它们的实现具有不同的元素存储方式,TreeSet将元素存储在红-黑树(平衡二叉查找树)数据结构中,LinkedHashSet也使用了散列,不同的是其使用了链表来维护元素的插入顺序。
      
在0到29之间的10000个随机数被添加到Set中,使用TreeSet:
              
import java.util.*;
public class SetOfInteger {
 
 public static void main(String[] args) {
  Random rand = new Random(47);
  
  Set<Integer> intest = new TreeSet<Integer>();
  
  for(int i = 0;i < 10000 ; i++){
   intest.add(rand.nextInt(30));
   
  }
  System.out.println(intest);
 }
}
输出结果:
[0, 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]
使用HashSet输出结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 29, 28]
(PS: 我也不知道为什么会如此有序委屈


      Queue: 只允许在容器一端插入对象,从另一端移除对象,Queue(队列)是一个典型的先进先出(FIFO)容器,队列常被当做一种可靠的的将对象从程序的某个区域传输到另一个区域的工具,因此队列在并发编程中特别重要。
              LinkedList提供了方法以支持队列的行为,并且它实现了Queue接口,因此LinkedList可以作为Queue接口的一种实现,
    
public class QueueDemo {
 
 public static void printQ(Queue queue){
  
  while(queue.peek() != null){    //peek()获取但不移除此队列的头;如果此队列为空,则返回 null。
   System.out.print(queue.remove() + " "); //获取并移除此队列的头。
  }
 }
 public static void main(String[] args) {
  Queue<Integer> queue = new LinkedList<Integer>();
  Random rand = new Random(47);
  
  for(int i=0; i<10;i++){
   queue.offer(rand.nextInt(i+10));
  }
  printQ(queue);
 }
}
输出结果:
8 1 1 1 5 14 3 1 0 1
  
              PriporityQueue:PriporityQueue是一个基于优先级堆的无界优先级队列,其声明下一个弹出元素是最需要的元素,此时它们何时得到处理就和它们等待时间无关了。

       Map: 将键映射到值得对象,(Map<K,V> K:此映射所维护的键的类型,V:映射值得类型),一个映射不能包含重复的键;每个键最多只能映射到一个值,Map的映射值可以是其他容器,也可以是其他Map,例如:Map<Person,List<Pet>>。
               TreeMap:基于红黑树(Red-Black tree)的 NavigableMap实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的Comparator 进行排序,映射实现可明确保证其顺序
               HashMap:基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用null 值和null 键,映射实现则不保证顺序
                         声明一个构造方法 HashMap(Map<? extends K,? extends V> m) 构造一个映射关系与指定Map 相同的新HashMap
   


2 0
原创粉丝点击