1 Collection
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/d5d175d825f453312cc85c8952c01266.png#clientId=u17e0c256-9a0a-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uca17ecc9&margin=%5Bobject%20Object%5D&originHeight=479&originWidth=1084&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=ub47c7428-772b-493f-af06-25ae18e89d3&title=#pic_center)
2 List
- List集合类中的元素有序(添加顺序和取出顺序一致、可以重复)
- List集合中的每个元素都由其对应的顺序索引
- List容器中的元素都对应一个整数型的序号记载在其容器中的位置,可以根据序号存取容器中的元素
- JDK API中List接口的实现类:ArrayList、LinkedList、Vector
1.1 常用函数
- add()
- addAll(1,list2)
- list.indexOf(“tom”)
- list.remove(0)
- list.set(1,“jessy”)
- list.remove(5)
1.2 ArrayList
- 可以加入多个null
- ArrayList是由数组来实现数据存储的
- 线程不安全(执行效率高),多线程情况下不使用ArrayList
- 源码机制
- ArrayList中维护了一个Object类型数组elementData,
transient Object[] elementData;
- 当创建ArrayList对象时,如果使用无参构造器,则初始elementData容量为0,第1次添加,则扩容elementData为10,如果需要再次扩容,则扩容elementData为1.5倍
- 如果使用指定大小的构造器,则初始elementData容量为指定大小,如果需要扩容,则直接扩容elementData为1.5倍
- 如果使用的是指定大小的构造器,则初始elementData容量为指定大小,如果需要扩容,则直接扩容elementData为1.5倍
1.3 Vector
- Vector类定义的说明
public class Vector<E> extends Abstract<E> implements List<E>,RandomAccess,Clonable,Serializable
- Vector 底层也是一个对象数组
protected Object[] elementData;
- Vector是线程同步的,即线程安全,Vector类的操作方法带有synchronized,在开发中需要线程同步安全时,考虑使用Vector
public synchronized E get(int index){
if(index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
- ArrayList 和 Vector 比较
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d6635e95236343e9c46e9d7eaaf332d8.png)
1.4 LinkedList
- LinkedList底层实现双向链表和双端队列特点
- 可以添加任意元素(元素可重复),包括Null
- 线程不安全,没有实现同步
- LinkedList 底层维护了一个双向链表
- LinkedList中维护了两个属性first和last分别指向首节点和为节点
- 每个节点(Node对象),里面又维护了prev、next、item三个属性,其中通过prev指向前一个,通过next指向后一个节点,最终实现双向链表
- LinkedList的元素的添加和删除,不是通过数组完成的,相对来说效率比较高
public class LinkedListExercise {
public static void main(String[] args){
Node jack = new Node("jack");
Node tom = new Node("tom");
Node jessy = new Node("jessy");
jack.next=tom;
tom.pre=jack;
tom.next=jessy;
jessy.pre=tom;
System.out.println("从头到尾遍历");
Node first = jack;
while (true){
if(first==null){
break;
}
System.out.println(first);
first=first.next;
}
System.out.println("从尾到头遍历");
Node last=jessy;
while (true){
if(last==null){
break;
}
System.out.println(last);
last=last.pre;
}
Node alice = new Node("alice");
alice.next = tom.next;
tom.next=alice;
alice.pre = tom;
jessy.pre =alice;
System.out.println("从头到尾遍历");
first = jack;
while (true){
if(first==null){
break;
}
System.out.println(first);
first=first.next;
}
}
}
class Node {
public Object item;
public Node pre;
public Node next;
public Node(Object name){
this.item=name;
}
public String toString(){
return "Node name=" + item;
}
}
- 常用函数
- remove(loc)
- 默认删除第一个节点
- loc:第几个节点
- set(loc,val)
- ArrayList 和LinkedList的区别
| 底层结构 | 增删的效率 | 改查的效率 |
---|
ArrayList | 可变数组 | 较低,数组扩容 | 较高 |
LinkedList | 双向链表 | 较高,通过链表追加 | 较低 |
3 Map
https://xianshang.blog.csdn.net/article/details/81487594
3.1 HashMap
- Map中的key和value都可以为null
- 一对k-v是放在一个HashMap$Node中,因为Node实现了Entry接口,有些书也说一对Entry就是一对k-v
- Map遍历
import java.util.*;
public class HashSetSource {
public static void main(String args[]){
Map map = new HashMap();
map.put("邓超","孙俪");
map.put(null,"刘亦菲");
map.put("鹿晗","关晓彤");
System.out.println("1.for循环遍历");
for(Object value: map.values()){
System.out.println(value);
}
System.out.println("2.迭代器遍历");
Iterator iterator = map.values().iterator();
while (iterator.hasNext()){
Object value = iterator.next();
System.out.println(value);
}
System.out.println("3.EntrySet迭代器");
Set entrySet = map.entrySet();
Iterator iterator1 =entrySet.iterator();
while (iterator1.hasNext()){
Object o = iterator1.next();
Map.Entry entry = (Map.Entry) o;
System.out.println(entry.getKey()+"-"+entry.getValue());
}
}
}
- 底层使用hash表方式存储,不能保证映射的顺序
- HashMap没有实现同步,因为线程不安全,方法没有做同步互斥处理,没有synchronized
- 底层机制
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/280f3f85cc482be703f7e22a9eade3ad.png#clientId=u17e0c256-9a0a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=256&id=u4a234c72&margin=%5Bobject%20Object%5D&name=image.png&originHeight=384&originWidth=960&originalType=binary&ratio=1&rotation=0&showTitle=false&size=458067&status=done&style=none&taskId=uf8cb18ae-bbfa-495d-b49e-cc0fafbc2ad&title=&width=640#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/5b82e2a985ad6f2265fb5222ea651e0f.png#clientId=u17e0c256-9a0a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=253&id=u052fdb48&margin=%5Bobject%20Object%5D&name=image.png&originHeight=379&originWidth=953&originalType=binary&ratio=1&rotation=0&showTitle=false&size=685565&status=done&style=none&taskId=ua932aadb-7137-4cb5-91d6-09827b47d02&title=&width=635.3333333333334#pic_center)
3.2 HashTable
- 使用
HashMap hashMap = new HashMap();
hashMap.put(i,"hello");
- 存放的元素是键值对 K-V
- hashtable的键和值都不能为Null,否则就会抛出NullPointerException
- hashTable 使用方法基本上和HashMap一样
- hashTable是线程安全的,hashMap是线程不安全的
- HashTable和HashMap的对比
| 版本 | 线程安全(同步) | 效率 | 允许null键null值 |
---|
HashMap | 1.2 | 不安全 | 高 | 可以 |
HashTable | 1.0 | 安全 | 较低 | 不可以 |
2.3 Properties
- Properties类继承自HashTable类并且实现了Map接口,也是使用一种键值对的形式来保存数据
- 使用特点和HashTable类似
- Properties还可以用于从xxx.properties文件中,加载数据到Properties类并进行读取和修改
- 说明:xxx.properties文件通常作为配置文件
- 使用
Properties properties = new Properties();
properties.put("abc",null);
4 Set
- 介绍
- 无序(添加和取出的顺序不一致),没有索引
- 不允许重复元素,所以最多包含一个Null
- 常用方法:和Collection接口一样
- 遍历方式:同Collection的遍历方式一样,因为Set接口是Collection接口的子接口
- 可以使用迭代器
- 增强for
- 不能使用索引的方式来获取
4.1 HashSet
- HashSet实现了Set接口
- HashSet实际上是HashMap
public HashSet(){
map = new HashMap();
}
- 有且仅能能存放一个Null值
- HashSet不保证元素是有序的,取决于hash后,再确定索引的结果。不保证存放元素的顺序和取出顺序一致
- 不能有重复元素/对象
- 使用
HashSet set = new HashSet();
set.add("小明");
set.add("小明");
- 底层机制
- HashMap底层:数组+链表+红黑树
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/d28cbf4f268bc8aa905fd105b92da757.png#clientId=u17e0c256-9a0a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=311&id=uf0e53322&margin=%5Bobject%20Object%5D&name=image.png&originHeight=466&originWidth=972&originalType=binary&ratio=1&rotation=0&showTitle=false&size=334597&status=done&style=none&taskId=u03a814cd-ec54-4e49-84fa-a0e3511355f&title=&width=648#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/4dabad670f6b10038944c15b3ecf9c30.png#clientId=u17e0c256-9a0a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=293&id=u1739fdb9&margin=%5Bobject%20Object%5D&name=image.png&originHeight=378&originWidth=838&originalType=binary&ratio=1&rotation=0&showTitle=false&size=487987&status=done&style=none&taskId=uf36fceab-aee1-4aa0-b2ca-e4288fb28d2&title=&width=648.6666870117188#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/adf590c0b92ed5b877aa21cf3474fc26.png#clientId=u76663fa7-8631-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=289&id=u21d67226&margin=%5Bobject%20Object%5D&name=image.png&originHeight=433&originWidth=1012&originalType=binary&ratio=1&rotation=0&showTitle=false&size=613262&status=done&style=none&taskId=udff115fc-060f-4338-9368-9f285510dde&title=&width=674.6666666666666#pic_center)
4.2 LinkedHashSet
- LinkedHashSet是HashSet的子类
- LinkedHashSet底层是一个LinkedHashMap,底层维护了一个数组+双向链表
- LinkedHashSet根据元素的hashCode来决定元素存储位置,同时使用链表维护元素的次序,使得元素看起来是以插入顺序保存的
- LinkedHashSet不允许添加重复元素
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/a49a716eac0ea78528d8927373973d44.png#clientId=u76663fa7-8631-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=347&id=udedb2c3a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=520&originWidth=979&originalType=binary&ratio=1&rotation=0&showTitle=false&size=735570&status=done&style=none&taskId=u5294d82b-6f3b-4754-a362-d51b808def5&title=&width=652.6666666666666#pic_center)
5 Collection工具类
- Collection是一个操作Set、List和Map等集合的工具类
- Collections中提供了一系列静态方法对集合元素进行排序、查询、修改
- 排序
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/4495421a81959b13a70831f029c0c57b.png#clientId=u76663fa7-8631-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=143&id=uee3a03db&margin=%5Bobject%20Object%5D&name=image.png&originHeight=214&originWidth=910&originalType=binary&ratio=1&rotation=0&showTitle=false&size=310427&status=done&style=none&taskId=ua55b0355-2efb-44d7-b5db-1d0c7764e53&title=&width=606.6666666666666#pic_center)
- 查找、替换
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/5f746a6d47f7cd58b642fca41ce23ab5.png#clientId=u76663fa7-8631-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=212&id=u0690f1cc&margin=%5Bobject%20Object%5D&name=image.png&originHeight=318&originWidth=901&originalType=binary&ratio=1&rotation=0&showTitle=false&size=476206&status=done&style=none&taskId=ub511d6f9-d5be-45a4-862a-14ec4b8553a&title=&width=600.6666666666666#pic_center)
6 总结
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/6e20ed59f6be04f9697847418cdc981b.png#clientId=u17e0c256-9a0a-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=259&id=u7b0eb985&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=846&originalType=binary&ratio=1&rotation=0&showTitle=false&size=509722&status=done&style=none&taskId=u0c84af99-32a6-4628-8d92-95dd255adf3&title=&width=564#pic_center)