importjava.util.LinkedList;classKW{publicstaticvoidmain(String[]args){//LinkedList class implements the List interface and Deque -> Queue interfaceLinkedList<String>ll=newLinkedList<>();ll.add("Apple");ll.add("Orange");ll.add("Mango");ll.add("Grapes");ll.add("Cherry");ll.add("Apple");System.out.println(ll);System.out.println("Size: "+ll.size());}}
importjava.util.LinkedList;classKW{publicstaticvoidmain(String[]args){//LinkedList class implements the List interface and Deque -> Queue interfaceLinkedList<String>ll=newLinkedList<>();ll.add("Apple");ll.add("Orange");ll.add("Mango");ll.add("Grapes");ll.add("Cherry");ll.add("Apple");for(Strings:ll){System.out.println(s);}System.out.println("\n"+ll.get(3));System.out.println(ll.contains("Apple"));}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Apple
Orange
Mango
Grapes
Cherry
Apple
Grapes
true
kodingwindow@kw:~$
Java LinkedList: addFirst() and addLast() methods
KW.java
importjava.util.LinkedList;classKW{publicstaticvoidmain(String[]args){//LinkedList class implements the List interface and Deque -> Queue interfaceLinkedList<String>ll=newLinkedList<>();ll.add("Apple");ll.add("Orange");ll.add("Mango");ll.add("Grapes");ll.add("Cherry");for(Strings:ll){System.out.println(s);}ll.addFirst("Strawberry");ll.addLast("Apple");System.out.println("\n"+ll);}}
importjava.util.LinkedList;classKW{publicstaticvoidmain(String[]args){//LinkedList class implements the List interface and Deque -> Queue interfaceLinkedList<String>ll=newLinkedList<>();ll.add("Apple");ll.add("Orange");ll.add("Grapes");ll.add("Cherry");ll.add("Apple");ll.add("Mango");System.out.println(ll);System.out.println("First element: "+ll.getFirst());System.out.println("Last element: "+ll.getLast());}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
[Apple, Orange, Grapes, Cherry, Apple, Mango]
First element: Apple
Last element: Mango
kodingwindow@kw:~$
Java LinkedList: indexOf() and lastIndexOf() methods
KW.java
importjava.util.LinkedList;classKW{publicstaticvoidmain(String[]args){//LinkedList class implements the List interface and Deque -> Queue interfaceLinkedList<String>ll=newLinkedList<>();ll.add("Apple");ll.add("Orange");ll.add("Mango");ll.add("Grapes");ll.add("Cherry");ll.add("Apple");System.out.println("indexOf Apple: "+ll.indexOf("Apple"));System.out.println("lastIndexOf Apple: "+ll.lastIndexOf("Apple"));}}
Java LinkedList: addAll() and containsAll() methods
KW.java
importjava.util.LinkedList;classKW{publicstaticvoidmain(String[]args){//LinkedList class implements the List interface and Deque -> Queue interfaceLinkedList<String>ll1=newLinkedList<>();ll1.add("Apple");ll1.add("Orange");ll1.add("Mango");System.out.println(ll1);LinkedList<String>ll2=newLinkedList<>();ll2.add("Grapes");ll2.add("Cherry");ll2.add("Apple");System.out.println(ll2);System.out.println(ll1.containsAll(ll2));ll1.addAll(ll2);System.out.println(ll1);}}
importjava.util.LinkedList;classKW{publicstaticvoidmain(String[]args){//LinkedList class implements the List interface and Deque -> Queue interfaceLinkedList<String>ll=newLinkedList<>();ll.add("Apple");ll.add("Orange");ll.add("Mango");ll.add("Grapes");ll.add("Cherry");ll.add("Apple");System.out.println(ll);ll.remove();System.out.println(ll);ll.remove(2);System.out.println(ll);ll.remove("Cherry");System.out.println(ll);ll.removeAll(ll);//ll.clear();if(ll.isEmpty()){System.out.println("LinkedList is empty now");}}}
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?