首页 > 技术文章 > Java基础--Collection集合

WhiperHong 2019-05-05 21:52 原文

1.Collection接口

    Collection是java集合框架(collection-frame)中的顶层接口。Collection接口表示一个容器,容器中只能存储引用数据类型,建议存同一类型的引用类型,方便后续遍历等操作。

    容器中的元素可以是有序的、可重复的,称为List接口;也可能是无序的、唯一的,称为Set接口。

    Collection接口的常用方法:

 

 1 public static void main(String[] args) {
 2         
 3         /**
 4          * 增:add/addAll
 5          * 删:clear/remove/removeAll/retainAll
 6          * 改:
 7          * 查:contains/containsAll/isEmpty/size
 8          */
 9         
10         Collection c1 = new ArrayList();
11         
12         // 追加
13         c1.add("apple"); // Object object = new String("apple");
14         // c1.add(1);         // Object object = new Integer(1); 
15         c1.add("banana");
16         System.out.println(c1);
17         
18         // 追加一个集合 
19         Collection c2 = new ArrayList();
20         c2.add("java");
21         c2.add("c++");
22         c1.addAll(c2);
23         System.out.println(c1);
24         
25         // clear
26         //c1.clear();
27         
28         // c1.remove("apple");
29         // c1.removeAll(c2);
30         //c1.retainAll(c2);
31         //System.out.println(c1);
32         
33         System.out.println(c1.contains("apple"));
34         c2.add("js");
35         System.out.println(c1.containsAll(c2));
36         // c1.clear();
37         System.out.println(c1.isEmpty());
38         // 返回集合元素的个数
39         System.out.println(c1.size());
40         
41         System.out.println(c1.equals(c2));
42         
43     }

    Collection接口的快速遍历:

1 // 快速遍历
2         // for-each
3         // Object 表示元素类型 
4         // item表示迭代变量
5         // c1表示集合
6         for (Object item : c1) {
7             System.out.println(item.toString());
8         }

    快速遍历的本质:

    Collection继承Iterable接口,表示集合支持快速遍历。Iterable接口定义了一个方法iterator()用于获取集合的迭代器,是一个Iterator接口类型,iterator()内部返回一个实现类实现类Iterator接口。这个实现类一定具有hasNext和next方法用于判断是否有下一个元素和获取下一个元素。快速遍历就是基于迭代器工作的。

 1 public static void main(String[] args) {
 2         
 3 
 4         Collection c1 = new ArrayList();
 5         c1.add("apple");
 6         c1.add("banana");
 7         c1.add("coco");
 8         
 9         
10         // 快速遍历
11         // for-each
12         // Object 表示元素类型 
13         // item表示迭代变量
14         // c1表示集合
15         for (Object item : c1) {
16             System.out.println(item.toString());
17         }
18         
19         // 迭代器遍历(国内)
20         Iterator it = c1.iterator();
21         while(it.hasNext()) {
22             Object item = it.next();
23             System.out.println(item.toString());
24         }
25         
26         // 国外
27         for(Iterator it2=c1.iterator();it2.hasNext();) {
28             Object item = it2.next();
29             System.out.println(item.toString());
30         }    
31     }

2.List接口

List 接口中的元素时有序的、可重复的。List接口中的元素通过索引(index)来确定元素的顺序。

有序的 collection(也称为序列)。可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素

List接口的常用方法:

 1 public static void main(String[] args) {
 2         
 3         /**
 4          * 增:add/addAll/add(index,el)/addAll(index,collection)
 5          * 删:clear/remove/removeAll/remove(index)
 6          * 改:set(index,el)
 7          * 查:get(index)/indexOf/lastIndexOf()
 8          * 其他:contains/containsAll/isEmpty/size
 9          */
10         List list1 = new ArrayList();
11         // 添加元素
12         list1.add("apple");
13         list1.add("banana");
14         // 在指定位置添加元素
15         list1.add(0, "coco");
16         
17         System.out.println(list1);
18         
19         List list2 = new ArrayList();
20         list2.add("java");
21         list2.add("c++");
22         
23         list1.addAll(1, list2);
24         System.out.println(list1);
25         
26         // 删除
27         list1.remove(0);
28         System.out.println(list1);
29         
30         // 修改
31         list1.set(0, "javax");
32         System.out.println(list1);
33         
34         //
35         System.out.println(list1.get(0));
36         list1.add("apple");
37         list1.add("apple");
38         System.out.println(list1);
39         System.out.println(list1.indexOf("apple"));
40         System.out.println(list1.lastIndexOf("apple"));
41     }

List接口的遍历:

ListIterator 继承于Iterator,在Iterator的基础上提供了以正向遍历集合,也可以以逆序遍历集合。

hasNext/next 以正向遍历

hasPrevious/previous 以逆序遍历

 1 public static void main(String[] args) {
 2         
 3         
 4         List list1 = new ArrayList();
 5         list1.add("apple");
 6         list1.add("banana");
 7         list1.add("coco");
 8         
 9         // 【1】快速遍历
10         System.out.println("--for each--");
11         for (Object item : list1) {
12             System.out.println(item.toString());
13         }
14         
15         // 【2】普通for
16         System.out.println("--for--");
17         for(int i=0;i<list1.size();i++) {
18             System.out.println(list1.get(i));
19         }
20         
21         // 【3】集合迭代器
22         System.out.println("--iterator--");
23         Iterator it = list1.iterator();
24         while(it.hasNext()) {
25             System.out.println(it.next());
26         }
27         
28         System.out.println("--list iterator--");
29         // 正向遍历
30         ListIterator it2 = list1.listIterator();
31         while(it2.hasNext()) {
32             System.out.println(it2.next());
33         }
34         
35         // 逆序遍历
36         while(it2.hasPrevious()) {
37             System.out.println(it2.previous());
38         }
39         
40         System.out.println("--list iterator with index--");
41         ListIterator it3 = list1.listIterator(1);
42         while(it3.hasNext()) {
43             System.out.println(it3.next());
44         }
45     }

3.LinkedList接口

LinkedList是List接口的实现类,底层数据结构是链表。

LinekList常用方法和遍历方法参照List接口。

LinkedList 线程不安全。

除了实现List接口, 还实现栈接口。

push入栈操作 / pop出栈操作

 1 public class Test01 {
 2     public static void main(String[] args) {
 3         LinkedList list = new LinkedList();
 4         list.push("apple");
 5         list.push("banana");
 6         list.push("coco");
 7         
 8         
 9         System.out.println(list.pop());
10         System.out.println(list.pop());
11         System.out.println(list.pop());
12         
13         // java.util.NoSuchElementException
14         System.out.println(list.pop());
15     }
16 }

队列(Queue)接口

add/remove/element() 可能会出现NoSuchElementException异常

 1 public static void main(String[] args) {
 2         
 3         LinkedList queue = new LinkedList();
 4         // 入队
 5         /**
 6          * 队列头                          队列尾
 7          *<-----          <-----
 8          * [apple, banana, coco]
 9          */
10         queue.add("apple");
11         queue.add("banana");
12         queue.add("coco");
13         System.out.println(queue);
14         
15         // 出队
16         System.out.println(queue.remove());
17         System.out.println(queue.remove());
18         System.out.println(queue.remove());        
19         System.out.println(queue);
20         
21         // java.util.NoSuchElementException
22         System.out.println(queue.remove());
23         
24         
25         // 获取表头元素
26         System.out.println(queue.element());
27     }

offer/poll/peek 可能会返回特殊值(null)

 1 public static void main(String[] args) {
 2         
 3         LinkedList queue = new LinkedList();
 4         // 入队
 5         /**
 6          * 队列头                          队列尾
 7          *<-----          <-----
 8          * [apple, banana, coco]
 9          */
10         queue.offer("apple");
11         queue.offer("banana");
12         queue.offer("coco");
13         
14         // 出队列
15         //System.out.println(queue.poll());
16         //System.out.println(queue.poll());
17         //System.out.println(queue.poll());
18         System.out.println(queue);
19 
20         //System.out.println(queue.poll());
21         
22         // 获取表头元素
23         System.out.println(queue.peek());
24     
25     }

双向队列(Deque)接口

 1 /**
 2  * 以双向队列形式操作LinkedList
 3  */
 4 public class Test04 {
 5     public static void main(String[] args) {
 6         
 7         LinkedList queue = new LinkedList();
 8         // 入队
 9         /**
10          *<-----          <-----
11          * [apple, banana, coco]
12          * ---->          ----->
13          */
14         
15         queue.addFirst("apple");
16         queue.addFirst("banana");
17         queue.addFirst("coco");
18         System.out.println(queue);
19         
20         System.out.println(queue.removeLast());
21         System.out.println(queue.removeFirst());
22         System.out.println(queue.removeFirst());
23         System.out.println(queue);
24         
25         // 获取头元素
26         System.out.println(queue.getFirst());
27     
28     }
29 }

 

推荐阅读