Does the LinkedList Java Core Collection use addLast?

I have a Linked List object called ll . I have added some data to this object. Assume the following:

 LinkedList ll = new LinkedList(); ll.add("Mohan"); ll.add("Rajesh"); ll.addFirst("Kumar"); ll.addLast("Nammu"); ll.add("Divyesh"); 

My question is: if I already inserted 100 data when I use the addFirst() method, then this data will be inserted first, but for the addLast() method the same functionality should happen, which means that if I use addLast() it should also insert data, but if I add more data after the addLast() method, so that the data only finally inserts what the addLast() method addLast() instead, can we use only the add () method only?

+6
source share
4 answers

LinkedList class implements both Deque and Queue . It inherits the add(E) method from Queue and addLast(E) from Deque . Both methods have the same functionality.

+15
source

How the states of javadoc, addLast and add equivalent: addLast (E e)

+3
source

javap java.util.LinkedList

Compiled from "LinkedList.java"

public class java.util.LinkedList extends java.util.AbstractSequentialList implements java.util.List, java.util.Deque, java.lang.Cloneable, java.io.Serializable {

you can see here that LinkedList extends AbstractSequentialList and implements a list interface, adds and removes methods from this interface, you can check it with the following command

javap java.util.List

Compiled from "List.java"

open interface java.util.List extends java.util.Collection {

public abstract int size ();

public abstract boolean isEmpty ();

public abstract boolean contains (java.lang.Object);

public abstract java.util.Iterator iterator ();

public abstract java.lang.Object [] toArray ();

public annotation T [] toArray (T []);

public abstract boolean add (E);

public abstract boolean remove (java.lang.Object);

public abstract boolean containsAll (java.util.Collection);

public abstract boolean addAll (java.util.Collection);

public abstract boolean addAll (int, java.util.Collection);

public abstract boolean removeAll (java.util.Collection);

public abstract boolean retainAll (java.util.Collection);

public void replaceAll (java.util.function.UnaryOperator);

public void sort (java.util.Comparator);

public abstract void clear ();

public abstract boolean equals (java.lang.Object);

public abstract int hashCode ();

public abstract E get (int);

public abstract E set (int, E);

public abstract void add (int, E);

public annotation E remove (int);

public abstract int indexOf (java.lang.Object);

public abstract int lastIndexOf (java.lang.Object);

public abstract java.util.ListIterator listIterator ();

public abstract java.util.ListIterator listIterator (int);

public abstract java.util.List subList (int, int);

public java.util.Spliterator spliterator ();

}

in fact, the add and addLast method does the same, but because of the above reason. He has both methods.

I think I gave an answer. feel free to comment ...

+1
source

Method: boolean add (E e) and adddLast (E) are equivalent.

Shown Here Javadocs

0
source

Source: https://habr.com/ru/post/956408/