LinkedList with Looping
In this article, we will see how to loop LinkedList in java
It’s very much common requirement to iterate or loop through LinkedList in java applications
There are mainly 4 ways to loop through LinkedList in java
1) Traditional For loop
2) Enhanced For loop
3) While loop
4) Iterator
Let’s see each of these ways with an example
1) Traditional For loop
- import java.util.*;
- public class LinkedListTraditionalForLoop {
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("Traditional For Loop");
- for (int index = 0; index < list.size(); index++) {
- System.out.println(list.get(index));
- }
- }
- }
import java.util.*; public class LinkedListTraditionalForLoop { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("Traditional For Loop"); for (int index = 0; index < list.size(); index++) { System.out.println(list.get(index)); } } }
Note : In the above example, we have used Traditional for loop to iterate the LinkedList
2) Enhanced For loop
- import java.util.*;
- public class LinkedListEnhancedForLoop {
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("Enhanced For Loop");
- for (Integer element : list) {
- System.out.println(element);
- }
- }
- }
import java.util.*; public class LinkedListEnhancedForLoop { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("Enhanced For Loop"); for (Integer element : list) { System.out.println(element); } } }
Note : In the above example, we have used advanced/enhanced for loop to iterate LinkedList
3) While loop
- import java.util.*;
- public class LinkedListWhileLoop {
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("While Loop");
- int count = 0;
- while ( count < list.size()) {
- System.out.println(list.get(count));
- count++;
- }
- }
- }
import java.util.*; public class LinkedListWhileLoop { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("While Loop"); int count = 0; while ( count < list.size()) { System.out.println(list.get(count)); count++; } } }
Note : In the above example, we are using while loop to iterate linkedlist
4) Iterator
- import java.util.*;
- public class LinkedListWhileIterator {
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("While with iterator");
- Iterator itr = list.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
import java.util.*; public class LinkedListWhileIterator { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("While with iterator"); Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } }
Note : In the above example, we have used iterator with while loop to iterate linkedlist