一、背景
最近在刷力扣算法题的过程中对于java集合的定义及使用想要达到更加精准的程度,于是有了本次的总结
二、目标
宏观总结:绘制java集合类图
微观:写每个集合常用的代码实现
收获:写当前的总结
通过以上三方面的目标,以达到实现力扣题的过程中所有集合可以手写出来,并且每个集合常用的方法可以了解;
三、过程
1.百度搜索java相关集合说明,绘制初步类图,并在jdk中去查看类间关系(java.bese.java.util包下)以完善类图
2.根据绘制的类图,代码实现例如:
2.1.List:arrayList、linkedList、vector
package algorithm.b01Collection;
import java.util.*;
public class a01List {
public static void main(String[] args) {
System.out.println("arrayList测试:");
arrayList();
System.out.println("linkedList测试:");
linkedList();
System.out.println("vector测试:");
vector();
}
public static void arrayList(){
ArrayList list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add("1");
list.add("2");
list.add("2");
System.out.println(Arrays.asList(list));
list.remove("b");
System.out.println(Arrays.asList(list));
list.add("b");
System.out.println(Arrays.asList(list));
}
public static void linkedList(){
LinkedList list = new LinkedList();
list.add(4);
list.add(2);
list.add(3);
list.add(1);
list.add(9);
list.add(9);
System.out.println(Arrays.asList(list));
//int类型就换成下标删除了
list.remove(2);
System.out.println(Arrays.asList(list));
list.add(1);
System.out.println(Arrays.asList(list));
}
public static void vector(){
Vector list = new Vector();
list.add(4);
list.add(2);
list.add(3);
list.add(1);
list.add(9);
list.add(9);
System.out.println(Arrays.asList(list));
//int类型就换成下标删除了
list.remove(2);
System.out.println(Arrays.asList(list));
list.add(1);
System.out.println(Arrays.asList(list));
}
}
2.2.Set:hashSet、treeSet
package algorithm.b01Collection;
import java.util.*;
public class a02Set {
public static void main(String[] args) {
System.out.println("hashSet 测试:");
hashSet();
System.out.println("treeSet 测试:");
treeSet();
}
public static void hashSet(){
HashSet set = new HashSet();
set.add("a");
set.add("a");
set.add("b");
set.add("b");
set.add("c");
System.out.println(Arrays.asList(set));
set.remove("b");
System.out.println(Arrays.asList(set));
set.add("b");
System.out.println(Arrays.asList(set));
}
public static void treeSet(){
TreeSet set = new TreeSet();
set.add("a");
set.add("a");
set.add("b");
set.add("b");
set.add("c");
System.out.println(Arrays.asList(set));
set.remove("b");
System.out.println(Arrays.asList(set));
set.add("b");
System.out.println(Arrays.asList(set));
}
}
2.3.QueueStack:queue、stack
package algorithm.b01Collection;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class a03QueueStack {
public static void main(String[] args) {
System.out.println("测试queue");
queue();
System.out.println("测试stack");
stack();
}
public static void queue(){
Queue<String> queue = new LinkedList<>();
queue.offer("b");
queue.offer("a");
queue.offer("d");
queue.offer("c");
queue.offer("e");
queue.offer("f");
System.out.println(queue);
System.out.println("poll="+queue.poll());
System.out.println(queue);
System.out.println("element="+queue.element());
System.out.println(queue);
System.out.println("peek="+queue.peek());
System.out.println(queue);
System.out.println("remove="+queue.remove());
System.out.println(queue);
}
public static void stack(){
Stack<String> stack = new Stack<>();
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
System.out.println(stack);
System.out.println(stack.peek());
System.out.println(stack);
System.out.println(stack.pop());
System.out.println(stack);
System.out.println(stack.empty());
System.out.println(stack.search("3"));
System.out.println(stack);
}
}
2.4.Map:hashMap、treeMap、linkedHashMap、weakHashMap
package algorithm.b01Collection;
import java.util.*;
public class a04Map {
public static void main(String[] args) {
System.out.println("测试 hashMap");
hashMap();
System.out.println("测试 treeMap");
treeMap();
System.out.println("测试 linkedHashMap");
linkedHashMap();
System.out.println("测试 weakHashMap");
weakHashMap();
//hashcode值相同的两个字符串
// String a = "/2010/6/13/19/766105207591607338053525248042284292/1.0";
//
// String b = "/2010/10/8/19/766105208164534487403083462088868610/1.0";
//
// System.out.println(a.hashCode());
//
// System.out.println(b.hashCode());
}
public static void hashMap(){
Map<String,Object> map = new HashMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
map.put(null,4);
map.put(null,5);
map.put("/2010/10/8/19/766105208164534487403083462088868610/1.0",5);
map.put("/2010/6/13/19/766105207591607338053525248042284292/1.0",9);
System.out.println(map);
System.out.println(map.containsKey("a"));
System.out.println(map.containsValue(1));
//remove还存在输入key和value进行移除的事情呢?
map.remove("a",1);
map.remove("/2010/6/13/19/766105207591607338053525248042284292/1.0");
System.out.println(map);
}
public static void treeMap(){
Map<String,Object> map = new TreeMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
map.put("d",5);
map.put("d",6);
// map.put(null,4);
// map.put(null,5);
map.put("/2010/10/8/19/766105208164534487403083462088868610/1.0",5);
map.put("/2010/6/13/19/766105207591607338053525248042284292/1.0",9);
System.out.println(map);
System.out.println(map.containsKey("a"));
System.out.println(map.containsValue(1));
//remove还存在输入key和value进行移除的事情呢?
map.remove("a",1);
map.remove("/2010/6/13/19/766105207591607338053525248042284292/1.0");
System.out.println(map);
}
public static void linkedHashMap(){
Map<String,Object> map = new LinkedHashMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
map.put("d",5);
map.put("d",6);
// map.put(null,4);
// map.put(null,5);
map.put("/2010/10/8/19/766105208164534487403083462088868610/1.0",5);
map.put("/2010/6/13/19/766105207591607338053525248042284292/1.0",9);
System.out.println(map);
System.out.println(map.containsKey("a"));
System.out.println(map.containsValue(1));
//remove还存在输入key和value进行移除的事情呢?
map.remove("a",1);
map.remove("/2010/6/13/19/766105207591607338053525248042284292/1.0");
System.out.println(map);
}
public static void weakHashMap(){
Map<String,Object> map = new WeakHashMap<>();
map.put("a",1);
map.put("b",2);
map.put("c",3);
map.put("d",4);
map.put("d",5);
map.put("d",6);
map.put(null,4);
map.put(null,5);
map.put("/2010/10/8/19/766105208164534487403083462088868610/1.0",5);
map.put("/2010/6/13/19/766105207591607338053525248042284292/1.0",9);
System.out.println(map);
System.out.println(map.containsKey("a"));
System.out.println(map.containsValue(1));
//remove还存在输入key和value进行移除的事情呢?
map.remove("a",1);
map.remove("/2010/6/13/19/766105207591607338053525248042284292/1.0");
System.out.println(map);
}
}
2.5.LinkedList(单双向链表):singleLinked、doubleLinked
package algorithm.b01Collection;
import lombok.Data;
public class a05LinkedList {
public static void main(String[] args) {
System.out.println("singleLinked 测试");
singleLinked();
System.out.println("doubleLinked 测试");
doubleLinked();
}
@Data
static class Node{
private int num;
private Node next;
private Node prx;
public Node(int num){
this.num = num;
}
public Node(){}
}
public static void singleLinked(){
Node node = new Node();
node.num = 1;
node.next = new Node(2);
node.next.next = new Node(3);
node.next.next.next = new Node(4);
while (node != null){
System.out.println(node.num);
node = node.next;
}
}
public static void doubleLinked(){
Node node = new Node();
node.num = 1;
node.prx = null;
node.next = new Node(2);
node.next.prx = node;
node.next.next = new Node(3);
node.next.next.prx = node.next;
node.next.next.next = new Node(4);
node.next.next.next.prx = node.next.next;
while (node != null){
if(node.prx != null){
System.out.println("上一个节点的值:"+node.prx.num);
}else {
System.out.println("上一个节点为null");
}
System.out.println("当前节点的值:"+node.num);
node = node.next;
}
}
}
四、总结扩展
1.有了本次的总结学习目前为止达到了上面的目标
2.针对于不同数据结构的底层原理和源码分析将会在刷一段时间的力扣题(100题)之后再做总结