![]() Return value - index value ListIterator listiterator() Returns an iterator over elements in the list ListIterator listiterator(int index) Returns an iterator over elements in the list starting from the specified position index - start position from which it needs to return the iterator. Return value - index value Boolean isEmpty() Checks if the list is empty or not Return value - true if list contains no values Iterator iterator() Retrieves the iterator of list in sequence Return value - Iterator int lastIndexOf(Object o) Retrieves the last occurrence of the specified object o - Element to be identified ![]() Return value - The element at the specified index Object getFirst() Returns the first element(head) in the list Object getLast() Returns the last element(tail) in the list int indexOf(Object o) Fetches the index of the first occurrence of the specified element o - The element to be identified Boolean contains(Object o) Checks if the list contains the specified element Return value - true if the list contains the element Boolean containsAll(Collection c) Checks if the list contains all the elements in the collection Return value - true if the list contains all the elements Object clone() Returns a shallow copy of the LinkedList Iterator descendingIterator() Returns an iterator over elements in the deque in the reverse order Object element() Returns the first element(head) in the list Boolean equals(Object o) Compares if the list contains all the specified elements in the exact order Return value - true if object elements match with the list Object getIndex(int index) Retrieves the element at the specified index index - the index at which the element that needs to be retrieved Return value - True void addFirst(Element e) Inserts an element at the beginning of the linked list e - the element to be inserted void addLast(Element e) Inserts an element at the end of the list e - the element to be inserted void clear() Clears all the elements in the list. If the index already contains element, it is subsequently shifted to the right index - index at which the elements needs to be added Return value - true Boolean addAll(int index, Collection c) Adds a collection of elements at the specified index. If the index already contains an element, it is shifted to the right index- the index at which the element needs to be insertedĮ - the element which needs to be inserted Boolean addAll(Collection c) Adds a collection of specified elements to the list. Return value - True void add(int index, Element e) Adds the element to the specified index. Method Description Parameter Boolean add(Element e) Adds the specified element to the end of list. Java LinkedList Methodsīelow is the list of commonly used methods for a Java LinkedList. Java LinkedList Hierarchyīelow are the 2 constructors which we can use in Java LinkedList: LinkedList Constructor Description LinkedList() Creates an empty LinkedList LinkedList(Collection c) Creates a LinkedList with all elements of the Collection c. The first element in the LinkedList will have null in the previous node reference and the last element in the LinkedList will have null in the next node reference. You can understand this clearly from the pictorial representation. This means each node contains 3 parts: the first part has a reference to the previous node, the second part contains the value of the element and the third part contains the reference to the next node. Working of a LinkedList in JavaĪs we discussed, LinkedList uses a double LinkedList. It is faster than arrays since there is no shifting of elements during insertion. ![]() We name the first element in the list as head and the last element as a tail. We call each element in the list as a node. LinkedList contains a collection of nodes and implements a linear data structure. It is called a doubly linked list since it contains a link to the previous node as well as the next successive node. It extends the AbstractList class and implements the List and Deque interfaces. ![]() Java LinkedList is a doubly-linked list that can store any type of data. LinkedList as Deque and Queue Java LinkedList.Different ways to iterate through a LinkedList in Java.Example: Empty or clear the LinkedList in Java.Example: Other methods of the LinkedList.Example: Create and Add elements to the list. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |