Java集合之collection体系大致内容为下图
1.collection接口
(1).集合的方法
a.增加
boolean add(XXX)将某元素放到集合中。(添加成功返回值为true)
boolean addAll(xxx)把一个集合的内容放到另一个集合中。(添加成功返回值为true)
public static void main(String[] args) {
//将字符串存到集合中
Collection col = new ArrayList();
col.add("1");
//将col集合的内容放到coll中
Collection col1 = new ArrayList();
col1.addAll(col);
}
b.删除
void clear(),删除集合中所有元素
boolean remove(xxx);删除集合中指定元素(删除什么传入什么,删除成功返回true)
boolean removeAll(xxx),从一个集合中删除传入集合的内容(删除成功返回true)
public static void main(String[] args) {
//将字符串存到集合中
Collection col = new ArrayList();
col.add("1");
//将col集合的内容放到coll中
Collection col1 = new ArrayList();
col1.add("2");
col1.addAll(col);
//从col中删除"1"
col.remove("1");
//在col1中删除col的内容
col1.removeAll(col);
//清空col1
col1.clear();
}
c.判断
boolean contains(xxx)是否包含传入的该元素
boolean containsAll(xxx)是否包含传入集合元素
boolean isEmpty()该集合是否为空
public static void main(String[] args) {
//将字符串存到集合中
Collection col = new ArrayList();
col.add("1");
boolean contains = col.contains("1");//true
boolean empty = col.isEmpty();//false
Collection col1 = new ArrayList();
col1.addAll(col);
boolean con = col1.containsAll(col);//true
}
f.数组的转换
Object[] toArray(),以数组的形式返回该集合中的所有元素
public static void main(String[] args) {
//将字符串存到集合中
Collection col = new ArrayList();
col.add("1");
col.add("2");
Object[] array = col.toArray();//["1","2"]
}
2.list
有序的 collection(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
a.添加
add(),addAll(),用法和collection接口中的方法一致,不过元素是被有序的添加到了集合的尾部,
boolean add(index,element);精确插入
public static void main(String[] args) {
//将字符串存到集合中
List list = new ArrayList();
list.add("1");
list.add("2");//将"2"添加到"1"后面
list.add(1, "3");//将"3"添加到二者之间
List list1 = new ArrayList();
list1.add("0");
list.addAll(list1);//将list1集合添加到list尾部
}
b.获取
Object get(index);取出集合中索引为index的元素。
public static void main(String[] args) {
//将字符串存到集合中
List list = new ArrayList();
list.add("1");
list.add("2");//将"2"添加到"1"后面
Object object = list.get(0);//"1"
}
e.size();返回集合中元素的个数
public static void main(String[] args) {
//将字符串存到集合中
List list = new ArrayList();
list.add("1");
list.add("2");//将"2"添加到"1"后面
list.size();
}
list.size()返回2
f.删除
三个基本的删除方法与collection一致
多出一个remove(index);
public static void main(String[] args) {
//将字符串存到集合中
List list = new ArrayList();
list.add("1");
list.add("2");//将"2"添加到"1"后面
list.remove(0);//删除"1"
}
3.ArrayList
ArrayList中的元素和可以重复是有序的集合,长度不固定。
不是线程安全的。
效率高(查的效率高,因为可使用索引)可存null。
其常用API与list接口基本一致
4.LinkedList
List 接口的链接列表实现。实现所有可选的列表操作,并且允许所有元素(包括null)。除了实现List接口外,LinkedList 类还为在列表的开头及结尾get、remove和 insert元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列。
不是线程安全的。
区别API public static void main(String[] args) {
//将字符串存到集合中
LinkedList list = new LinkedList();
list.addFirst(1);
list.addLast(2);
list.removeFirst();
list.removeLast();
list.add(1);
list.add(2);
list.add(3);
Object pop = list.pop();//1
list.push(5);//[5,2,3]
}
5.Vetor
和ArrayList功能类似,最主要的区别就在于vector是线程并发安全的。但是缺点是效率比较低。
6.set
无序,不包含重复元素,最多一个null,方法与collection中基本相同
7.HashSet
无序,不包含重复元素,最多一个null,不是线程安全,方法与collection中基本相同
8.LinkedHashSet
有迭代顺序,不包含重复元素,最多一个null,不是线程安全,方法与collection中基本相同
9.TreeSet(可支持自定义排序,如果其所存储的对象中没有实现comparable接口)
不可重复,可支持自定义排序,不允许null存在,不是线程安全。