一、Collection概述
1.由于数组的长度是固定的,为了使程序能够方便地存储和操作数目不固定的一组数据,JDK类库提供了Java集合。
2.与数组不同的是,集合中不能存放基本类型数据,而只能存放对象的引用。
3.数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素
二、Collection介绍
主要有List接口和Set接口继承了Collection接口
1).List接口
特点:1.有序 2.有索引 3.元素可重复
遍历方式:1.普通for循环 2.增强for 3.迭代器
2) Set接口
特点:1.无序 2.无索引 3.元素不可重复
遍历方式:1.增强for 2.迭代器
三、源码解析
int size();//集合大小
boolean isEmpty();//判断集合是否为空集合
boolean contains(Object o);//判断集合是否包含某个元素
Iterator<E> iterator();//提供了返回迭代器对象,使得集合可以调用迭代器的方法
Object[] toArray();//返回包含该集合中所有元素的数组
<T> T[] toArray(T[] a);//得到的数组元素类型为原集合中的数据类型
boolean add(E e);//添加元素
boolean remove(Object o);//移除元素
boolean containsAll(Collection<?> c);//判断某个集合是否包含在原集合中
boolean addAll(Collection<? extends E> c);//将某个集合添加进原集合中,拼接在原集合的末端
boolean removeAll(Collection<?> c);//移除原集合的某个子集合
boolean retainAll(Collection<?> c);//1.如果存在相同元素,原集合仅保留相同的元素。2.如果不存在相同元素,原集合会变为空。
boolean equals(Object o);//比较此collection集合与指定对象是否相等,是比较的是里面元素是否相等,而不是比较地址是否相等。
int hashCode();//获得hashCode值
2.测试代码
代码如下:
public class demoCollection {
public static void main(String[] args) {
Collection<String> strings=new ArrayList<>();
Collection<String> strings1=new ArrayList<>();
for(int i=0;i<10;i++){
String str=String.valueOf(i);
strings.add(str);
}
for(int i=0;i<5;i++){
String str=String.valueOf(i);
strings1.add(str);
}
//int size();集合的大小
System.out.println("int size()--"+strings.size());
//boolean isEmpty();判断集合是否为空集合
System.out.println("boolean isEmpty()--"+strings.isEmpty());
//boolean contains(Object o);判断集合是否包含某个元素
System.out.println("boolean contains(Object o)--"+strings.contains("1"));
//Iterator<E> iterator();提供了返回迭代器对象,使得集合可以调用迭代器的方法
Iterator iterator=strings.iterator();
while (iterator.hasNext()){
System.out.println("Iterator<E> iterator()--"+iterator.next());
}
//Object[] toArray();返回包含该集合中所有元素的数组
Object[] ints=strings.toArray();
for(Object s:ints){
System.out.println("Object[] toArray()--"+s);
}
//<T> T[] toArray(T[] a);//得到的数组元素类型为原集合中的数据类型
// String[] strings1= (String[]) strings.toArray();
String[] strs=strings.toArray(new String[strings.size()]);
for(String s:strs){
System.out.println("<T> T[] toArray(T[] a)--"+s);
}
//boolean containsAll(Collection<?> c);判断某个集合是否包含在原集合中
System.out.println("boolean containsAll(Collection<?> c)--"+strings.containsAll(strings1));
//boolean addAll(Collection<? extends E> c);//将某个集合添加进原集合中
// strings.addAll(strings1);
// System.out.println("boolean addAll(Collection<? extends E> c)--"+strings);
//boolean removeAll(Collection<?> c);移除原集合的某个子集合
// strings.removeAll(strings1);
// System.out.println("boolean removeAll(Collection<?> c);--"+strings);
//boolean retainAll(Collection<?> c);//1.如果存在相同元素,原集合仅保留相同的元素。2.如果不存在相同元素,原集合会变为空。
strings.retainAll(strings1);
System.out.println("boolean retainAll(Collection<?> c)--"+strings);
//int hashcode;得到hashCode值
System.out.println("int hashcode--"+strings.hashCode());
//boolean equals(Object o);比较此collection集合与指定对象是否相等,是比较的是里面元素是否相等,而不是比较地址是否相等。
System.out.println("boolean equals(Object o)--"+strings.equals(strings1));
System.out.println("==--"+strings==(strings1).toString());
}
}
总结
Collection接口为集合提供了基础的增删改查方法,提供了集合的一个骨干型接口,为后来的集合对其进行继承或实现其方法提供了相当大的便利,理解集合从理解Collection接口开始。