0% found this document useful (0 votes)
1 views5 pages

04_ What is wrong with this code_ Java Collection & ConcurrentModificationException _ 800+ Big Data & Java Interview FAQs

The document discusses a Java code snippet that throws a ConcurrentModificationException when attempting to remove an item from a list while iterating over it. It provides two solutions: using the iterator's remove method or utilizing a CopyOnWriteArrayList to avoid the exception. The content is part of a larger collection of resources aimed at preparing for Java and Big Data interviews.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
1 views5 pages

04_ What is wrong with this code_ Java Collection & ConcurrentModificationException _ 800+ Big Data & Java Interview FAQs

The document discusses a Java code snippet that throws a ConcurrentModificationException when attempting to remove an item from a list while iterating over it. It provides two solutions: using the iterator's remove method or utilizing a CopyOnWriteArrayList to avoid the exception. The content is part of a larger collection of resources aimed at preparing for Java and Big Data interviews.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

06/12/2024, 19:37 04: What is wrong with this code?

with this code? Java Collection & ConcurrentModificationException | 800+ Big Data & Java Interview FAQs

800+ Q&As Menu Contact & reviews Register Login

800+ Big Data & Java Interview FAQs


It pays to prepare & choose from 2-6 offers. Read & Write more code to fast-track & go places with confidence.

Select A to Z of Java & Big Data Techs


search here … Go

Home Why?  Career  Membership 

Home › Quick Prep Java Interview › 150+ Java coding Q&As › Coding - What is wrong? › 04: What is wrong

with this code? Java Collection & ConcurrentModificationException

04: What is wrong with this 300+ Java Interview


Q&As - Quick Prep FAQs

code? Java Collection & 300+ Java Interview FAQs 📌 

ConcurrentModificationExce 150+ Spring & Hibernate


FAQs
📌

ption
150+ Java Architect FAQs

100+ Java code quality Q&As

150+ Java coding Q&As
 Posted on April 26, 2015 

Q9. What is wring with this code?

300+ Big Data Interview


1 import java.util.ArrayList;
Q&As - Quick Prep FAQs
2 import java.util.Iterator;
3 import java.util.List; 300+ Big Data Interview
4 FAQs 📌 

5 public class WhatIsWrong {


250+ Scala Interview FAQs 
6
7 public static void main(String args[]) { 250+ Python interview
8 List<String> myList = new ArrayList<String>(); FAQs 📌 
9
10 myList.add("Java");
11 myList.add("JEE");
12 myList.add("XML");
13 Key Areas & Companion
14 Iterator<String> it = myList.iterator(); Techs FAQs
15 while (it.hasNext()) {
16 String value = it.next(); 16+ Tech Key Areas FAQs 
17 System.out.println("List Value:" + value);
18 if (value.equals("Java")) { 10+ Companion Tech Q&As 
19 myList.remove(value);
20 }
21 }
22 }
23 }
24

https://www.java-success.com/part-3-beginner-what-is-wrong-with-this-java-code/ 1/5
06/12/2024, 19:37 04: What is wrong with this code? Java Collection & ConcurrentModificationException | 800+ Big Data & Java Interview FAQs
25 800+ Enterprise & Core
Java Q&As
A9. throws a Runtime exception called
300+ Core Java Q&As 
“java.util.ConcurrentModificationException” The output will be:
300+ Enterprise Java Q&As 

1 List Value:Java
2 Exception in thread "main" java.util.ConcurrentModificationExc
3 at java.util.ArrayList$Itr.checkForComodification(ArrayLis
Java & Big Data Tutorials
4 at java.util.ArrayList$Itr.next(ArrayList.java:836)
5 at WhatIsWrong.main(WhatIsWrong.java:17)
6 Tutorials - Golang 

Tutorials - Big Data 


Fix 1: Use iterator.remove() instead of collection.remove Tutorials - Enterprise Java 

1 import java.util.ArrayList;
2 import java.util.Iterator;
3 import java.util.List;
4
5 public class WhatIsWrong {
6
7 public static void main(String args[]) {
8 List<String> myList = new ArrayList<String>();
9
10 myList.add("Java");
11 myList.add("JEE");
12 myList.add("XML");
13
14 Iterator<String> it = myList.iterator();
15 while (it.hasNext()) {
16 String value = it.next();
17 System.out.println("List Value:" + value);
18 if (value.equals("Java")) {
19 it.remove(); //FIX
20 }
21 }
22 }
23 }
24

Output:

1 List Value:Java
2 List Value:JEE
3 List Value:XML
4

Fix 2: Use java.util.concurrent classes like CopyOnWriteArrayList

1 import java.util.Iterator;
2 import java.util.List;
3 import java.util.concurrent.CopyOnWriteArrayList;
https://www.java-success.com/part-3-beginner-what-is-wrong-with-this-java-code/ 2/5
06/12/2024, 19:37 04: What is wrong with this code? Java Collection & ConcurrentModificationException | 800+ Big Data & Java Interview FAQs
4
5 public class WhatIsWrong {
6
7 public static void main(String args[]) {
8 List<String> myList = new CopyOnWriteArrayList<String
9
10 myList.add("Java");
11 myList.add("JEE");
12 myList.add("XML");
13
14 Iterator<String> it = myList.iterator();
15 while (it.hasNext()) {
16 String value = it.next();
17 System.out.println("List Value:" + value);
18 if (value.equals("Java")) {
19 myList.remove(value);
20 }
21 }
22 }
23 }
24

Output:

1 List Value:Java
2 List Value:JEE
3 List Value:XML
4

This content is for 100-Day-Full-Access, 200-Day-Full-Access, and 365-Day-Full-


Access members only.
Register

‹ 03: What is wrong with this code? Data types & equals( ) Vs, hashCode( ) contract

05: What is wrong with this code? Deep Vs Shallow comparison & multithreading ›

About Latest Posts

Arulkumaran Kumaraswamipillai
Mechanical Engineer to self-taught Java engineer in 1999. Contracting since 2002 as a Java Engineer &
Architect and since 2016 as a Big Data Engineer & Architect. Preparation & key know-hows empowered me
to attend 150+ job interviews, choose from 130+ job offers & negotiate better contract rates. Author of the
book "Java/J2EE job interview companion", which sold 35K+ copies & superseded by this site with 3.5K+

https://www.java-success.com/part-3-beginner-what-is-wrong-with-this-java-code/ 3/5
06/12/2024, 19:37 04: What is wrong with this code? Java Collection & ConcurrentModificationException | 800+ Big Data & Java Interview FAQs
registered users Amazon.com profile | Reviews | LinkedIn | LinkedIn Group | YouTube Email: java-
interview@hotmail.com

How to take the road less travelled?


Practicing & brushing up a few Q&As each day on broad number of main stream categories can make a huge impact in 3-12
months.

"You are paid to read & write lots of code & solve business problems in a
collaborative environment"
100+ Free Java Interview FAQs
100+ Free Big Data Interview FAQs

Don't be overwhelmed by the number of Q&As. Job interviews are not technical contests to see who gets most number of questions right.
Nobody knows everything. The Clarity of the answers you give with real-life examples will go a long way in getting you multiple job offers.
It pays to brush-up & choose from 2-6 job offers. Experienced interviewers can easily judge your real experience from a few open-ended
questions & the answers you provide.

If you are pressed for time, popular categories are pinned 📌


for you to prioritise based on the job description. Here are my top career
making know-hows & tips to open more doors as a Java/Big Data Engineer, Architect or Contractor.

1. Feeling stagnated?
2. How to earn more?
3. Freelancing Vs contracting?
4. Self-taught professional?
5. Job Interview Tips
6. Resume Writing Tips

Top 12 popular posts last 30 days


300+ Java Interview Q&As with code, scenarios & FAQs
300+ Core Java Interview Q&As 01. Ice breaker Tell us about yourself? 02. Ice Breaker 8 Java real life scenarios…
(6,825)

300+ Big Data Interview Q&As with code, scenarios & FAQs
100+ SQL Interview Q&As 01. 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code? 02.…
(2,425)

00: Top 50+ Core Java interview questions & answers for 1 to 3 years experience
Top 50 core Java interview questions covering core Java concepts with diagrams, code, examples, and scenarios. If you don't get…
(1,971)

Java 8 String streams and finding the first non repeated character with functional programming
Q1.Find the first non repeated character in a given string input using Java 8 or later? A1.Extends Find the first…
(1,669)

18 Java scenarios based interview Q&As for the experienced – Part 1

https://www.java-success.com/part-3-beginner-what-is-wrong-with-this-java-code/ 4/5
06/12/2024, 19:37 04: What is wrong with this code? Java Collection & ConcurrentModificationException | 800+ Big Data & Java Interview FAQs
Let's look at scenarios or problem statements & how would you go about handling those scenarios in Java. These scenarios…
(1,541)

Membership Levels
Membership prices listed below are in Australian Dollars (A$). If you purchase in other currencies like Indian rupees, the equivalent…
(718)

30+ Java Code Review Checklist Items


This Java code review checklist is not only useful during code reviews, but also to answer an important Java job…
(524)

8 real life Java scenarios with Situation-Action-Result (i.e. SAR) technique


The SAR (Situation-Action-Result) technique is very useful to tackle open-ended questions like: 1. What were some of the challenges you…
(522)

01: 30+ Java architect interview questions & answers – Part 1


One of the very frequently asked open-ended interview questions for anyone experienced is: Can you describe the high-level architecture of…
(480)

15 Ice breaker interview Q&As asked 90% of the time


Most interviews start with these 15 open-ended questions. These are ice breaker interview questions with no right or wrong answers…
(412)

0: 50+ SQL scenarios based interview Q&As – What is wrong with this SQL code?
This extends 18+ SQL best practices & optimisation interview Q&As. You can practice these SQLs by setting up the data…
(370)

02: 10 Java String class interview Q&As


Java Collection interview questions and answers and Java String class interview questions and answers are must know for any Java…
(357)

Disclaimer
The contents in this Java-Success are copyrighted and from EmpoweringTech pty ltd. The EmpoweringTech pty ltd has the right to correct or enhance the current content without

any prior notice. These are general advice only, and one needs to take his/her own circumstances into consideration. The EmpoweringTech pty ltd will not be held liable for any

damages caused or alleged to be caused either directly or indirectly by these materials and resources. Any trademarked names or labels used in this blog remain the property of

their respective trademark owners. Links to external sites do not imply endorsement of the linked-to sites. Privacy Policy

© 2024 800+ Big Data & Java Interview FAQs Responsive WordPress Theme powered by CyberChimps

https://www.java-success.com/part-3-beginner-what-is-wrong-with-this-java-code/ 5/5

You might also like