Collection 集合框架
1. Collection 集合框架:在实际开发中,传统的容器(数组)在进行增、删等操作算法和具体业务耦合在一起,会增加程序的开发难度;这时JDK提供了这样的容器---Collection 集合框架,集合框架中 ,将使用的对象储存于特定数据结构的容器中 ,包含了一系列数据不同数据结构(线性表、查找表)的实现类。
1 import java.util.ArrayList; 2 import java.util.Collection; 3 4 public class Test1 { 5 public static void main(String[] args) { 6 /** 7 * 增:add/addAll; 8 * 删:clear/removeAll/retainAll; 9 * 改 10 * 查:contains/containsAll/isEmpty/size; 11 */ 12 13 Collection c1 = new ArrayList(); 14 //追加 15 c1.add("apple");//多态,父类引用子类:Object object = new String("apple"); 16 c1.add("banana");//多态,父类引用子类:Object object = new String("banana"); 17 System.out.println(c1);//输出[apple, banana] 18 } 19 20 }
1)Collection 部分方法
1 import java.util.ArrayList; 2 import java.util.Collection; 3 import java.util.Iterator; 4 5 public class Test1 { 6 public static <E> void main(String[] args) { 7 8 Collection<E> c1 = new ArrayList<E>(); 9 c1.add((E) "apple"); 10 c1.add((E) "apple2"); 11 c1.add((E) "apple3"); 12 c1.add((E) "apple4"); 13 System.out.println(c1.toString()); 14 15 //快速遍历 16 for (Object item : c1) { 17 System.out.println(item.toString()); 18 } 19 20 //另一写法 21 Iterator<E> it = c1.iterator(); 22 while (it.hasNext()) { 23 Object item = it.next(); 24 System.out.println(item.toString()); 25 } 26 27 //优化写法,for循环结束后,少占用JVM内存 28 for (Iterator<E> it2 = c1.iterator(); it2.hasNext();) { 29 Object item = it2.next(); 30 System.out.println(item.toString()); 31 } 32 33 34 Collection<E> c2 = new ArrayList<E>(); 35 c2.add((E) "java"); 36 c2.add((E) "C++"); 37 c1.addAll(c2); 38 39 40 //clear 41 //c1.clear();//移除此 collection 中的C1所有元素 42 //c1.remove("apple");//移除此 collection 中的C1的"apple"元素 43 //System.out.println(c1.toString()); 44 45 System.out.println(c1.contains("apple"));//返回true , c1中含有指定元素"apple"。 46 System.out.println(c1.contains(c2));//返回false,c1中含有c2的多个元素,不是指定的一个元素。 47 System.out.println(c1.containsAll(c2));//返回true,c1中包含指定 collection 中的C2所有元素 48 //c1.clear();//移除此 collection 中的C1所有元素,一下语句就返回true; 49 System.out.println(c1.isEmpty());//返回false,如果此collection中c1不含元素就返回true; 50 51 System.out.println(c1.size());//返回7; //返回集合collection 中的元素的数量 52 53 System.out.println(c1.equals(c2));//返回false; //判断指定对象是否相等 54 55 } 56 57 }
1.2 List接口是Collection的子接口,用于定义线性表数据结构,元素可重复、有序的;可以将List理解为存放对象的数组,只不过其元素个数可以动态的增加或减少。
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 import java.util.List; 4 import java.util.ListIterator; 5 6 public class Test2 { 7 public static <E> void main(String[] args) { 8 List<E> list1 = new ArrayList<E>(); 9 list1.add((E) "apple");// 添加元素 10 list1.add((E) "banana");// 添加元素 11 list1.add((E) "orange");// 添加元素 12 list1.add((E) "lemon");// 添加元素 13 14 for (E item : list1) { 15 System.out.print(item.toString() + " "); 16 }// 快速遍历 17 18 System.out.println();// 换行 19 20 list1.add(0, (E) "fruit");// 在指定位置添加元素 21 22 System.out.println();// 换行 23 24 // 快速遍历(优化写法) 25 for (Iterator<E> it = list1.iterator(); it.hasNext();) { 26 E item = it.next(); 27 System.out.print(item + " "); 28 } 29 30 System.out.println();// 换行 31 32 List<E> list2 = new ArrayList<E>(); 33 list2.add((E) "cabbage"); 34 list2.add((E) "spinach"); 35 36 System.out.println(list2.toString());// 输出[cabbage, spinach] 37 System.out.println();// 换行 38 39 list1.addAll(list2); 40 System.out.println(list1.toString());// 输出[fruit, apple, banana, orange, 41 // lemon, cabbage, spinach] 42 System.out.println();// 换行 43 44 list1.remove(0);// 删除指定位置元素 45 System.out.println(list1);// 输出[apple, banana, orange, lemon, cabbage, 46 // spinach] 47 list1.remove("spinach");// 删除指定元素 48 System.out.println(list1);// 输出[apple, banana, orange, lemon, cabbage] 49 50 list1.set(0, (E) "python");// 修改:把集合的第0位上的apple修改为python; 51 System.out.println(list1);// 输出[python, banana, orange, lemon, cabbage] 52 53 System.out.println(list1.get(0));// 输出python; 查询集合里第0位上的元素 54 list1.add((E) "c++"); 55 list1.add((E) "c++"); 56 list1.add((E) "c++"); 57 System.out.println(list1); 58 System.out.println(list1.indexOf("c++"));// 返回此列表中第一次出现的指定元素的索引 59 System.out.println(list1.lastIndexOf("c++"));// 返回此列表中最后出现的指定元素的索引 60 61 // 正向遍历 hasNext / next 62 ListIterator<E> it2 = list1.listIterator(); 63 while (it2.hasNext()) { 64 System.out.print(it2.next() + " "); 65 66 } 67 68 // 逆向遍历 hasPrecious / previous 69 System.out.println(); 70 while (it2.hasPrevious()) { 71 System.out.print(it2.previous() + " "); 72 } 73 74 // 正向遍历优化写法 75 System.out.println(); 76 for (ListIterator<E> it3 = list1.listIterator(); it3.hasNext();) { 77 E item = it3.next(); 78 System.out.print(item + " "); 79 } 80 81 } 82 83 }
1.3ArrayList/Vector
1)ArryList是List接口的实现类,底层数据结构是数组,并且实现可变的数组。 线程不安全的
2)Vector 是List接口的实现类,底层数据结构是数组,也是实现可变的数组。 线程安全的
① ArryList
1 import java.util.ArrayList; 2 import java.util.Iterator; 3 4 public class Test3 { 5 public static <E> void main(String[] args) { 6 // ArryList是List接口的实现类,底层数据结构是数组,并且实现可变的数组。 线程不安全的 7 // Vector 是List接口的实现类,底层数据结构是数组,也是实现可变的数组。 线程安全的 8 9 ArrayList<E> list = new ArrayList<E>(); 10 list.add((E) "cabbage"); 11 list.add((E) "spinage"); 12 list.add((E) "turnip"); 13 list.add((E) "pumpkin"); 14 15 System.out.println(list.toString()); 16 //System.out.println(list.size()); 输出集合元素的个数 17 18 System.out.println(); 19 20 // 快速遍历 21 for (Object item : list) { 22 System.err.print(item.toString() + " "); 23 } 24 25 System.out.println(); 26 27 // 迭代器遍历 28 for (Iterator<E> it = list.iterator(); it.hasNext();) { 29 E item = it.next(); 30 System.out.print(item.toString() + " "); 31 } 32 33 } 34 35 }
② Vector
1 import java.util.Iterator; 2 import java.util.Vector; 3 4 public class Test4 { 5 public static <E> void main(String[] args) { 6 Vector<E> list = new Vector<E>(); 7 list.add((E) "lemon"); 8 list.add((E) "orange"); 9 list.add((E) "pomegranate"); 10 11 System.out.println(list.toString()); 12 13 //快速遍历 14 for (Object item : list) { 15 System.out.print(item.toString() + " "); 16 } 17 18 19 System.out.println(); 20 //迭代器遍历 21 for (Iterator<E> it = list.iterator(); it.hasNext();) { 22 E item = it.next(); 23 System.out.print(item.toString() + " "); 24 } 25 26 } 27 28 }
1.3 LinkedList
1) LinkedList是List接口的实现类,底层数据结构是链表。
2) LinkedList常用方法和遍历方法参照List接口。(线程不安全)
堆栈操作
1 import java.util.LinkedList; 2 3 public class Test1 { 4 public static <E> void main(String[] args) { 5 // 堆栈操作LinkedList 6 7 LinkedList<E> list = new LinkedList<E>(); 8 list.push((E) "lemon"); 9 list.push((E) "orange"); 10 list.push((E) "pomegranate"); 11 12 System.out.print(list.pop() + " "); 13 System.out.print(list.pop() + " "); 14 System.out.print(list.pop() + " "); 15 16 //System.out.print(list.pop() + " ");//出错 java.util.NoSuchElementException 17 } 18 19 }
队列形式操作
1 import java.util.LinkedList; 2 3 public class Test2 { 4 public static <E> void main(String[] args) { 5 //LinkedList 队列形式操作 6 LinkedList<E> queue = new LinkedList<E>(); 7 //入队 8 queue.add((E) "lemon"); 9 queue.add((E) "orange"); 10 queue.add((E) "pomegranate"); 11 System.out.println(queue); 12 //System.out.println(queue.element());//获取头元素 13 14 //出对 15 //System.out.println(queue.remove(2));//指定位置出队 16 //System.out.println(queue.remove(1)); 17 //System.out.println(queue.remove(0)); 18 19 System.out.println(queue.remove()); 20 System.out.println(queue.remove()); 21 System.out.println(queue.remove()); 22 23 //System.out.println(queue.remove());//出错java.util.NoSuchElementException 24 25 queue.add((E) "lemon4"); 26 27 System.out.println(); 28 29 //System.out.println(queue.remove(0)); 30 31 System.out.println(queue.element());//获取头元素 , 输出lemon4 此时lemon4为 表头元素 32 } 33 34 }
1.4 Iterator和ListIterator
1)Iterator集合迭代器遍历集合过程中,不能再向集合汇总添加元素,否则出现 ConcurrentModificationException 并发修改异常。
1 import java.awt.event.ItemEvent; 2 import java.util.ArrayList; 3 import java.util.Iterator; 4 5 public class Test3 { 6 //Iterator和Listlterator 7 //Iterator在迭代过程中不予许向集合中添加元素 8 public static <E> void main(String[] args) { 9 ArrayList<E> list = new ArrayList<E>(); 10 list.add((E) "lemon"); 11 list.add((E) "orange"); 12 list.add((E) "peach"); 13 14 for (Iterator<E> it = list.iterator(); it.hasNext(); ) { 15 String item = (String)it.next(); 16 if(item.equals("lemon")){ 17 list.add((E) "test");//遍历过程中添加元素,会出现ConcurrentModificationException 并发修改异常。 18 } 19 System.out.println(list.toString()); 20 21 } 22 23 } 24 }
2)ListIterator允许程序员按任一方向遍历列表、迭代期间修改列表,并获得迭代器在列表中的当前位置。
1 import java.util.ArrayList; 2 import java.util.ListIterator; 3 4 public class Test4 { 5 public static <E> void main(String[] args) { 6 //ListIterator允许程序员按任一方向遍历列表、迭代期间修改列表,并获得迭代器在列表中的位置。 7 ArrayList<E> list = new ArrayList<E>(); 8 list.add((E) "lemon"); 9 list.add((E) "orang"); 10 list.add((E) "peach"); 11 12 //迭代遍历 13 for (ListIterator<E> it = list.listIterator(); it.hasNext();) { 14 String item = (String)it.next(); 15 if (item.equals("lemon")) { 16 it.add((E) "test"); 17 } 18 } 19 20 System.out.println(list.toString()); 21 //输出[lemon, test, orang, peach],ListIterator迭代期间可以修改列表 22 23 } 24 25 }
重点:Iterator实现类的源码hasNext/next
1.5 泛型
1)泛型是JDK1.5引入的新特性,泛型的本质是参数化类型。在类、接口、方法的定义过程中,所操作的数据类型为传入的指定参数类型。所有的集合类型都带有泛型参数,这样在创建集合时可以指定放入集合中的对象类型。同时,编译器会以此类型进行检查。
2)ArrayList支持泛型,泛型尖括号里的符号可随便些,但通常大写E。
3)迭代器也支持泛型,但是迭代器使用的泛型应该和它所迭代的集合的泛型类型一致!
4)泛型只支持引用类型,不支持基本类型,但可以使用对应的包装类。
5)如果泛型不指定类型的话,默认为Object类型。
1 import java.awt.Point; 2 import java.util.ArrayList; 3 import java.util.Iterator; 4 5 public class Test5 { 6 public static void main(String[] args) { 7 ArrayList<Point> list = new ArrayList<Point>(); 8 list.add(new Point(1, 2)); 9 list.add(new Point(3, 4)); 10 11 System.out.println(list); 12 13 for (int i = 0; i < list.size(); i++) { 14 Point p = list.get(i); //只运行Point类型,否则造型异常 15 System.out.println(p.getX());//输出两个点X的坐标 16 } 17 18 //遍历 19 for (Iterator<Point> it = list.iterator(); it.hasNext();) { 20 Point p = it.next(); 21 System.out.println(p); 22 } 23 } 24 25 }
6)泛型方法可以定义多个泛型类型
1 // 可以定义多个泛型的类型 2 public <A,B> void showInfo(A a,B b) { 3 System.out.println(a); 4 System.out.println(b); 5 }