File tree Expand file tree Collapse file tree 5 files changed +182
-0
lines changed
collections/linkedlist/iterate-linkedlist-in-java Expand file tree Collapse file tree 5 files changed +182
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+ import java .util .List ;
5+
6+ /**
7+ * A java program to iterate over linkedList
8+ * using the java 8 foreach method.
9+ *
10+ * @author coderolls.com
11+ */
12+ public class LinkedListForEachMethod {
13+
14+ public static void main (String [] args ) {
15+
16+ List <String > linkedList = new LinkedList <String >();
17+
18+ linkedList .add ("Mango" );
19+ linkedList .add ("Banana" );
20+ linkedList .add ("Grapes" );
21+ linkedList .add ("Watermelon" );
22+ linkedList .add ("Apple" );
23+ linkedList .add ("Avocado" );
24+
25+ System .out .println ("Iterating through LinkedList using forEach method and lamda expression........\n " );
26+
27+ // add lambda expression in the foreach method
28+ linkedList .forEach (s -> System .out .println ("Object form the LinkedList is " + s ));
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+ import java .util .List ;
5+
6+ /**
7+ * A java program to iterate over linkedList
8+ * using the basic for loop.
9+ *
10+ * @author coderolls.com
11+ *
12+ */
13+ public class LinkedListForLoop {
14+
15+ public static void main (String [] args ) {
16+
17+ List <String > linkedList = new LinkedList <String >();
18+
19+ linkedList .add ("Mango" );
20+ linkedList .add ("Banana" );
21+ linkedList .add ("Grapes" );
22+ linkedList .add ("Watermelon" );
23+ linkedList .add ("Apple" );
24+ linkedList .add ("Avocado" );
25+
26+ System .out .println ("Iterating through LinkedList using the basic for loop!\n " );
27+
28+ int len = linkedList .size ();
29+
30+ // iterate over linkedList using basic for loop
31+ for (int i = 0 ; i < len ; i ++) {
32+
33+ // get String object from linkedList at index i
34+ String str = linkedList .get (i );
35+ System .out .println ("Object form the LinkedList at " + i + " is " + str );
36+ }
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+ import java .util .List ;
5+
6+ /**
7+ * A java program to iterate over linkedList
8+ * using the foreach loop.
9+ *
10+ * @author coderolls.com
11+ *
12+ */
13+ public class LinkedListForeachLoop {
14+
15+ public static void main (String [] args ) {
16+
17+ List <String > linkedList = new LinkedList <String >();
18+
19+ linkedList .add ("Mango" );
20+ linkedList .add ("Banana" );
21+ linkedList .add ("Grapes" );
22+ linkedList .add ("Watermelon" );
23+ linkedList .add ("Apple" );
24+ linkedList .add ("Avocado" );
25+
26+ System .out .println ("Iterating through LinkedList using the foreach loop!\n " );
27+
28+ // iterate over linkedList using foreach loop
29+ for (String str : linkedList ) {
30+ System .out .println ("Object form the LinkedList is " +str );
31+ }
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .Iterator ;
4+ import java .util .LinkedList ;
5+ import java .util .List ;
6+
7+ /**
8+ * A java program to iterate through LinkedList using Iterator interface.
9+ *
10+ * @author coderolls.com
11+ *
12+ */
13+ public class LinkedListIterator {
14+
15+ public static void main (String [] args ) {
16+ List <String > linkedList = new LinkedList <String >();
17+
18+ linkedList .add ("Mango" );
19+ linkedList .add ("Banana" );
20+ linkedList .add ("Grapes" );
21+ linkedList .add ("Watermelon" );
22+ linkedList .add ("Apple" );
23+ linkedList .add ("Avocado" );
24+ System .out .println ("Iterating through LinkedList using Iterator interface........\n " );
25+
26+ // getting iterator
27+ Iterator <String > iterator = linkedList .iterator ();
28+
29+ // hasNext() returns true only if object is available at next call
30+ while (iterator .hasNext ()) {
31+
32+ // next() returns obejct, we can cast it to reuqired type
33+ String str = iterator .next ();
34+ System .out .println ("Object form the LinkedList is " + str );
35+ }
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+ import java .util .List ;
5+
6+ /**
7+ * A java program to iterate over linkedList
8+ * using the while loop.
9+ *
10+ * @author coderolls.com
11+ *
12+ */
13+ public class LinkedListWhileLoop {
14+
15+ public static void main (String [] args ) {
16+
17+ List <String > linkedList = new LinkedList <String >();
18+
19+ linkedList .add ("Mango" );
20+ linkedList .add ("Banana" );
21+ linkedList .add ("Grapes" );
22+ linkedList .add ("Watermelon" );
23+ linkedList .add ("Apple" );
24+ linkedList .add ("Avocado" );
25+
26+ System .out .println ("Iterating through LinkedList using the while loop!\n " );
27+
28+ int len = linkedList .size ();
29+
30+ // iterate over linkedList using while loop
31+ int i =0 ;
32+
33+ while (i <len ) {
34+
35+ // get String object from linkedList at index i
36+ String str = linkedList .get (i );
37+
38+ System .out .println ("Object form the LinkedList is " +str );
39+
40+ //increment index after getting the object
41+ i ++;
42+ }
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments