一、集合与数组
1. 集合与数组存储数据概述:
集合、数组都是对多个数据进行存储操作的结构,简称Java容器。 说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)
2. 数组存储的特点:
一旦初始化以后,其长度就确定了。 数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了。
比如:String[] arr、int[] arr1、Object[] arr2
3. 数组存储的弊端:
- 一旦初始化以后,其长度就不可修改。
- 数组中提供的方法非常限,对于添加、删除、插入数据等操作,非常不便,同时效率不高。
- 获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
- 数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。
4. 集合存储的优点:
解决数组存储数据方面的弊端。
5. 集合的分类
Java集合可分为Collection和Map两种体系
-
Collection接口:单列数据,定义了存取一组对象的方法的集合
- List:元素有序、可重复的集合
- Set:元素无序、不可重复的集
-
Map接口:双列数据,保存具有映射关系“key-value对”的集合
6. 集合的框架结构
二,collection接口
-
collection接口时 set list queque接口的父接口与,该接口可以用于操作set集合 也可以用于操作list和queque集合
-
- JDK不提供此接口的任何直接实现,而是提供更具体的子接口(如:Set和List)实现。
-
在JDK 5.0之前,Java集合会丢失容器中所有对象的数据类型,把所有对象都当成 Object类型处理;从JDK 5.0增加了泛型以后,Java集合可以记住容器中对象的数据类型。
1. 单列集合框架结构
|----List接口:存储有序的、可重复的数据。 -->“动态”数组
|----ArrayList:作为List接口的主要实现类,线程不安全的,效率高;底层采用Object[] elementData数组存储
|----LinkedList:对于频繁的插入删除操作,使用此类效率比ArrayList效率高底层采用双向链表存储
|----Vector:作为List的古老实现类,线程安全的,效率低;底层采用Object[]数组存储
|----Set接口:存储无序的、不可重复的数据 -->数学概念上的“集合”
|----HashSet:作为Set接口主要实现类;线程不安全;可以存null值
|----LinkedHashSet:作为HashSet的子类;遍历其内部数据时,可以按照添加顺序遍历;对于频繁的遍历操作,LinkedHashSet效率高于HashSet.
|----TreeSet:可以按照添加对象的指定属性,进行排序。
-
添加
add(Object obj)addAll(Collection coll)
-
获取有效元素个数
int size()
-
清空集合
void clear()
-
是否为空集合
boolean isEmpty()
-
是否包含某个元素
boolean contains(Object obj):是通过元素的equals方法来判断是否是同一个对象boolean containsAll(Collection c):也是调用元素的equals方法来比较的。用两个两个集合的元素逐一比较
-
删除
boolean remove(Object obj):通过元素的equals方法判断是否是要删除的那个元素。只会删除找到的第一个元素boolean removeAll(Collection coll):取当前集合的差集
-
取两个集合的交集
boolean retainAll(Collection c):把交集的结果存在当前的集合中,不影响c
-
集合是否相等
boolean equals(Object obj)
-
转换成对象数组
Object [] toArray()
-
获取集合对象的哈希值
- `hashCode()`
复制代码
- 遍历
- `iterator()`:返回迭代器对象,用于集合遍历
@Test
public void test1() {
Collection collection = new ArrayList();
//1.add(Object e):将元素添加到集合中
collection.add("ZZ");
collection.add("AA");
collection.add("BB");
collection.add(123);
collection.add(new Date());
//2.size():获取添加元素的个数
System.out.println(collection.size());//5
//3.addAll(Collection coll1):将coll1集合中的元素添加到当前集合中
Collection collection1 = new ArrayList();
collection1.add("CC");
collection1.add(213);
collection.addAll(collection1);
System.out.println(collection.size());//9
//调用collection1中的toString()方法输出
System.out.println(collection);//[ZZ, AA, BB, 123, Tue Apr 28 09:22:34 CST 2020, 213, 213]
//4.clear():清空集合元素
collection1.clear();
System.out.println(collection1.size());//0
System.out.println(collection1);//[]
//5.isEmpty():判断当前集合是否为空
System.out.println(collection1.isEmpty());//true
}
@Test
public void test2() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Tom", 23));
coll.add(new Person("Jarry", 34));
coll.add(false);
//6.contains(Object obj):判断当前集合中是否包含obj
//判断时需要调用obj对象所在类的equals()方法
System.out.println(coll.contains(123));//true
System.out.println(coll.contains(new Person("Tom", 23)));//true
System.out.println(coll.contains(new Person("Jarry", 23)));//false
//7.containsAll(Collection coll1):判断形参coll1中的元素是否都存在当前集合中
Collection coll1 = Arrays.asList(123, 4566);
System.out.println(coll.containsAll(coll1));//flase
//8.remove(Object obj):从当前集合中移除obj元素
coll.remove(123);
System.out.println(coll);//[456, Person{name='Tom', age=23}, Person{name='Jarry', age=34}, false]
//9.removeAll(Collection coll1):差集:从当前集合中和coll1中所有的元素
Collection coll2 = Arrays.asList(123, 456, false);
coll.removeAll(coll2);
System.out.println(coll);//[Person{name='Tom', age=23}, Person{name='Jarry', age=34}]
}
@Test
public void test3() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Tom", 23));
coll.add(new Person("Jarry", 34));
coll.add(false);
//10.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
Collection coll1 = Arrays.asList(123, 345, 456);
boolean b = coll.retainAll(coll1);
System.out.println(b);//true
System.out.println(coll);//[123, 456]
//11.equals(Object obj):返回true需要当前集合和形参集合的元素相同
Collection coll2 = new ArrayList();
coll2.add(123);
coll2.add(456);
System.out.println(coll.equals(coll2));//true
//12.hashCode():返回当前对象的哈希值
System.out.println(coll.hashCode());//5230
//13.集合--->数组:toArray()
Object[] array = coll.toArray();
for (Object obj : array) {
System.out.println(obj);
}
//14.数组--->集合:调用Arrays类的静态方法asList()
List<int[]> ints = Arrays.asList(new int[]{123, 345});
System.out.println(ints.size());//1
List<String> strings = Arrays.asList("AA", "BB", "CC");
System.out.println(strings);//[AA, BB, CC]
//15.iteratoriterator():返回Iterator接口的实例,用于遍历集合元素。
}