当前位置:网站首页>Simple usage of LinkedList (2022.6.13-6.19)
Simple usage of LinkedList (2022.6.13-6.19)
2022-06-30 19:34:00 【Water is water】
package com.reviewDemo;
import java.util.Iterator;
import java.util.LinkedList;
/**
* @Author: Bsx
* @Date: 2022/6/14
* @Description: LinkedList Use Demo
*/
public class Test_002 {
public static void main(String[] srgs) {
// Create and store int Type of linkedList
LinkedList<Integer> linkedList = new LinkedList<>();
/************************** linkedList Basic operation ************************/
linkedList.addFirst(0); // Add elements to the beginning of the list
linkedList.add(1); // Add elements at the end of the list
linkedList.add(2, 2); // Add the element in the specified position
linkedList.addLast(3); // Add elements to the end of the list
System.out.println("LinkedList( Direct output ): " + linkedList);
System.out.println("getFirst() Get the first element : " + linkedList.getFirst()); // Returns the first element of this list
System.out.println("getLast() Get the last element : " + linkedList.getLast()); // Return the last element of this list
System.out.println("removeFirst() Delete the first element and return : " + linkedList.removeFirst()); // Remove and return the first element of this list
System.out.println("removeLast() Delete the last element and return : " + linkedList.removeLast()); // Remove and return the last element of this list
System.out.println("After remove:" + linkedList);
System.out.println("contains() Method to determine whether the list contains 1 This element :" + linkedList.contains(1)); // Determine that this list contains the specified element , If it is , Then return to true
System.out.println(" The linkedList Size : " + linkedList.size()); // Returns the number of elements in this list
/************************** Location access operations ************************/
System.out.println("-----------------------------------------");
linkedList.set(1, 3); // Replace the element at the specified location in this list with the specified element
System.out.println("After set(1, 3):" + linkedList);
System.out.println("get(1) Get the designated location ( Here for 1) The elements of : " + linkedList.get(1)); // Returns the element in this list at the anchor
/************************** Search operation ************************/
System.out.println("-----------------------------------------");
linkedList.add(3);
System.out.println("indexOf(3): " + linkedList.indexOf(3)); // Returns the index of the specified element that first appears in this list
System.out.println("lastIndexOf(3): " + linkedList.lastIndexOf(3));// Returns the index of the last specified element in this list
/************************** Queue operation ************************/
System.out.println("-----------------------------------------");
System.out.println("peek(): " + linkedList.peek()); // Gets but does not remove the header of this list
System.out.println("element(): " + linkedList.element()); // Gets but does not remove the header of this list
linkedList.poll(); // Get and remove the header of this list
System.out.println("After poll():" + linkedList);
linkedList.remove();
System.out.println("After remove():" + linkedList); // Get and remove the header of this list
linkedList.offer(4);
System.out.println("After offer(4):" + linkedList); // Add the specified element to the end of this list
/************************** Deque operation ************************/
System.out.println("-----------------------------------------");
linkedList.offerFirst(2); // Insert the specified element at the beginning of this list
System.out.println("After offerFirst(2):" + linkedList);
linkedList.offerLast(5); // Insert the specified element at the end of this list
System.out.println("After offerLast(5):" + linkedList);
System.out.println("peekFirst(): " + linkedList.peekFirst()); // Gets but does not remove the first element of this list
System.out.println("peekLast(): " + linkedList.peekLast()); // Gets but does not remove the last element of this list
linkedList.pollFirst(); // Get and remove the first element of this list
System.out.println("After pollFirst():" + linkedList);
linkedList.pollLast(); // Get and remove the last element of this list
System.out.println("After pollLast():" + linkedList);
linkedList.push(2); // Push elements onto the stack represented by this list ( Insert into the head of the list )
System.out.println("After push(2):" + linkedList);
linkedList.pop(); // Pop an element from the stack represented by this list ( Get and remove the first element of the list )
System.out.println("After pop():" + linkedList);
linkedList.add(3);
linkedList.removeFirstOccurrence(3); // Remove the first occurrence of the specified element... From this list ( Traversing the list from head to tail )
System.out.println("After removeFirstOccurrence(3):" + linkedList);
linkedList.removeLastOccurrence(3); // Remove the last occurrence of the specified element... From this list ( Traversing the list from tail to head )
System.out.println("After removeFirstOccurrence(3):" + linkedList);
/************************** Traversal operation ************************/
System.out.println("-----------------------------------------");
linkedList.clear();
for (int i = 0; i < 100000; i++) {
linkedList.add(i);
}
// Iterator traversal
long start = System.currentTimeMillis();
Iterator<Integer> iterator = linkedList.iterator();
while (iterator.hasNext()) {
iterator.next();
}
long end = System.currentTimeMillis();
System.out.println("Iterator:" + (end - start) + " ms");
// Sequential traversal ( Random ergodicity )
start = System.currentTimeMillis();
for (int i = 0; i < linkedList.size(); i++) {
linkedList.get(i);
}
end = System.currentTimeMillis();
System.out.println("for:" + (end - start) + " ms");
// Another kind for Loop traversal
start = System.currentTimeMillis();
for (Integer i : linkedList)
;
end = System.currentTimeMillis();
System.out.println("for2:" + (end - start) + " ms");
// adopt pollFirst() or pollLast() To traverse LinkedList
LinkedList<Integer> temp1 = new LinkedList<>();
temp1.addAll(linkedList);
start = System.currentTimeMillis();
while (temp1.size() != 0) {
temp1.pollFirst();
}
end = System.currentTimeMillis();
System.out.println("pollFirst() or pollLast():" + (end - start) + " ms");
// adopt removeFirst() or removeLast() To traverse LinkedList
LinkedList<Integer> temp2 = new LinkedList<>();
temp2.addAll(linkedList);
start = System.currentTimeMillis();
while (temp2.size() != 0) {
temp2.removeFirst();
}
end = System.currentTimeMillis();
System.out.println("removeFirst() or removeLast():" + (end - start) + " ms");
}
}
边栏推荐
- 项目配置了eslint,编辑器没有关闭eslint功能的情况下,eslint没有生效
- 事件队列、微任务与宏任务的理解和面试题
- Redis beginner to master 01
- 【DesignMode】工厂模式 (factory pattern)
- MySQL recursion
- Whether the SQL is indexed
- MQ的选择(2022.5.9-5.15)
- Pyth-Solana链上联通现实的桥梁
- Huaxing Securities: kitex practice under the original hybrid Cloud Architecture
- Evolution of screen display technology
猜你喜欢
随机推荐
微信小程序快速入门 --项目介绍
Develop those things: how to add text watermarks to videos?
Neon optimization 2: arm optimization high frequency Instruction Summary
How to use xUnit framework to maintain test cases?
力扣------统计包含给定前缀的字符串
Why do more and more people choose cloud rendering?
一文详解|Go 分布式链路追踪实现原理
JS string interception method summary
Browser window switch activation event visibilitychange
Video content production and consumption innovation
Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched
嵌入式软件开发新趋势:DevOps
阿里天池SQL训练营学习笔记5
What securities dealers recommend? In addition, is it safe to open a mobile account?
虚拟主机什么时候适合更换成云主机?
Evolution of screen display technology
Coding officially entered Tencent conference application market!
如何使用物联网低代码平台进行服务管理?
20220607 fell below the recommended retail price, and the GPU market is moving towards oversupply
Understanding of event queue, micro task and macro task and interview questions







