一.Collection
1.方法
-
boolean add(E e)
- 向集合尾部添加元素
-
void clear()
- 清空集合中所有元素
-
boolean contains(Object o)
- 查找集合中是否存在指定的元素,存在返回true,否则返回false
-
boolean isEmpty()
- 判断集合是否不存在任何元素,为空返回true,否则返回false
-
Iterator<E> iterator()
- 返回集合的迭代器
-
boolean remove(Object o)
- 查找并移除指定元素,即使遇到多个相同值的元素,也只移除一个,移除成功返回true,否则返回false
-
int size()
- 返回集合元素的个数
-
<T> T[] toArray(T[] a)
- 把泛型集合转换为泛型数组
-
boolean addAll(Collection<? extends E> c)
- 把另一个集合所有元素添加到当前集合中
- 如[1,2,2,3]与[2,3,4]返回[1,2,2,3,2,3,4]
-
boolean removeAll(Collection<?> c)
- 移除当前集合与另一集合元素相同的所有元素
- 如[1,2,2,3]与[2,3,4]返回[1],虽然当前集合有两个2,但是也会一并被移除掉
-
boolean retainAll(Collection<?> c)
- 保留当前集合与另一集合相同的元素
- 如[1,2,2,3]与[2,3,4]返回[2,2,3],把另一集合的元素逐一拿出在当前集合查找,如果存在则留下
-
boolean containsAll(Collection<?> c)
- 判断当前集合是否包含另一集合的所有元素
- 如[1,2,2,3]与[2,3,4]返回false
2.Collections工具类
public static <T> void sort(List<T> list)
对List集合排序public static <T> int binarySearch(List<?> list,T key)
对List集合进行二分法查找public static <T> T max(Collection<?> coll)
获取集合中最大值public static <T> T min(Collection<?> coll)
获取集合中最小值public static void reverse(List<?> list)
对List集合顺序进行反转public static void shuffle(List<?> list)
随机打乱集合中元素的位置
二.List
有序可重复的集合
1.List接口
(1)方法
-
void add(int index, E element)
- 在集合中指定下标位置插入元素
-
E get(int index)
- 返回指定下标的元素
-
int indexOf(Object o)
- 返回元素在集合中第一次出现的下标位置,如果元素不存在,则返回-1
-
int lastIndexOf(Object o)
- 返回元素在集合中最后一次出现的下标位置,如果元素不存在,则返回-1
-
E set(int index, E element)
- 用指定元素替换指定位置的元素、
-
E remove(int index)
- 移除指定下标位置的元素
-
ListIterator<E> listIterator()
- 返回List集合专用的迭代器
- 可用于解决遍历集合过程中同时增加修改集合(ConcurrentModificationException异常)
(2)集合遍历
List<String> list=new ArrayList<String>();
//普通for遍历(只能用于List集合,对Set集合不适用)
for (int i = 0; i < list.size(); i++) {
String s=list.get(i);
}
//迭代器遍历
Iterator<String> it=list.iterator();
while(it.hasNext()){
String s=it.next();
}
//增强for循环
for (String s : list) {
}
2.ArrayList
底层结构是数组,查询快,增删慢,线程不同步
3.Vector
底层结构是数组,查询快,增删慢,线程同步
4.LinkedList
底层结构是链表,查询慢,增删快,线程不同步
(1)方法
-
void addFirst(E e)
- 将元素插入到集合的开头位置
-
void addLast(E e)
- 将元素插入到集合的末尾位置
-
E getFirst()
- 获取集合中开头位置的元素
-
E getLast()
- 获取集合中末尾位置的元素
-
E removeFirst()
- 移除集合中开头位置的元素
-
E removeLast()
- 移除集合中末尾位置的元素
5.ArrayList丶LinkedList丶Vector区别
集合名称 | 线程同步 | 内部结构 | 查询速度 | 增删速度 |
---|---|---|---|---|
Vector | 线程安全 | 数组 | 快 | 慢 |
ArrayList | 线程不安全 | 数组 | 快 | 慢 |
LinkedList | 线程不安全 | 链表 | 慢 | 块 |
三.Set
无序不可重复的集合
1.HashSet
HashSet先比较哈希值
hashCode()
,如果不同,则添加进集合;如果相同再比较equals()
方法,比较结果为false才存入集合
(1)自定义类同样属性值为同一个对象
- 在Eclipse中按快捷键
shift+Alt+S
,再点击Generate hashCode() and equals()
,自动生成重写的方法
2.LinkedHashSet
底层链表,有序,不可重复
3.TreeSet
底层二叉树,不可重复,可自定义排序
(1)自定义排序
- 实现
Comparable
接口并重写compareTo
方法 - 返回值为负数被比较对象放在二叉树当前对象左边,正数放在右边,返回值为0则不存储,遍历时都是从左到右
//实现Comparable接口
class Student implements Comparable<Student>{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
//重写compareTo方法
@Override
public int compareTo(Student s) {
int num=(this.age-s.age)*-1; //年龄倒序
num=num==0?this.name.compareTo(s.name):num; //如果年龄相同则比较姓名
num=num==0?1:num; //如果姓名比较结果也一样也存入集合中(让结果不为0即可)
return num;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
//创建TreeSet集合
TreeSet<Student> ts=new TreeSet<Student>();
//添加元素,会根据compareTo方法存储
ts.add(new Student("张三",17));
ts.add(new Student("李四",17));
ts.add(new Student("王五",18));
ts.add(new Student("李四",17));
//输出结果:
/*
Student [name=王五, age=18]
Student [name=张三, age=17]
Student [name=李四, age=17]
Student [name=李四, age=17]
*/
for (Student student : ts) {
System.out.println(student);
}
- 创建TreeSet集合时传递比较器
- 比较器
Comparator
会比Comparable
优先执行
// 创建TreeSet集合时传递比较器给构造函数
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// 年龄倒序
int num = (s1.getAge() * s2.getAge()) * *1;
// 如果年龄相同则比较姓名
num = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
// 如果姓名比较结果也一样也存入集合中(让结果不为0即可)
num = num == 0 ? 1 : num;
return num;
}
});
// 添加元素,会根据比较器存储
ts.add(new Student("张三", 17));
ts.add(new Student("李四", 17));
ts.add(new Student("王五", 18));
ts.add(new Student("李四", 17));
// 输出结果:
/*
* Student [name=王五, age=18]
* Student [name=张三, age=17]
* Student [name=李四, age=17]
* Student [name=李四, age=17]
*/
for (Student student : ts) {
System.out.println(student);
}
四.Map
1.HashMap
HashMap底层哈希算法,键值对存储,无序,键不可重复,键值可以为null,线程不同步不安全
(1)方法
void clear()
清空集合中所有元素V remove(Object key)
根据指定键值删除元素V put(K key, V value)
添加键值对,返回被替换的值,如果不存在相同的键,则返回nullvoid putAll(Map<? extends K,? extends V> m)
把另一个集合全部添加进当前集合中boolean containsKey(Object key)
判断集合中是否存在指定的键boolean containsValue(Object value)
判断集合中是否存在指定的值boolean isEmpty()
判断集合中是否不存在任何元素int size()
获取集合中元素的个数Set<K> keySet()
获取集合中所有的键Collection<V> values()
获取集合中的所有值V get(Object key)
根据键获取值Set<Map.Entry<K,V>> entrySet()
获取所有键值对
(2)遍历
HashMap<String, Integer> hm=new HashMap<>();
hm.put("a", 1);
hm.put("b", 2);
hm.put("c", 3);
//通过键集合获取值
Set<String> ks=hm.keySet();
Iterator<String> it=ks.iterator();
while(it.hasNext()){
String key=it.next();
Integer value=hm.get(key);
System.out.println(key+"="+value);
}
//简化
for (String key : hm.keySet()) {
System.out.println(key+"="+hm.get(key));
}
//通过键值对集合分别获取键和值
Set<Map.Entry<String,Integer>> es=hm.entrySet();
Iterator<Map.Entry<String,Integer>> ies=es.iterator();
while(ies.hasNext()){
Map.Entry<String,Integer> en=ies.next();
String key=en.getKey();
Integer value=en.getValue();
System.out.println(key+"="+value);
}
//简化
for (Map.Entry<String, Integer> entry : hm.entrySet()) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
2.Hashtable
Hashtable底层哈希算法,键值对存储,无序,键不可重复,键值都不可以为null,线程同步安全
3.LinkedHashMap
LinkedHashMap是用链表结构存储的HashMap,有序,其他跟HashMap一样、
4.TreeMap
TreeMap存储的是键值对,其他跟TreeSet一样