Collection树形图
Collection 接口
|--List 接口,继承Collection
|--ArrayList --- implement List
|--Vector --- implement List
|--LinkedList --- implement List
|--Set 接口,继承Collection
|--HashSet --- implement Set
|--linkedHashSet 继承HashSet
|--TreeSet --- implement Set
Collection
|--List 有序,可重复
|--ArrayList
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高
|--Vector
底层数据结构是数组,查询快,增删慢。
线程安全,效率低
|--LinkedList
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高
|--Set 无序,唯一
|--HashSet
底层数据结构是哈希表。
元素唯一性的保证
依赖两个方法:添加的对象要重写hashCode()和equals()
|--LinkedHashSet
底层数据结构是链表和哈希表
由链表保证元素有序
由哈希表保证元素唯一
|--TreeSet
底层数据结构是红黑树。
保证元素排序的保证
自然排序
比较器排序
保证元素唯一性的保证
根据比较的返回值是否是0来决定
常用方法:
1:添加功能
- boolean add(Object obj):添加一个元素
- boolean addAll(Collection c):添加一个集合的元素
2:删除功能 - void clear():移除所有元素
- boolean remove(Object o):移除一个元素
- boolean removeAll(Collection c):移除多个在集合c中给定的元素,只要有一个元素被移除了,就返回true。
3:判断功能 - boolean contains(Object o):判断集合中是否包含指定的元素
- boolean containsAll(Collection c):如果此 collection 包含指定 collection 中的所有元素,则返回 true,少了一个都返回false
- boolean isEmpty():判断集合是否为空
4:获取功能 - Iterator iterator
5:长度功能 - int size():元素的个数
6:交集功能 - boolean retainAll(Collection c): 假设有两个集合A,B。 A对B做交集,最终的结果保存在A中,B不变。 返回值表示的是A是否发生过改变。
7:把集合转换为数组 - Object[] toArray()
// 创建集合1
Collection c1 = new ArrayList();
c1.add("a1");
c1.add("a2");
c1.add("a3");
c1.add("a4");
// 创建集合2
Collection c2 = new ArrayList();
c2.add("b1");
c2.add("b2");
c2.add("b3");
c2.add("b4");
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
System.out.println("--------------");
c1.addAll(c2);
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
System.out.println("--------------");
System.out.println(c1.remove("b4"));
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
System.out.println("--------------");
System.out.println(c1.removeAll(c2));
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
System.out.println("--------------");
System.out.println(c1.contains("a2"));
System.out.println(c1.containsAll(c2));
System.out.println("--------------");
System.out.println(c1.isEmpty());
System.out.println(c1.size());
System.out.println("--------------");
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
c1.add("b1");
boolean retainAll = c2.retainAll(c1);
System.out.println(retainAll);
System.out.println("c1:" + c1);
System.out.println("c2:" + c2);
System.out.println("--------------");
for(Iterator it=c1.iterator();it.hasNext();){
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("--------------");
Object[] arr=c1.toArray();
for(int x=0;x<arr.length;x++){
System.out.print(arr[x]+" ");
}
}
输出:
c1:[a1, a2, a3, a4]
c2:[b1, b2, b3, b4]
--------------
c1:[a1, a2, a3, a4, b1, b2, b3, b4]
c2:[b1, b2, b3, b4]
--------------
true
c1:[a1, a2, a3, a4, b1, b2, b3]
c2:[b1, b2, b3, b4]
--------------
true
c1:[a1, a2, a3, a4]
c2:[b1, b2, b3, b4]
--------------
true
false
--------------
false
4
--------------
c1:[a1, a2, a3, a4]
c2:[b1, b2, b3, b4]
true
c1:[a1, a2, a3, a4, b1]
c2:[b1]
--------------
a1 a2 a3 a4 b1
--------------
a1 a2 a3 a4 b1