Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Queue and LinkedList of Java collection

2025-04-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Java collection of Queue and LinkedList how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java collection Queue and LinkedList how to use" it!

Overview of LinkedList

LinkedList implements the List interface like ArrayList, except that ArrayList is the implementation of a variable size array of the List interface, and LinkedList is the implementation of the List interface linked list. The linked list-based implementation makes LinkedList better than ArrayList in inserting and deleting, while random access is inferior to ArrayList.

LinkedList implements all optional list operations and allows all elements including null.

In addition to implementing the List interface, the LinkedList class provides a unified naming method for the get, remove, and insert elements at the beginning and end of the list. These operations allow the linked list to be used as a stack, queue, or double-ended queue.

This class implements the Deque interface, providing first-in, first-out queue operations for add and poll, as well as other stack and double-end queue operations.

All operations are performed according to the needs of the doubly linked list. Indexing in a list traverses the list from the beginning or end (near the end of the specified index).

At the same time, like ArrayList, this implementation is not synchronous.

Source code analysis definition

First, let's look at the definition of LinkedList:

Public class LinkedList extends AbstractSequentialList implements List, Deque, Cloneable, java.io.Serializable We can clearly see from this code that LinkedList inherits AbstractSequentialList and implements List, Deque, Cloneable, and Serializable. AbstractSequentialList provides a backbone implementation of the List interface, minimizing the work required to implement this interface supported by "continuous access" data stores, such as linked lists, thereby reducing the complexity of implementing the List interface. Deque is a linear collection that supports inserting and removing elements at both ends and defines the operation of a double-ended queue. Attribute

Two basic properties, size and header, are provided in LinkedList.

Private transient Entryheader = new Entry (null, null, null)

Private transient int size = 0

Where size represents the size of the LinkedList, header represents the header of the linked list, and Entry is the node object.

Private static class Entry {E element; / / element node Entry next; / / next element Entry previous; / / previous element Entry (E element, Entry next, Entry previous) {this.element = element; this.next = next; this.previous = previous;}} above is the source code of the Entry object, and Entry is the inner class of LinkedList, which defines the stored element. The previous element and the latter element of the element, which is a typical way of defining a two-way linked list. Construction method

LinkedList provides two construction methods: LinkedList () and LinkedList (Collection)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report