5. LinkedList can accept any element including multiple null values.7. LinkedList uses Iterator interface to traverse the elements.While traversing the elements using iterator if the structure of the LinkedList is modified using add or remove method ,iterator will throw ConcurrentModificationException.So iterators for LinkedList are fail-fast.

Sample Program ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

packagecom.ai1tutorial.collection.LinkedList;

import java.util.LinkedList;

import java.util.Iterator;

import java.util.List;

publicclassLinkedListDemo{

publicstaticvoidmain(String[]args){

List linkedList=newLinkedList<String>();

linkedList.add("Ajay");

linkedList.add("Kumar");

linkedList.add("Mohanty");

Iterator iterator=linkedList.iterator();

while(iterator.hasNext()){

System.out.println(iterator.next());

}

}

}

Output ::

1

2

3

Ajay

Kumar

Mohanty

Now let us see an example of ConcurrentModificationException.In the below program while iterating the LinkedList,i am modifying the structure of the LinkedList by adding a new element.So I am getting ConcurrentModificationException.Example of ConcurrentModificationException ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

packagecom.ai1tutorial.collection.LinkedList;

import java.util.LinkedList;

import java.util.Iterator;

import java.util.List;

publicclassLinkedListDemo{

publicstaticvoidmain(String[]args){

List<String>linkedList=newLinkedList<String>();

linkedList.add("Ajay");

linkedList.add("Kumar");

linkedList.add("Mohanty");

Iterator iterator=linkedList.iterator();

while(iterator.hasNext()){

System.out.println(iterator.next());

LinkedList.add("Jyoti");

}

}

}

Output ::

1

2

3

4

5

Ajay

Exception inthread"main"java.util.ConcurrentModificationException

at java.util.LinkedList$Itr.checkForComodification(Unknown Source)

at java.util.LinkedList$Itr.next(Unknown Source)

at com.ai1tutorial.collection.LinkedList.LinkedListDemo.main(LinkedListDemo.java:16)

About the Author

Dear Friends,
I am A.K.M. .This online journal ai1tutorial.com is a humble effort from my side to all students and professionals who want to learn computer science related technologies.I am not a professional blogger and I write posts in this blog only in my leisure time.I may not be 100% perfect in all my posts,but your review comments can surely help me to make it 100% perfect.