一:集合框架的概述
1:集合、数组都是对多个数据进行存储操作的结构,简称java容器。
说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)
2:数组在存储多个数据方面的特点:
- 一旦初始化以后,其长度就确定了。
- 数组一旦定义好,其元素的类型也就确定了,我们也只能操作指定类型的数据了。
- 比如:String[] arr;int[] arr1;object[] arr2;
数组在存储多个数据方面的缺点:
- 一旦初始化以后,其长度就不可修改。
- 数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便,同时效率不高。
- 获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用。
- 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。
Collection接口继承树:

二:集合框架
1 2 3 4 5 6 7 8 9
| |----Collection接口:单列集合,用来存储一个一个的对象 |----List接口:存储有序的、可重复的数据。 -->“动态”数组 |----ArrayList、LinkedList、Vector
|----Set接口:存储无序的、不可重复的数据 -->高中讲的“集合” |----HashSet、LinkedHashSet、TreeSet
|----Map接口:双列集合,用来存储一对(key - value)一对的数据 -->高中函数:y = f(x) |----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
|
三:Collection接口中的方法的使用
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 30 31 32 33 34 35 36 37
| import java.util.ArrayList; import java.util.Collection; import java.util.Date;
public class CollectionTest {
@Test public void test1(){ Collection coll = new ArrayList(); coll.add("AA"); coll.add("BB"); coll.add(123); coll.add(new Date()); System.out.println(coll.size()); Collection coll1 = new ArrayList(); coll1.add(456); coll1.add("CC"); coll.addAll(coll1); System.out.println(coll.size()); System.out.println(coll); coll.clear(); System.out.println(coll.isEmpty()); }
}
|
四:集合和数组之间的转换
集合—>数组:toArray()
数组—>集合:调用Arrays类的静态方法:asList()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public void test4(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false);
System.out.println(coll.hashCode());
Object[] arr = coll.toArray(); for(int i = 0;i < arr.length;i++){ System.out.println(arr[i]); }
List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"}); System.out.println(list);
List arr1 = Arrays.asList(new int[]{123, 456}); System.out.println(arr1.size());
|
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| Collection接口中声明的方法的测试 结论: 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals(). public class CollectionTest {
@Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456);
coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false); boolean contains = coll.contains(123); System.out.println(contains); System.out.println(coll.contains(new String("Tom")));
System.out.println(coll.contains(new Person("Jerry",20)));
Collection coll1 = Arrays.asList(123,4567); System.out.println(coll.containsAll(coll1)); } @Test public void test2(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false); coll.remove(1234); System.out.println(coll); coll.remove(new Person("Jerry",20)); System.out.println(coll); Collection coll1 = Arrays.asList(123,456); coll.removeAll(coll1); System.out.println(coll);
} @Test public void test3(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false);
Collection coll1 = new ArrayList(); coll1.add(456); coll1.add(123); coll1.add(new Person("Jerry",20)); coll1.add(new String("Tom")); coll1.add(false); System.out.println(coll.equals(coll1)); } }
|
五:Iterator()
集合元素的遍历操作,使用迭代器Iterator接口
- 内部的方法:hasNext()和next()。
- 集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
- 内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()
- 如果还未调用next()或在上一次调用 next 方法之后已经调用了 remove 方法,再调用remove都会报IllegalStateException异常。
Iterator迭代器的执行原理

Demo
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| public class IteratorTest {
@Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false); Iterator iterator = coll.iterator();
while(iterator.hasNext()){ System.out.println(iterator.next()); } } @Test public void test2(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false);
while (coll.iterator().hasNext()){ System.out.println(coll.iterator().next()); }
} @Test public void test3(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false); Iterator iterator = coll.iterator(); while (iterator.hasNext()){
Object obj = iterator.next(); if("Tom".equals(obj)){ iterator.remove();
}
} iterator = coll.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } }
}
|
六:foreach
Demo:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import org.junit.Test;
import java.util.ArrayList; import java.util.Collection;
public class ForTest {
@Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false);
for(Object obj : coll){ System.out.println(obj); }
}
@Test public void test2(){ int[] arr = new int[]{1,2,3,4,5,6}; for(int i : arr){ System.out.println(i); } }
@Test public void test3(){
String[] arr = new String[]{"MM","MM","MM"};
for(String s : arr){ s = "GG"; } for(int i = 0;i < arr.length;i++){ System.out.println(arr[i]); }
}
}
|