Running on Java 22-ea+27-2262 (Preview)
Home of The JavaSpecialists' Newsletter

186Iterator Quiz

Author: Olivier CroisierDate: 2010-07-30Java Version: 6Category: Language
 

Abstract: Most of the northern hemisphere is on holiday, so here is a quick quiz for those poor souls left behind manning the email desk. How can we prevent a ConcurrentModificationException in the iterator?

 

Welcome to the 186th issue of The Java(tm) Specialists' Newsletter, sent from the beautiful island of Crete. Yesterday morning, Olivier Croisier sent me a quiz that kept me wondering for a while. Olivier teaches our Java Specialist Master Course in France (in French). Since most of my readers are on holiday, I thought a small quiz would be great for those poor souls left behind manning the help desk.

javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.

Iterator Quiz

Hi everyone,

Here is my Java Quiz #40, I hope you'll like it - if you're not somewhere on a beach drinking Mojitos :)

The idea behind this quiz was found during the last Java Specialist training session, when we were musing through the JDK source code. One of the students, Romain Revol, helped me prove it worked.

As it is summer (despites the dark clouds and low temperature in Paris), I tried to tell a short, funny (I hope) story to explain what you are expected to do in this quizz.

So, without further ado, here it is !

To please your Project Manager, a former developer (yeaaars ago), you sometimes let him help you develop some "very important" parts of your application.

Today, he's in charge of displaying "Hello World" by iterating on a list containing those words. Alas, distracted by his going on vacation this very afternoon, he forgets to add "World" to the list before starting the iteration. Trying to correct his mistake, he adds it a few lines later, but now his code unexpectedly breaks down at runtime ("this must be a JVM bug !").

A few minutes before leaving, he asks you to find a solution in his absence, with the following instructions :

  • Do not modify his existing code, it's Perfect (of course).
  • The FIXME tag shows where you're allowed to insert your corrective code
  • He must be able to understand your solution when he comes back (so using Reflection is not an option).

Are you worth the trust of your beloved Manager ?

final List<String> list = new ArrayList() {{ add("Hello"); }};
final Iterator<String> iterator = list.iterator();
System.out.println(iterator.next());
list.add("World");
// FIXME : work here while I'm sunbathing
System.out.println(iterator.next());

Have fun !

Olivier


Once you've figured it out, please head over to Olivier's blog The Coders Breakfast.net to see if you got it right.

Heinz

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...