Collection:单列集合 | List:元素有序、可重复 | ArrayList:底层数组,易查找 |
LinkedList:底层链表,易删除和添加 | ||
Set:元素不可重复 | HashSet:底层哈希表,哈希表底层是数组(主)和链表,遍历顺序不保证 | |
TreeSet:元素排序 | ||
Map:双列集合,key重复时替换 | HashMap: | |
TreeSet:对key进行排序 | ||
接口 | 实现类 |
补充:
Collections类:针对集合操作的工具类,里面都是静态方法
Collection
常用方法:
size() | 大小 |
add(E e) | 添加 |
remove(Object o) | 删除 |
isEmpty() | 是否为空 |
clear() | 清空 |
contains(Object o) | 是否包含 |
iterator() | 返回集合元素的迭代器 |
Iterator:迭代器,集合专用的迭代方式
使用:Iterator<E> iterator=collection.inerator();
方法:next() 返回下一个元素
hasNext() 是否有下一个元素
注意:执行next()方法时,不允许执行添加、删除操作,因为next方法会判断预期修改值和实际修改值是否想等。(并发修改异常)
//使用
Collection<String> collection=new ArrayList<String>();
collection.add("Hello");
collection.add("world");
collection.add("你好");
//遍历集合,输出
Iterator<String> iterator=collection.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//其它遍历方式
//普通for循环,带索引
for(int i=0;i<collection.size();i++){
System.out.println(((ArrayList<String>) collection).get(i));
}
//增强for循环,不带索引,内部实现是Iterator迭代器
for (String string:collection) {
System.out.println(string);
}
1. List:有序、可重复
特有方法:
add(int index,E e) | 在指定位置添加,后面的元素依次后移 |
remove(int index) | 删除指定位置元素,返回删除的元素 |
set(int index,E e) | 修改指定位置元素,返回修改前的那个元素 |
get(int index) | 获取指定位置元素 |
特有迭代器:ListIterator
next() | |
hasNext() | |
previous() | 上一个元素 |
hasPrevious() | 是否有上一个元素 |
add(E e) | 添加一个元素 |
//ListIterator的add方法不会产生并发修改异常,因为add中会把实际值 赋值给 预期值
//源码如下:
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount; //把实际值 赋值给 预期值
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
- ArrayList:底层是数组,查询快
方法:
size() | 长度 |
add( E ) add( index,E ) | 添加元素 |
get( index ) | 获取元素 |
remove( E ) [返回boolean] remove( index ) [返回删掉的元素] | 删除元素 |
set( index,E ) [返回原来的元素] | 修改元素 |
- LinkedList:底层是链表,增删快
特有方法:
addFirst(E e) | addLast(E e) | 添加元素 |
getFirst() | getLast() | 获取元素 |
removeFirst() | removeLast() | 删除元素 |
2. Set:不可重复
Set:元素无重复
没有带索引的方法,不能用普通for循环遍历
- HashSet:底层是哈希表
哈希表解释见(5. 哈希表):https://blog.csdn.net/poppy_rain/article/details/96485097
对迭代顺序不保证(遍历输出时,顺序不定)
要保证对‘对象’的无重复,需要在类内重写hashCode() 和 equals()方法(自动生成即可)
LinkedHashSet:底层是哈希表和链表(可以保证迭代顺序不变)
//使用案例
HashSet<Student> hashSet = new HashSet<Student>();
hashSet.add(new Student(1, "xiaohei", 22));
hashSet.add(new Student(2, "xiaobai", 23));
hashSet.add(new Student(3, "xiaohui", 14));
hashSet.add(new Student(3, "xiaohui", 14));
Iterator<Student> HashSet = hashSet.iterator();
while (HashSet.hasNext()) {
System.out.println(HashSet.next().toString());
}
public class Student{
........
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return id == student.id &&
age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age);
}
......
}
- TreeSet:元素有序
两种构造方法:
TreeSet() :自然排序(升序)
TreeSet(Comparator c) :根据指定的比较器排序
public void funTreeSet(){
TreeSet<Integer> treeSet01=new TreeSet<Integer>();//自然排序
TreeSet<Integer> treeSet02=new TreeSet<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//十位数正序 o1/10,个位数倒序 o1%10
int num01= o1/10 - o2/10; //十位数正序
int num02= (num01==0) ? (o2%10 - o1%10) : num01; //个位数倒序
return num02;
}
});//按照 自定义比较器 排序
//treeSet添加10个随机数
for(int i=0;i<10;i++){
Random r=new Random();
int n=r.nextInt(50);
treeSet01.add(n);
treeSet02.add(n);
}
//输出
for (int i:treeSet01) {
System.out.print(i+"\t");//输出正序排序结果
}
System.out.println();
for (int i:treeSet02) {
System.out.print(i+"\t");//输出 自定义比较器 排序结果
}
System.out.println();
}
//随机一遍输出结果
1 3 17 18 20 22 25 41 45
3 1 18 17 25 22 20 45 41
实现对类排序:需要类实现Comparable<E>接口,重写compareto()方法
public class Student implements Comparable<Student> {
......
//Comparable 自然排序
@Override
public int compareTo(Student s) {
int num = this.id-s.id; //this在前,正序
int num2 = num==0 ? this.age-s.age:num;
int num3 = num2==0 ? this.name.compareTo(s.name):num2;
return num3;
}
......
}
Map
Map:<key,value>
key值无重复
常用方法:
get(key) | 根据key值获取value |
keySet() | 获取key值集合,返回类型:Set<key> |
values() | 获取value值集合,返回类型:Collection<value> |
- HashMap:
常用方法:
put(key,value) | 添加,key重复时进行替换 |
remove(key) | 删除,返回key值对应的value |
size() | 大小 |
clear() | 清空 |
isEmpty() | 是否为空 |
containsKey(key) | 是否包含某个key |
containsValue(value) | 是否包含某个value |
两种遍历方式:
1. 获取key值集合,遍历key 找到对应的value
2. 获取map的键值对(entrySet),根据键值对分别获取key和value
public void funHashMap(){
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put("A","apple");
hashMap.put("B","book");
hashMap.put("C","calendar");
//遍历1
Set<String> keys = hashMap.keySet();
for(String key:keys){
System.out.print(key+":"+hashMap.get(key)+"\t");
}
System.out.println();
//遍历2
Set<Map.Entry<String, String>> entrySet = hashMap.entrySet();
for(Map.Entry<String,String> entry:entrySet){
System.out.print(entry.getKey()+":"+entry.getValue()+"\t");
}
}
- TreeMap:对key进行排序
排序方法同TreeSet
Collections 类
常用方法:(用list表示集合参数)
sort(list) sort(list, Comparator) | 排序:自然排序(升序),类需要实现Comparable接口; 或者自定义比较器进行排序 |
reverse(list) | 反转 |
shuffle(list) | 随机置换(洗牌) |