Collection接口
集合概述
集合和数组一样,都是对多个数据进行存储操作的结构。都是Java容器。但是,数组在使用时,会存在一些弊端。
我们使用数组存储数据时,数组的长度是固定。因此对于不确定长度的数据,我们很难使用数组存储。其次,数组提供的方法有限,对于要进行复杂操作的数据,很难实现需求。
由于数组的一些局限性,在存储数据时提供了集合这个容器。集合的作用便是解决数组存储数据的弊端。
Collection接口集合
Collection接口:单列集合,存放一个个对象。
Collection接口下有两个子接口List接口和Set接口。
在List接口下的实现类有ArrayList、 LinkedList、 Vector
在Set接口下的实现类有HashSet、LinkedHashSet、TreeSet
虽然我们在使用时,并不直接去使用Collection的实现类。但是常用的集合类都是实现了Collection接口的方法。
Collection接口的常用方法
collection的常用方法有:
-
boolean add(Object obj)/addALL(Object obj)
向集合中添加Object型元素/将集合obj中的元素添加到当前集合 -
int size();
获取集合中元素的个数 -
void clear();
清空集合中的所有元素 -
boolean contains(Object o)/containsAll(Collection col2)
判断集合中是否存在某个指定对象/ 判断col2的元素是否都在当前集合当中 集合与集合之间 -
boolean remove(Object o)/removeAll(Collection obj)
删除集合中的指定对象/差集,移除当前集合中的obj含有的元素 -
boolean isEmpty();
判断集合中的元素个数是否为0 -
Object[] toArray()
把集合中的元素转成数组返回 -
数组->集合Arrays.asList()形参为可变长参数,
Arrays.asList的作用是将数组转化为list -
Iterator iterator();
返回集合中的迭代器对象 -
col1.retainAll(Collection obj) 交集,移除当前集合和obj集合的交集
-
equals(object obj) 比较两个集合是否相等,包含顺序
-
hashCode();返回当前对象的哈希值
public static void test1(){
Collection col1 = new ArrayList();
//add()
col1.add("hah");
//size
System.out.println(col1.size());
//addALL(Object obj) 将集合obj中的元素添加到当前集合
Collection col2= Arrays.asList(123);
col1.add(col2);
System.out.println(col1);
//clear()/清空
col1.clear();
System.out.println(col1);
//isEmpty() 判断是否为空
System.out.println(col1.isEmpty());
}
public static void test(){
Collection col1 = new ArrayList();//该集合有序且支持重复 list(列表)
col1.add("haha");
col1.add("xixi");
col1.add("xixi");
col1.add(123);
col1.add('a');
col1.add(new String("aolianna"));
col1.add(new Person("WE"));//自定义类
//1、 contains(object obj); 判断当前集合中是否包含obj
//判断时会调用obj对象所在类的equals()
boolean ni=col1.contains(123);
System.out.println(ni);//true
System.out.println(col1.contains("haha"));//true
System.out.println(col1.contains(new String("aolianna")));//ture
System.out.println(col1.contains(new Person("WE")));//未重写equals方法 false
//在向接口Collection实现类的对象添加自定义对象时,要求自定义类重写equals方法
//2、containsAll(Collection col2) 判断col2的元素是否都在当前集合当中 集合与集合之间
Collection col2 = Arrays.asList(123,"haha");//aslist() return new ArrayList();
System.out.println(col1.containsAll(col2));//true
}
public static void test02(){
Collection col1 = new ArrayList();
col1.add("haha");
col1.add("xixi");
col1.add(123);
col1.add('a');
col1.add(new String("aolianna"));
col1.add(new Person("WE"));//自定义类
//3、remove(obj); 删除当前集合中的obj,先equals查询
System.out.println(col1.remove("haha"));
System.out.println(col1);//要求自定义类重写toString();
//4、removeAll(Collection obj) :差集,移除当前集合中的obj含有的元素
Collection col2=Arrays.asList(456,new Person("WE"));
System.out.println(col1.removeAll(col2));
System.out.println(col1);
}
public static void test03(){
Collection col1 = new ArrayList();
col1.add("haha");
col1.add("xixi");
col1.add("xixi");
col1.add(123);
col1.add('a');
col1.add(new String("aolianna"));
col1.add(new Person("WE"));//自定义类
//5、 col1.retainAll(Collection obj) 交集,移除当前集合和obj集合的交集
Collection col2=Arrays.asList(123,456,'a');
System.out.println(col1.removeAll(col2));
System.out.println(col1);
System.out.println("=====================");
//6、equals(object obj) 比较两个集合是否相等,包含顺序
Collection col3=Arrays.asList("haha");
System.out.println(col1.equals(col3));
}
需要注意的是在集合中添加自定义类对象时,在定义的类中要重写要求自定义类重写equals方法和toString()方法。
Collection集合与数组的转换方法
public static void test04(){
Collection col1 = new ArrayList();
col1.add("haha");
col1.add("xixi");
col1.add(123);
col1.add('a');
col1.add(new String("aolianna"));
col1.add(new Person("WE"));//自定义类
//hashCode();返回当前对象的哈希值
System.out.println(col1.hashCode());
System.out.println("===============");
//集合->数组 toArray()
Object[] array = col1.toArray();
System.out.println(Arrays.toString(array));
//数组->集合Arrays.asList()形参为可变长参数,Arrays.asList的作用是将数组转化为list
/*(1)该方法不适用于基本数据类型(byte,short,int,long,float,double,boolean),可以是integer。
(2)该方法将数组与列表链接起来,当更新其中之一时,另一个自动更新
(3) 不支持add和remove方法
*/
List<String> list = Arrays.asList(new String[]{"jak", "hah"});
List ints = Arrays.asList(new Integer[]{123, 456});
System.out.println(list);
System.out.println(ints);
//Iterator iterator(); 遍历集合元素
//返回集合中的迭代器对象
}
Iterator迭代器
在Java中提供了Iterator迭代器来实现对集合元素的遍历。
在创建了一个集合对象后,调用该对象的 iterator()方法生成一个该集合对象的迭代器。
该迭代器会生成一个指向该集合对象的指针。
在该接口中,常用的方法有
- hasNext() 判断是否还有下一个元素
- next() 将指向该集合的指针下移,返回指针指向的元素
- remove() 删除指针当前指向的元素
public static void test01(){
Collection col1 = new ArrayList();
col1.add("haha");
col1.add(123);
col1.add("xixi");
col1.add('a');
col1.add(new String("aolianna"));
col1.add(new Person("WE"));//自定义类
Iterator iterator= col1.iterator();
//next(); 指针下移,将下移
//方式一
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
System.out.println(iterator.next());
//方式二
for(int i=0;i< col1.size();i++){
System.out.println(iterator.next());
}
// 方式三
// hasNext() 判断是否还有下一个元素
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
public static void test02(){
Collection col1 = new ArrayList();
col1.add("haha");
col1.add(123);
col1.add("xixi");
col1.add('a');
col1.add(new String("aolianna"));
col1.add(new Person("WE"));//自定义类
//remove() 删除指针当前指向的元素
Iterator iterator= col1.iterator();
while(iterator.hasNext()){
Object obj=iterator.next();
if("aolianna".equals(obj)) {
iterator.remove();
}
}
Iterator iterator1= col1.iterator();//重新生成指针
while(iterator1.hasNext()){
System.out.println(iterator1.next());
}
}