Java 集合(Collection)、Map
Collection 主要分为三大类List、Set、Queue,它们都是java.util包里的. 而Map是key-value 的数据结构,它和Collection 是两种不同的数据结构,因此不能归为同一类.
Java Collection 大家族
- Collection
- List
- AbstractList
- LinkedList
- ArrayList
- Vector
- Stack
- AbstractList
- Set (value唯一)
- AbstractSet
- HashSet 无序
- LinkedHashSet 有序
- TreeSet 有序
- HashSet 无序
- AbstractSet
- Queue
- ConcurrentLinkedQueue 并发队列
- BlockingQueue 阻塞队列
- ArrayBlocingQueue
- LinkedBlockingQueue
- PriorityBlockingQueue
- SynchronousQueue
- List
工具类
- Collections
- Arrays
Collection 相关Api
boolean | add(E e)
确保此 collection 包含指定的元素(可选操作)。 |
---|---|
boolean | addAll(Collection
extends E>c)
将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。 |
void | clear()
移除此 collection 中的所有元素(可选操作)。 |
boolean | contains(Object o)
如果此 collection 包含指定的元素,则返回 true。 |
boolean | containsAll(Collection
>c)
如果此 collection 包含指定 collection 中的所有元素,则返回 true。 |
boolean | equals(Object o)
比较此 collection 与指定对象是否相等。 |
int | hashCode()
返回此 collection 的哈希码值。 |
boolean | isEmpty()
如果此 collection 不包含元素,则返回 true。 |
Iterator
|
iterator()
返回在此 collection 的元素上进行迭代的迭代器。 |
boolean | remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。 |
boolean | removeAll(Collection
>c)
移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。 |
boolean | retainAll(Collection
>c)
仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。 |
int | size()
返回此 collection 中的元素数。 |
Object[] | toArray()
返回包含此 collection 中所有元素的数组。 |
|
toArray(T[] a)
返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。 |
Java List 应用
public class Test
{
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println("获取大小 size() => " + list.size());
System.out.println("判空 isEmpty() => " + list.isEmpty());
System.out.println("是否存在a => " + list.contains("a"));
System.out.println("移除元素a => " + list.remove("a"));
System.out.println("是否存在a => " + list.contains("a"));
System.out.print("for 获取alue ========> ");
for(String value : list)
{
System.out.print(value + " ");
}
System.out.println("\n清空所有元素 clear() ");
}
}
运行结果为
获取大小 size() => 3
判空 isEmpty() => false
是否存在a => true
移除元素a => true
是否存在a => false
for 获取alue ========> b c
清空所有元素 clear()
Java Set 应用
Set 最主要的特点是值唯一
public class Test
{
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
set.add("pangugle");
set.add("pangugle");
System.out.println("获取大小 size() => " + set.size());
System.out.println("判空 isEmpty() => " + set.isEmpty());
System.out.println("是否存在a => " + set.contains("a"));
System.out.println("移除元素a => " + set.remove("a"));
System.out.println("是否存在a => " + set.contains("a"));
System.out.print("for 获取alue ========> ");
for(String value : set)
{
System.out.print(value + " ");
}
System.out.println("\n清空所有元素 clear() ");
}
}
获取大小 size() => 4
判空 isEmpty() => false
是否存在a => true
移除元素a => true
是否存在a => false
for 获取alue ========> b pangugle c
清空所有元素 clear()
常用的Set的HashSet, TreeSet, LinkedHashSet, 它们的用法都差不多, TreeSet 内部使用树的算法,LinkedHashSet 使用的是链表的算法