What is LinkedList ?
LinkedList is a doubly-linked list implementation of the List and Deque interfaces.
How to perform Basic Operations in LinkedList?
Basic Operations are:
- creating object for LinkedList
- adding objects into LinkedList
- searching an object in LinkedList
- whether it is listed under this LinkedList instance or not
- checking whether the LinkedList is empty or not
- Get the size of the LinkedList
Basic Operations
import java.util.LinkedList; public class MyBasicOperations { public static void main(String a[]){ LinkedListTry it Yourselfll = new LinkedList (); ll.add("Orange"); ll.add("Apple"); ll.add("Grape"); ll.add("Banana"); System.out.println(ll); System.out.println("Size of the linked list: "+ll.size()); System.out.println("Is LinkedList empty? "+ll.isEmpty()); System.out.println("Does LinkedList contains 'Grape'? "+ll.contains("Grape")); } }
How to add all elements of a list to LinkedList?
Example
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class MyLinkedListNewCollection { public static void main(String a[]){ LinkedListTry it Yourselfarrl = new LinkedList (); //adding elements to the end arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("Actual LinkedList:"+arrl); List list = new ArrayList (); list.add("one"); list.add("two"); arrl.addAll(list); System.out.println("After Copy: "+arrl); } }
How to reverse LinkedList content?
We can reverse the content by calling Collections.reverse() method.
We have to pass LinkedList instance to this method, which reverses the content.
Example
import java.util.Collections; import java.util.LinkedList; public class MyLinkedListReverse { public static void main(String a[]){ LinkedListTry it Yourselflist = new LinkedList (); list.add("Java"); list.add("Cric"); list.add("Play"); list.add("Watch"); list.add("Glass"); Collections.reverse(list); System.out.println("Results after reverse operation:"); for(String str: list){ System.out.println(str); } } }
How to swap two elements in a LinkedList?
By calling Collections.swap() method we can swap two elements of the LinkedList.
We have to pass the indexes which we need to swap.
Example
import java.util.Collections; import java.util.LinkedList; public class MyLinkedListSwap { public static void main(String a[]){ LinkedListTry it Yourselflist = new LinkedList (); list.add("Java"); list.add("Cric"); list.add("Play"); list.add("Watch"); list.add("Glass"); list.add("Movie"); list.add("Girl"); Collections.swap(list, 2, 5); System.out.println("Results after swap operation:"); for(String str: list){ System.out.println(str); } } }
How to read first element from LinkedList?
LinkedList provides few methods to read first element, those methods are:
element(): Retrieves, but does not remove, the head (first element) of this list.
getFirst(): Returns the first element in this list.
peek(): Retrieves, but does not remove, the head (first element) of this list.
peekFirst(): Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.
Example
import java.util.LinkedList; public class MyFirstElement { public static void main(String a[]){ LinkedListTry it Yourselfarrl = new LinkedList (); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("First Element: "+arrl.element()); System.out.println("First Element: "+arrl.getFirst()); System.out.println("First Element: "+arrl.peek()); System.out.println("First Element: "+arrl.peekFirst()); } }
How to add element at first position in LinkedList?
LinkedList provides few methods to add element at first position, those methods are:
addFirst(): Inserts the specified element at the beginning of this list.
offerFirst(): Inserts the specified element at the front of this list.
Example
import java.util.LinkedList; public class MyAddFirst { public static void main(String a[]){ LinkedListTry it Yourselfarrl = new LinkedList (); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println(arrl); System.out.println("Adding element at first position..."); arrl.addFirst("I am first"); System.out.println(arrl); System.out.println("Adding element at first position..."); arrl.offerFirst("I am first - 2"); System.out.println(arrl); } }
How to add element at last position in LinkedList?
LinkedList provides few methods to add element at last position, those methods are:
addLast(): Appends the specified element to the end of this list.
offerLast(): Inserts the specified element at the end of this list.
offer(): Adds the specified element as the tail (last element) of this list.
Example
import java.util.LinkedList; public class MyAddLast { public static void main(String a[]){ LinkedListTry it Yourselfarrl = new LinkedList (); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.addLast("I am last"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.offerLast("I am last - 1"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.offer("I am last - 2"); System.out.println(arrl); } }
Solve this!!
#20080,20081,20082,20083- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling