当前位置:网站首页>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");
}
}
边栏推荐
- How to configure webrtc video stream format playback in the new version of easygbs?
- await和async
- Task04: set operation - addition and subtraction of tables, join, etc. - learning notes of Tianchi Longzhu project SQL training camp
- 美国服务器租用和托管服务哪个好?
- 「杂谈」如何改善数据分析工作中的三大被动局面
- Force deduction ----- count the string containing the given prefix
- Safe holidays without holidays, VR traffic makes children travel safely | Guangzhou Sinovel viewpoint
- ArcGIS no plug-in load (no offset) day map
- MQ的选择(2022.5.9-5.15)
- 在广州的朋友,有机会可以参加下
猜你喜欢

ANSI/UL 94 5-V级垂直燃烧试验

在广州的朋友,有机会可以参加下

设计电商秒杀系统

20220607 fell below the recommended retail price, and the GPU market is moving towards oversupply

Evolution of screen display technology

Construction and practice of full stack code test coverage and use case discovery system

How to use the low code platform of the Internet of things for service management?

Video content production and consumption innovation

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
![20220528 [talk about fake chips] those who are greedy for cheap often suffer heavy losses. Take stock of those fake memory cards and solid state drives](/img/96/8e195536127b90d4eec54294371cad.png)
20220528 [talk about fake chips] those who are greedy for cheap often suffer heavy losses. Take stock of those fake memory cards and solid state drives
随机推荐
Develop those things: how to add text watermarks to videos?
Final chapter of binary tree
商业智能BI与业务管理决策思维之四:业务成本分析
【UML】UML类图
全技术栈、全场景、全角色云原生系列培训重磅首发,助力企业打造硬核云原生技术团队
ROS advertisement data publishing tips - latch configuration
基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务
Promise从认识到使用
20220607 fell below the recommended retail price, and the GPU market is moving towards oversupply
sql是否走索引
测试必备工具 —— Postman实战教程
连接实验室服务器
码蹄集 - MT3435 · 赋值 - 二分图问题 - 图文讲解
VoIP Push 在海外音视频业务中的应用
简述机器学习中的特征工程
[JetsonNano] [教程] [入门系列] [一] 如何开启VNC共享
如何利用 xUnit 框架对测试用例进行维护?
Whether the SQL is indexed
设计电商秒杀系统
Kalman filter -- Derivation from Gaussian fusion