Java的集合框架体系结构
1. ArrayList
2. LinkedList
3. HashMap
4. LinkedHashMap
5. TreeMap
6. ArrayDeque
7. PriorityQueue
1、ArrayList
创建
**ArrayList的增删改查–>add();remove();set();get();
除了最基本的操作方法还有
方法 | 解释 |
addAll() | 添加集合中所有元素到ArrayList中,可以是Array或者Set等数据集合 |
clone() | 复制一份集合,不过这里是浅复制,一旦原来的ArrayList发生改变也会随着改变,可以使用深拷贝,直接使用for创建一个 |
contains() | 查看ArrayList是否含有某一个元素 |
indexOf | 查找某个元素在ArrayList的index |
removeAll() | 在ArrayList移除一个集合中所有的元素 |
size() | ArrayList元素中元素的个数 |
isEmpty() | ArrayList是否为空 |
subList() | 截取ArrayList的元素 |
sort() | 对于ArrayList的元素进行排序; |
toArray() | 对于ArrayList转换成Array,也可通过String[] stringArray = new String[]{“chen”, “zi”, “mu”, “chen”, “abc”, “chen”}; arrayList.addAll(List.of(stringArray));反转 |
toString() | 将ArrayList转换成String, |
lastIndexOf() | 字面意思 |
retainAll() | 保留一个集合里指定元素 |
containAll() | 查看是否包含一个集合中所有元素; |
removeRange() | 移除ArrayList指定索引之间的元素。 |
replaceAll() | 对于ArrayList的元素进行挨个替换,配合lambda |
removeIf() | 删除满足条件的元素,配合匿名函数; |
package arrayListTemplate;
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
* @Project: oob
* @Date: 2023/1/12-22:13
* @Author: chenzi
* @Filename: tempArrayList
*/
public class tempArrayList {
// NOTE增加
public ArrayList arrayListOperator(ArrayList arrayList, String[] values) {
for (String value : values) {
arrayList.add(value);
}
return arrayList;
}
// NOTE 删除
public ArrayList arrayListDelete(ArrayList arrayList, int index) {
arrayList.remove(index);
return arrayList;
}
//NOTE 改
public ArrayList arrayListModify(ArrayList arrayList, int index, String value) {
arrayList.set(index, value);
return arrayList;
}
// NOTE 查
public String arrayListSearch(ArrayList arrayList, int index) {
String value="";
try {
value = (String) arrayList.get(index);
} catch (Exception e) {
value = "null";
}finally {
System.out.println("get result");;
}
// ArrayList value;
return value;
}
// main()fuction
public static void main(String[] args) {
String[] arrayValues = {"1", "alpha", "beta", "gamma"};
ArrayList<String> strArrayListTemp = new ArrayList<String>();
tempArrayList chenzi = new tempArrayList();
ArrayList resAdd = chenzi.arrayListOperator(strArrayListTemp, arrayValues);
System.out.println(resAdd);
ArrayList resDelete = chenzi.arrayListDelete(resAdd, 2);
System.out.println(resDelete);
ArrayList resModify = chenzi.arrayListModify(resDelete, 0, "start");
System.out.println(resModify);
String value=chenzi.arrayListSearch(resAdd,2);
System.out.println(value);
}
}LinkedList
LinkedList的基本操作,跟ArrayList 非常像
package arrayListTemplate;
import javax.management.RuntimeErrorException;
import java.util.LinkedList;
/**
* @Project: oob
* @Date: 2023/1/12-23:53
* @Author: chenzi
* @Filename: tempLinkedArrayList
*/
public class tempLinkedArrayList {
LinkedList<String> tempLinkedList = new LinkedList<String>();
// Note LinkedList增
public LinkedList linkedListAdd(LinkedList tempLinkedList, String[] values) {
for (String val : values) {
tempLinkedList.add(val);
}
return tempLinkedList;
}
public LinkedList linkedListDelete(LinkedList tempLinkedList, int index) {
tempLinkedList.remove(index);
return tempLinkedList;
}
public LinkedList linkedListModify(LinkedList tempLinkedList, int index, String value) {
try {
tempLinkedList.set(index, value);
} catch (Exception e) {
// throw new RuntimeErrorException("Error");
throw new RuntimeException("Your Message");
}
return tempLinkedList;
}
public String linkedListValue(LinkedList tempLinkedList, int index) {
String value;
try {
value = (String) tempLinkedList.get(index);
} catch (Exception e) {
throw new RuntimeException("找不到元素");
}
return value;
}
public static void main(String[] args) {
LinkedList<String> testLinkedList = new LinkedList<String>();
tempLinkedArrayList chenzi = new tempLinkedArrayList();
String[] values = {"alpha", "beta", "gamma", "delta"};
LinkedList<String> resAdd = chenzi.linkedListAdd(testLinkedList, values);
System.out.println("增加四个元素 结果为:\n" + resAdd);
LinkedList<String> resDelete = chenzi.linkedListDelete(resAdd, 2);
System.out.println("删除index为2的元素后的LinkedList:\n" + resDelete);
LinkedList<String> resModify = chenzi.linkedListModify(resDelete, 0, "start");
System.out.println("修改index=0的值为'start',result:\n" + resModify);
String value=chenzi.linkedListValue(resModify,0);
System.out.println("拿到index=0的值:\n"+value);
}
}两者的区别
Java常用集合类型关系

主要的逻辑就是三类:List、Set、Map
在此基础上进行
List->ArrayList、LinkedList、Vector、Stack
Set->HashSet、TreeSet
Map->HashMap、TreeMap、HashTable
- ArrayList是实现了List接口,继承了AbstractList抽象类。
- LinkedList是继承AbstractSequentiList双向链表,可以作为堆栈、队列、双端队列来进行操作。
- 速度对比:新增元素的两种方式:1:增加在结尾,2:增加到指定位置,
ArrayList如果增加到结尾只需要判断是否进行扩容,直接加上就可以,如果指定位置的话,需要对插入位置后面的元素复制到新元素位置之后,所以性能会比较差。
LikedList 如果增加的元素,以为LinkedList保存上一个跟下一个的位置。
新增的时候
在头部插入:LinkedList>ArrayList,如果在后半部可能ArrayList>LinkedList,因为需要进行遍历,ArrayList更直接。
删除元素的时候
ArrayList从集合头部位置删除元素花费的时间380
LinkedList从集合头部位置删除元素花费的时间4
ArrayList从集合中间位置删除元素花费的时间381
LinkedList从集合中间位置删除元素花费的时间5922
ArrayList从集合尾部位置删除元素花费的时间8
LinkedList从集合尾部位置删除元素花费的时间12迭代器与可迭代对象
相对于Python迭代器一定是可迭代对象
Iterator是实现了:iter()跟__next(),才是迭代器,而可迭代对象只有__iter()
HashMap
HashMap类似于python里的dict,通过键值对来进行保存数据,对象通过范式进行定义,通过put()跟get()来进行访问。
public HashMap hashMapPut() {
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
Sites.put(1, "alpha");
Sites.put(2, "beta");
Sites.put(3, "gamma");
Sites.put(4, "delta");
System.out.println(Sites);
return Sites;
}
public String hashMapGet(HashMap hashMapExample, Integer key) {
String value = (String) hashMapExample.get(key);
return value;
}
public void hashMapForloop(HashMap hashMapExample) {
System.out.println(hashMapExample.keySet());
System.out.println(hashMapExample.keySet() instanceof Object);
for (Object key : hashMapExample.keySet()) {
System.out.println(key + ":" + hashMapExample.get(key));
}
}
//**TODO HasMap的遍历--->for object?不知道什么原因。**
public void hashMapForloop(HashMap hashMapExample) {
System.out.println(hashMapExample.keySet());
System.out.println(hashMapExample.keySet() instanceof Object);
for (Object key : hashMapExample.keySet()) {
System.out.println(key + ":" + hashMapExample.get(key));
}
}
public static void main(String[] args) {
hashMapClass chenzi = new hashMapClass();
HashMap hashMapExample = chenzi.hashMapPut();
String value = chenzi.hashMapGet(hashMapExample, 2);
System.out.println(value);
chenzi.hashMapForloop(hashMapExample );
}HashMap的遍历方式
这里主要是三种,使用Iterator、forEach、Lambda。
package List;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @Author: chenzi
* @Date: 2023/1/13--19:57
* @Project: collection
**/
public class HashMapSort {
public HashMap initHashMap(String[] keys, Integer[] values) {
HashMap<String, Integer> hashMapTemplate = new HashMap<>();
for (int index = 0; index < keys.length; index++) {
hashMapTemplate.put(keys[index], values[index]);
}
return hashMapTemplate;
}
// NOTE 使用iterator来进行遍历
public void hasmapSortedAlapha(HashMap hashMapTemplate) {
Iterator<Map.Entry<String, Integer>> iterator = hashMapTemplate.entrySet().iterator();
System.out.println("构建一个iterator来进行遍历!!!");
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
// NOTE 使用forEach 来进行遍历
public void hashMapSortedBeta(HashMap hashMapTemplate) {
System.out.println("forEach 对HashMap来进行遍历");
for (Object entry: hashMapTemplate.entrySet()) {
System.out.println(entry);
}
}
// Note 使用lambda 表达式来遍历HashMap
public void hashMapSortedGamma(HashMap hashMapTemplate){
System.out.println("lambda表达式来进行遍历");
hashMapTemplate.forEach((key,val)->{
System.out.println(key+":"+val);
});
}
public static void main(String[] args) {
// HashMap<String,Integer> templateHashMap= new HashMap<>();
String[] keys = new String[]{"a", "b", "c", "d", "e", "f", "g"};
Integer[] values = {2, 19, 3, 100, 5, 8, 1};
HashMapSort chenzi = new HashMapSort();
HashMap hashMapTemplate = chenzi.initHashMap(keys, values);
System.out.println(hashMapTemplate);
chenzi.hasmapSortedAlapha(hashMapTemplate);
chenzi.hashMapSortedBeta(hashMapTemplate);
chenzi.hashMapSortedGamma(hashMapTemplate);
}
}LinkedHashMap
相对于HashMap最大的区别就是有序的。
如果在尾部都是null:null的情况下,HashMap是放到最前面的,而LinkedHashMap是按照顺序进行摆放
LinkedHashMap的基本操作
LinkedHashMapTemplate.put();
LinkedHashMapTemplate.remove()
LinkedHashMapTemplate.replace()
LinkedHashMapTemplate.get()Map的排序
因为HashMap是无须的所以如果需要进行对HashMap进行排序这里就应用到了LinkedHashMap
String[] keyArray = new String[]{"alpha", "beta", "gamma", "delta"};
Integer[] valueArray = new Integer[]{2, 12, 455, 1000};
Map<String, Integer> alphaMap = new HashMap<>();
for (int i = 0; i < keyArray.length; i++) {
alphaMap.put(keyArray[i], valueArray[i]);
}
TreeMap<String, Integer> sortedMap = new TreeMap<>(alphaMap);
System.out.println(sortedMap);
// NOTE 使用stream
LinkedHashMap<String ,Integer>sortedValue= new LinkedHashMap<>() ;
// 这里不能new HashMap 因为这里HashMap是无序的。
alphaMap.entrySet()
.stream()
.sorted(Map.Entry.<String, Integer>comparingByValue(Comparator.reverseOrder()))
.forEachOrdered(x-> sortedValue.put(x.getKey(),x.getValue()));
System.out.println(sortedValue);TreeMap
红黑树(英语:Red–black tree)是一种自平衡的二叉查找树(Binary Search Tree),结构复杂,但却有着良好的性能,完成查找、插入和删除的时间复杂度均为 log(n)。
LinkedHashMap相对于HashMap增加了有序的,TreeMap相对于LinkedHashMap,可以保存元素的自然顺序,或者通过Comparator接口的自定义顺序;
他们之间的对比如下:

















