当前位置:网站首页>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");
}
}
边栏推荐
猜你喜欢

Ansi/ul 94 class 5-V vertical combustion test

Promise从认识到使用

德国AgBB VoC有害物质测试

German agbb VOC hazardous substances test

【DesignMode】工厂模式 (factory pattern)

Four tips tell you how to use SMS to promote business sales?
![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

假期安全不放假,VR交通让孩子安全出行|广州华锐视点

法国A+ 法国VOC标签最高环保级别

Pyth-Solana链上联通现实的桥梁
随机推荐
「经验」爬虫在工作中的实战应用『理论篇』
详解kubernetes备份恢复利器 Velero | 深入了解Carina系列第三期
ROS advertisement data publishing tips - latch configuration
全技术栈、全场景、全角色云原生系列培训重磅首发,助力企业打造硬核云原生技术团队
成长一夏 挑战赛来袭 专属社区福利来袭~免费获得CSDN定制T恤衫
Go语言学习教程(十)
Ditto设置全局仅粘贴文本快捷键
MQ的选择(2022.5.9-5.15)
设计电商秒杀系统
Build graphql service based on Actix, async graphql, rbatis, pgsql/mysql (4) - change service
一文详解|Go 分布式链路追踪实现原理
「杂谈」如何改善数据分析工作中的三大被动局面
How JS correctly clears all child elements under an element
MySQL modify data type_ MySQL modify field type [easy to understand]
Task04:集合运算-表的加减法和join等--天池龙珠计划SQL训练营学习笔记
Connect to lab server
阿里天池SQL训练营学习笔记5
Business Intelligence BI and business management decision-making thinking 4: business cost analysis
Nodejs 安装与介绍
Construction and practice of full stack code test coverage and use case discovery system