Linked List Library In Java

0 views
Skip to first unread message

Brian Bezdicek

unread,
Aug 4, 2024, 9:50:20 PM8/4/24
to geldgagangkitt
TheLinkedList class has all of the same methods as the ArrayList class becausethey both implement the List interface. This means that you can add items, changeitems, remove items and clear the list in the same way.

The ArrayList class has a regular array inside it. When an element is added, it is placedinto the array. If the array is not big enough, a new, larger array is created to replace theold one and the old one is removed.


The LinkedList stores its items in "containers." The list has a link to the first containerand each container has a link to the next container in the list. To add an element to the list,the element is placed into a new container and that container is linked to one of the othercontainers in the list.


I've been writing my linked list data structure from scratch as a student for projects/assignments and I was wondering in the "real-world" does a developer have to write their own linked list DS or use any of the already provided linked list object in the Java docs. Which is better & in what scenario?


A custom LinkedList implementation will very likely be more efficient because it allows you to make optimizations for your needs, however the JDK's LinkedList will always be preferable for its re-usability and maintainability benefits:


Thus, the "technical" answer is: for production work you have to assess cost/benefit of both of those alternatives; and then you decide what to do. But especially for classes that come out of the Java standard libraries, you should prefer re-use. You depend on those libraries anyway; so there is no point in not using them.


B) cost of doing so: of course, the direct cost for creating and maintaining your own solution over time (esp. maintenance is often heavily underestimated! And of course, all those points that "castle" listed in his answer.


All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index. Note that this implementation is not synchronized. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list: List list = Collections.synchronizedList(new LinkedList(...)); The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs. This class is a member of the Java Collections Framework.Since:1.2See Also:List, ArrayList, Serialized FormField SummaryFields inherited from class java.util.AbstractListmodCountConstructor SummaryConstructors Constructor and DescriptionLinkedList()Constructs an empty list.LinkedList(Collection


I have a solution coded but I'm not sure if this is working properly. I don't have any compile errors that are coming up but wanted a second opinion if there is a better way to do this or if corrections should be made.


As I'm seeing, that is a double linked list. As is suggested in comments, avoid using an index as part of the node itself, the index is part of the List, because the list controls the way each node is traversed to perform any operation (add, remove, find, etc)


The main reason for not using the get method for retrieve each node is that get operation search for the node from the beginning each time you call it. It is better to get the start node and start traversing the nodes from there.


I would like to create a circular/cyclic linked list where the tail of the list would point back to the head of the list. So can I use java.util.LinkedList and modify the tail node after creation of the list to make it circular/cyclic? If so, can you show me some code on how that would happen?


java.util.LinkedList is one of the Collections datatypes. The purpose of Collections is to provide utility structures, not bothering the programmer to worry about their internal implementation. If you must have internals that work in a certain way, and the java.util ones do not guarantee that is how they work, then they are not for you.


Then store a ListNode head, and make sure prev of head points to the "end" of the list, and next of the "end" points back to head. Honestly, though, there's little difference between a bidirectionally linked list keeping a tail pointer and a circular linked list.


There is a project to reproduce some core Java functionality on kotlin-multiplatform here: GitHub - kotlin-extra-library/kotlin-extlib: Kotlin community common multiplatform library. But when it comes to LinkedList I agree with @elizarov and @mtimmerm. It is just not usable. Fast injection of elements inside the list (the only think LinkedList is good for) is almost never used and could be done by other means.


Linked lists are extremely efficient for sequentially accessing elements from one end or for adding elements to one end. They are not for accessing elements by index or for some bulk operations like reversing. But they are never dangerous. Only programmers can be dangerous.


I decided not to write a reply about it. But in general linked structure will be slower because of additional memory indirection. I think that modern JVM could optimize it away, but it will be definitely not faster.


@sandeep549 I cannot agree with you. All deques (incl. queues and stack) are much faster when they are based on arrays. You should never use linked lists for that purpose, especially if you do competitive programming where performance is important.


But I see (checked some benchmarks online), despite of this resize overhead in array based implementation, ArrayDeque is faster for stack and queue operations, even when there is no iteration involved at all.


Thanks for this interesting discussion. Only thing I would like to say is that I subscribe to the notion that Deque / Queue / Stack interfaces would be nice to have in stdlib - although they are so trivial we can implement them ourselves.


Java LinkedList is an implementation of the List and Deque interfaces. It is one of the frequently used List implementation class. It extends AbstractSequentialList and implements List and Deque interfaces. It is an ordered collection and supports duplicate elements. It stores elements in Insertion order. It supports adding null elements. It supports index based operations. If you want to learn more about List basics, please go through this post: Java List.


As we know, Java LinkedList is one the List implementation class. It also implements Deque. As shown in class diagram below, it does NOT extends directly from AbstractList class. It extends AbstractSequentialList class.


Here we have created a LinkedList object and added 4 items. As we discussed LinkedList.size() method is used to get the number of elements in the list. NOTE:- Without using Generics, Java LinkedList supports Heterogeneous elements. However, it is not recommended to use Collections without Generics. Let us explore Java Generics Advantages and usage in the coming section with one simple example.


In this section, we will discuss on how to use Generics with Java LinkedList. As we know, Java Generics are useful to write Type Safety programming and do Stronger type checks at compile time. They are also useful to eliminate the casting overhead. Example:-


Here we will explore how a LinkedList object works as a Deque. We use these operations to implement Queues or Stacks. We will discuss how a Stack or Queues works in-depth in my coming posts. Example:-


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the report an issue button at the bottom of the tutorial.


instead of using 2 for loops to rotate whole array left for say 4 times (which is of course have time-complexity issue takes of compiler time ,so its not a good solution) is it good to use linked list for that ,if yes then why ,please tell


Hi Rambabu, Here I would like to mention a typo error in the bellow line. Java LinkedList interface is a member of the Java Collections Framework. LinkedList is not interface, it is a class. it will really confuse the readers, if they are new to Java.


really thanks for this post . As per you . It does not implement RandomAccess interface. So we can access elements in sequential order only. It does not support accessing elements randomly. but in the next paragraph i can see it supports get and set , if it supports get and set then how it cant support random access , r


In Java, LinkedList is a generic class that extends the AbstractSequentialList and implements List, Queue, and Deque interfaces. It is a part of the Java Collection API Library. It basically is an implementation of a type of linked list data structure that facilitates the storage of elements. The contents of this storage can grow and shrink at run time as per the requirement. Quite visibly, it is not very different from the other List classes, such as ArrayList. But, the point of difference is in how the list is maintained at the core. The article is an attempt to delineate the concept behind the data structure involved and its implementation in Java in a terse manner.

3a8082e126
Reply all
Reply to author
Forward
0 new messages