一:集合框架的概述
1,集合,数组都是对多个数据进行存储操作的结构,简称Java容器。
说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi)数据库中
2.1数组在存储多个数据方面的特点:
>一旦初始化以后,其长度就确定了。
>数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了。比如:String[]arr;int[] arr1;
2.2数组在储存多个数据方面的缺点:
>一旦初始化以后,其长度就不可修改。
>数组中提供的方法非常有限,对于添加,删除,插入数据等操作,非常不便,同时效率不高
>获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
>数组存储数据的特点:有序,可重复。对于无序,不可重复的需求,不能满足。
Java集合可分为Collection和Map两种体系
Collection接口:单列数据,定义了存取一组对象的方法和集合
>List:元素有序,不可重复的集合
>Set:元素无序,不可重复的集合
Map接口:双列数据,保存具有映射关系“key-value对”的集合
二:集合框架
|---Collection接口:单列集合,用来存储一个一个的对象
|---List接口:存储有序的,可重复的数据。--->“动态”数组,替换原有的数组
|---ArrayList,LinkedList,Vector
|---ArrayList:作为list接口的主要实现类,线程不安全的,效率高:底层使用Object[] elementDate存储
|---LinkedList:对于频繁的插入,删除操作,使用此类效率比ArrayList高:底层使用双向链表存储
|---Vector:作为List接口的古老实现类:线程安全的,效率低
|---Set接口:存储无序的,不可重复的数据。--->高中讲的“集合”
|---HashSet,LinkedHashSet,TreeSet
|---Map接口:双列集合,用来存储一对(key-value)一对的数据.--->高中函数:y=f(x),x对应key,y对应value
|---HashMap,LinkedHashMap,TreeMap,Hashtable,Properties
三:Collection接口重点方法的使用
Collection接口的重点方法分别有:
1:add(Object e):将元素e添加到集合中
2:size():获取集合中元素的个数
3:addAll():将一个集合的所有元素添加到另一个集合中
4:clear():清空集合中的元素
5:isEmpty():判断当前集合是否为空
6:contains(Object obj):判断当前集合是否包含元素obj
注:在用contains()方法进行判断前,要将调用obj所在的类中的equals()进行重写
7:containsAll(Collection coll1):判断coll1中的所有元素是否在当前集合中
8:remove(Object obj):从当前集合中移除obj元素
9:removeAll(Collection coll1):从当前元素中移除coll1中包含的所有元素
10:retainAll(Collection coll1):交集:取当前集合与目标集合coll1的交集,并将结果返回给当前集合
11:equals(Collection coll1):想要返回true,需要当前集合和目标集合coll1中元素都相同,且在Object类中需要重写equals()方法
12:toChar(Collection coll1):将集合转化为数组
拓展:将数组转换成集合:调用Arrays类中的静态方法:toList()
13:iterator():返回Iterator接口的实例,用于遍历集合元素
以下是关于上述13个方法的使用:
import org.junit.Test;
import java.util.*;
public class CollectionTest {
@Test
public void test1(){
Collection coll=new ArrayList();
//add(Object e);将元素e添加到集合coll中
coll.add("AA");
coll.add("BB");
coll.add(123);
coll.add(new Date());
//size();获取添加的元素的个数
System.out.println(coll.size());//4
//addAll():将coll1集合中的元素添加到当前的集合中
Collection coll1=new ArrayList();
coll1.add(456);
coll1.add("CC");
coll.addAll(coll1);
System.out.println(coll.size());//6
//clear():清空集合元素
coll.clear();
System.out.println(coll.isEmpty());
//isEmpty():判断当前集合是否为空
System.out.println(coll.isEmpty());
//contains(Object obj):判断当前集合中是否包含obj
//我们在判断时会调用obj对象所在类的equals()。
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
Person p =new Person("Jerry",20);
coll.add(p);
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));
System.out.println(coll.contains(p));
//containsAll(Collection coll2):判断形参coll2中的所有元素是否都存在于当前集合中
Collection coll2 = Arrays.asList(123,4567);
System.out.println(coll.containsAll(coll2));
//remove(object obj):从当前集合中一处obj元素。
coll.remove(123);
System.out.println(coll);
System.out.println(coll.remove(new Person("Jerry", 20)));
System.out.println(coll);
//removeAll(Collection coll1):差集:从当前集合中移除coll1中所有元素
Collection coll3=Arrays.asList(456,4567);
coll.removeAll(coll3);
System.out.println(coll);
}
@Test
public void test2(){
Collection coll=new ArrayList();
coll.add(123);
coll.add(456);
coll.add(456);
coll.add("CC");
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
// //retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
// Collection coll1=Arrays.asList(123,456,789);
// coll.retainAll(coll1);
// System.out.println(coll);
//equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同,且要重写equals
Collection coll1=new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(456);
coll1.add("CC");
coll1.add(new Person("Jerry",20));
coll1.add(new String("Tom"));
coll1.add(false);
System.out.println(coll.equals(coll1));
}
@Test
public void test4(){
Collection coll1=new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(456);
coll1.add("CC");
coll1.add(new Person("Jerry",20));
coll1.add(new String("Tom"));
coll1.add(false);
//hashCode():
System.out.println(coll1.hashCode());
//集合--->数组:toArray()
Object[] arr = coll1.toArray();
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
//拓展:数组--->集合:调用Arrays类的静态方法asList()
List<Object> list = Arrays.asList(new String[]{"AA","BB","CC"});
System.out.println(list);
List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
System.out.println(arr1);//数组整体为一个元素
List<Integer> arr2 = Arrays.asList(123, 456);
System.out.println(arr2);
List<Integer> arr3 = Arrays.asList(new Integer[]{123, 456});
System.out.println(arr3);
//iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
}
}
四:集合元素的遍历操作
遍历集合元素的方法有三种,分别为Iterator迭代器方法,增强for循环,普通for循环
1:Iterator迭代器方法
以下是Iterator迭代器方法实例:
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class IteratorTest {
@Test
public void test(){
Collection coll1=new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(456);
coll1.add("CC");
coll1.add(new Person("Jerry",20));
coll1.add(new String("Tom"));
coll1.add(false);
Iterator iterator=coll1.iterator;
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
2:增强for循环
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
public class ForTest {
@Test
public void test(){
Collection coll1=new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(456);
coll1.add("CC");
coll1.add(new Person("Jerry",20));
coll1.add(new String("Tom"));
coll1.add(false);
//for(集合中元素的类型 局部变量: 集合对象)
//内部任然调用迭代器
for(Object obj:coll1){
System.out.println(obj);
}
}
@Test
public void test2(){
int[] arr=new int[]{1,2,3,4,5,6};
for(int a:arr){
System.out.println(a);
}
}
}
可以看出,不论是集合还是数组,都可以用增强for循环来完成遍历
3:普通for循环
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
public class ForTest {
@Test
public void test(){
Collection coll1=new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(456);
coll1.add("CC");
coll1.add(new Person("Jerry",20));
coll1.add(new String("Tom"));
coll1.add(false);
for(int i=0;i<coll1.size();i++){
System.out.println(coll1(i));
}
}
}
五:源码分析
1:ArrayList的源码分析
jdk7的情况下
ArrayList list = new ArrayList();//底层创建了长度是10的Object[]数组,当数组容量不足时,则会扩容,默认情况下,扩容为原来数组的1.5倍,同时将原有数组中的数据复制到新的数组中
结论:建议开发中使用带参的构造器:ArrayList list = new ArrayList(int capacity)
jdk8中ArrayList的变化
ArrayList list = new ArrayList();//底层Object[] elementData初始化为{},并没有创建长度为10的数组
list.add(123);//第一次调用add()时,底层才创建了长度为10的数组,并将数据123添加到elementData[0]
后续的添加和扩容操作与jdk7无异
小结:jdk7中的ArrayList的对象的创建类似于单例的饿汉式,二jdk8中的ArrayList的对象的创建类似于单例的懒汉式,延迟了数组的创建,节省内存
2:LinkedList的源码分析
LinkedList list = new LinkedList();内部声明了Node类型的first和last属性,默认值为null
list add(123);//将123封装到Node中,创建Node对象。
其中Node定义为:体现了LinkedList的双向链表的说法
private static class Node<E>{
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev,E element , Node<E> next){
this.item=element;
this.next=next
this.prev=prev;
3:Vector的源码分析
jdk7和假大空8中通过Vector()构造器创建对象时,底层都创建了长度为10的数组,在扩容方面,默认扩容为原来数组长度的2倍。
补充:在前面,我忘记将Person类写出来,现在补上
public class Person {
private String name;
private int age;
public Person(){
}
public Person(String name,int age){
this.age=age;
this.name=name;
}
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{"+
"name='"+name+'\''+
",age="+age+
'}';
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
}