集合1、集合概述与Collection接口
集合
一、集合框架的概述
-
集合、数组都是对多个数据进行存储操作的结构,简称java容器
说明:此时的存储,主要是指内存层面的存储,不涉及到持久化的存储(.txt、.jpg、.avi、数据库等)
-
数组在存储多个数据方面的特点:
- 一旦初始化以后,其长度就确定了
- 数组一旦定义好以后,其元素的类型也就确定了,我们也就只能操作指定类型的数据了。比如:String[] arr; int [] arr1; Object[] arr2;
-
数组在存储多个数据方面的缺点:
- 一旦初始化以后,其长度就不可修改
- 数组中提供的方法非常有限,对于添加、删除、插入数据等操作非常不便,同时效率不高
- 获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
- 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足
二、集合框架
Collection接口:单列结合,用来存储一个一个的对象
List接口:存储有序的、可重复的数据。--->“动态数组”
实现类:ArrayList类、LinkedList类、Vector类
Set接口:存储无序的、不可重复的数据。--->高中数学的“集合”
实现类:HashSet类、LinkedHashSet类、TreeSet类
Map接口:双列集合,用来存储一对(key-value)一对的数据(键值对)
实现类:HashMap类、LinkedHashMap类、TreeMap类、HashTable类、Properties类
三、Collection接口中方法的使用
add(Object o)
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(Collection c)
//addAll(Collection c) 将 c 集合中的元素添加到当前的集合中
Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add("CC");
coll.addAll(coll1);
System.out.println(coll1.size()); //6
clear() 和 isEmpty()
//clear()
coll.clear();
//isEmpty() 判断当前集合是否为空
System.out.println(coll.isEmpty()); //true
contains()
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
coll.add(456);
coll.add(new Person("Jerry", 20));
//contains(Object obj) 判断当前集合中是否包含obj
/*
我们在判断时会调用 obj 对象所在类的 equals()方法
*/
boolean contains = coll.contains(123);
System.out.println(contains); //true
System.out.println(coll.contains(new String("Tom"))); //true
System.out.println(coll.contains(new Person("Jerry", 20))); //true
containsAll(Collection c)
//containsAll(Collection c) 判断 c 中所有元素是否都存在于当前集合中
Collection coll1 = Arrays.asList(123,3456);
System.out.println(coll.containsAll(coll1)); //true
remove(Object obj)
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
coll.add(456);
coll.add(new Person("Jerry", 20));
//remove(Object obj) 从当前集合中移除元素 obj
coll.remove(123);
System.out.println(coll); //[456, Tom, false, 456, Person{name='Jerry', age=20}]
coll.remove(new Person("Jerry", 20));
System.out.println(coll); //[456, Tom, false, 456]
removeAll(Collection c)
Collection coll1 = Arrays.asList(456);
//removeAll(Collection c) 从当前集合中移除 c 中的所有元素
System.out.println(coll); //[456, Tom, false, 456]
coll.removeAll(coll1);
System.out.println(coll); //[Tom, false]
retainAll(Collection c)
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);
coll.add(new Person("Jerry", 20));
Collection coll1 = Arrays.asList(123, 456, 798);
//retainAll(Collection c) 获取当前集合和 c 集合的交集,并返回给当前集合
coll.retainAll(coll1);
System.out.println(coll); //[123, 456]
equals(Object obj)
Collection coll1 = new ArrayList();coll1.add(123);coll1.add(456);coll1.add(new String("Tom"));coll1.add(false);coll1.add(new Person("Jerry", 20));Collection coll2 = new ArrayList();coll2.add(123);coll2.add(456);coll2.add(new String("Tom"));coll2.add(false);coll2.add(new Person("Jerry", 20));////equals(Object obj) 判断当前集合元素和 obj 集合元素是否相等 PS. new的是ArratList(),是有序的,所以还需要比较顺序System.out.println(coll1.equals(coll2)); //true
hashCode()
//hashCode() 返回当前对象的哈希值System.out.println(coll1.hashCode());
toArray()
//toArray() 集合---->数组Object[] obj = coll1.toArray();for (int i = 0; i < obj.length; i++) { System.out.println(obj[i]);}
/*拓展:数组--->集合? */List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});System.out.println(list);
Iterator接口的使用(集合元素的遍历)
详见 Iterator 文件

浙公网安备 33010602011771号