栈与队列
栈与队列和String与stringBuilder演示
package com.m.list_impl;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MyTest {
public static void main(String[] args) {
// /**
// * Stack
// * 栈与队列
// */
// Stack<String> stack = new Stack<>();
// stack.add("JavaSE");
// stack.add("ConcurrentException");
// stack.add("JavaME");
// stack.add("JavaEE");
//
// System.out.println(stack.peek());
// System.out.println(stack.pop());
// System.out.println(stack.empty());
// System.out.println(stack);
//
// Queue<String> queue = new LinkedList<>();
// queue.add("JavaSE");
// queue.add("ConcurrentException");
// queue.add("JavaME");
// queue.add("JavaEE");
// System.out.println(queue.peek());
// System.out.println(((LinkedList<String>) queue).pop());
String str = "SpringIoc";
System.out.println(str.hashCode());
str = "SpringAop";
System.out.println(str.hashCode());
Lock lock = new ReentrantLock();
StringBuilder stringBuilder = new StringBuilder("SpringIoc");
StringBuffer stringBuffer = new StringBuffer();
// System.out.println(stringBuilder.hashCode());
//
// stringBuilder.replace(0,stringBuilder.length(),"SpringAop");
// System.out.println(stringBuilder.hashCode());
for (int i = 0; i < 20; i++) {
final int temp = i;
new Thread(()->{
// synchronized (Object.class) {
stringBuilder.append(String.valueOf(temp)+"、");
System.out.println(stringBuilder);
System.out.println(stringBuilder.hashCode());
// }
}).start();
}
}
}
LinkedList底层原理实现
1.底层
1.LinkedList的底层是通过链表来实现的。
2.链表的单元
链表的单元是节点(Node)
链表是由多个节点构成,每个节点都包含三个部分,头部指向上一个节点,中部指向该节点,尾部指向下一个节点
双向链表:
源码实现
节点类
package com.m.list_impl.impl;
public class Node {
private Node prev;
private Object item;
private Node next;
public Node getPrev() {
return prev;
}
public void setPrev(Node prev) {
this.prev = prev;
}
public Object getItem() {
return item;
}
public void setItem(Object item) {
this.item = item;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
实现类
package com.m.list_impl.impl;
import java.util.*;
public class MyLinkedList1 {
private Node first; //起点
private Node last; //终点
private int size; //个数
//-----------业务方法----------------
public Object getFirst() {
return first.getItem();
}
public Object getLast() {
return last.getItem();
}
public void addFirst(Object object) {
Node node = new Node();
//没有元素
if (first == null) {
node.setItem(object);
first = node;
last = node;
node.setPrev(node);
node.setNext(node);
} else {
node.setItem(object);
first.setPrev(node);
node.setNext(first);
first = node;
}
this.size++;
}
public void addLast(Object object) {
Node node = new Node();
//没有元素
if (last == null) {
node.setItem(object);
first = node;
last = node;
node.setPrev(node);
node.setNext(node);
} else {
node.setItem(object);
last.setNext(node);
node.setPrev(last);
last = node;
}
this.size++;
}
//删除开头一个元素
public Object removeFirst() {
final Node f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
//删除末尾一个元素
public Object removeLast() {
final Node l = this.last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
public void add(Object o) {
addLast(o);
}
public int size() {
return this.size;
}
public boolean isEmpty() {
return this.size > 0 ? true : false;
}
public Object get(int index) {
checkElementIndex(index);
return getNode(index).getItem();
}
//-----------工具方法----------------
private Object unlinkLast(Node l) {
final Object element = l.getItem();
final Node prev = l.getPrev();
l.setItem(null);
l.setPrev(null);
this.last = prev;
if (prev == null) {
first = null;
} else {
prev.setNext(null);
}
size--;
return element;
}
private Object unlinkFirst(Node f) {
final Object element = f.getItem();
final Node next = f.getNext();
f.setItem(null);
f.setNext(null);
first = next;
if (next == null) {
last = null;
} else {
next.setPrev(null);
}
size--;
return element;
}
Node getNode(int index) {
checkElementIndex(index);
Node f = first;
for (int i = 0; i < index; i++) {
f = f.getNext();
}
return f;
}
/**
* Tells if the argument is the index of an existing element.
*/
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
/**
* Tells if the argument is the index of a valid position for an
* iterator or an add operation.
*/
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: " + index + ", Size: " + size;
}
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
}
测试类
package com.m.list_impl;
import com.m.list_impl.impl.MyLinkedList1;
import java.util.LinkedList;
public class MyArrayList {
public static void main(String[] args) {
MyLinkedList1 myLinkedList1 = new MyLinkedList1();
myLinkedList1.addFirst("JavaSE");
System.out.println(myLinkedList1.isEmpty());
// myLinkedList1.addLast("SpringIoc");
//
// myLinkedList1.addFirst("SpringAop");
//
// for (int i = 0; i < myLinkedList1.size(); i++) {
// System.out.println(myLinkedList1.get(i));
// }
// myLinkedList1.removeFirst();
// System.out.println(myLinkedList1.getFirst());
}
}