一方面,面向对象语言对事务的体现都是以对象的形式,为了方便多个对象的操作,就要对对象进行存储。另一方面,使用Array存储对象具有一些弊端,而Java集合就像一种容器,可以动态的把多个对象的引用存入集合中
1.1 Java集合的组织结构
集合工具类
Collections
Arrays
排序接口
Comparable
Comparator
1.2 Collection接口
主要API:
-
size()
返回集合长度 -
add(Object object)
添加 返回boolean -
addAll(Collection collection)
把集合中的所有元素添加到另一个集合里 -
isEmpty()
返回boolea是否为空 -
clear()
清空集合 -
contains(Object obj)
集合是否包含某对象 -
containsALL(Collection coll)
是否包含集合中的所有元素 -
retainAll(Collection coll)
保留两个集合共同的元素,交集 -
remove(Object obj)
移除一个元素 -
equals()
-
hashcode()
-
toArray()
将集合转化为数组 -
iterator()
返回Iterator接口实现类的对象,遍历集合对象
public class TestCollection {
@Test
public void testCollcetion1() {
//因为Collection是接口,所以用List接口下的实现类 ArrayList来创建对象
Collection collection = new ArrayList();
//1.size()返回集合长度
System.out.println(collection.size());
//2.add(Object object)添加 返回boolean
collection.add(123);
collection.add("AA");
collection.add(new Date());
collection.add("BB");
//3.addAll(Collection collection) 把集合中的所有元素添加到另一个集合里
//Arrays数组转集合
Collection collection2 = Arrays.asList(1, 2, 3);
collection.addAll(collection2);
System.out.println(collection.size());
System.out.println(collection);
//4.isEmpty() 返回boolea是否为空
System.out.println(collection.isEmpty());
//5.clear() 清空集合
collection.clear();
System.out.println(collection.isEmpty());
}
@Test
public void testCollection2() {
Collection collection = new ArrayList();
collection.add(123);
collection.add("AA");
collection.add(new Date());
collection.add("BB");
collection.add(new Person("班", 18));
//6.contains(Object obj)
//根据元素所在类的equals方法,重写,判断成员变量的值而不是判断地址值
boolean flag = collection.contains(new Person("班", 18));
System.out.println(flag);
//7.containsALL(Collection coll) 是否包含集合中的所有元素
Collection collection2 = new ArrayList();
collection2.add(123);
collection2.add(new String("AA"));
boolean flag2 = collection.containsAll(collection2);
System.out.println(flag2);
//8 retainAll(Collection coll) 保留两个集合共同的元素,交集
//结果返回到调用集合
collection.retainAll(collection2);
System.out.println(collection); // [123, AA]
//9.remove(Object obj)
boolean flag4 = collection.remove("bb");
System.out.println(flag4);
//10.removeAll(Collection coll)
boolean flag5 = collection.retainAll(collection2);
//11.equals
//12.hashcode() 计算集合中的Hash值
System.out.println(collection.hashCode());
//13.toArray 将集合转化为数组
Object[] obj = collection.toArray();
for(Object o:obj){
System.out.println(o);
}
//14.iterator() 返回Iterator接口实现类的对象,遍历集合对象
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
1.3 List接口
List
也是接口,继承自Collection
接口,Collection
的15个方法皆可用
- 元素有序可重复
ArrayList
(动态数组):List
的主要实现类- 基于数组实现,增删慢,查询快,线程不安全
LinkedList
(链表):对于频繁的插入和删除建议选择- 基于双向链表实现,增删快,查询慢,线程不安全
Vector
: 古老的实现类,线程安全- 基于数组实现,增删慢,查询快,线程安全
- 为了线程安全损失了性能,总体上性能不如ArrayList
List
集合里添加了一些根据索引来操作集合元素的方法 -
void add(int index,Object ele)
在指定的位置添加 -
boolean addALL(int index,Collection eles)
在指定位置加上好多元素,以集合的方式传入 -
Object get(int index)
根据下标获得一个元素 -
int indexOf(Object obj)
获得一个元素的下标 int lastIndexOf(Object obj)
返回最后一个元素的下标Object remove(int index)
删除指定位置的元素Object set(int index,Object ele)
设定指定位置的元素变成一个新值List subList(int fromIndex,int toIndex)
返回从fromIndex到 toIndex之间的子list !左闭右开-
List
常用方法 - 增
add
or 插入add(index,Object obj)
- 删
remove
元素/下标 - 改
set
- 查
get(index)
@Test public void testList1() { //List是接口 , ArrayList是实现类 List list = new ArrayList(); list.add(123