using iterators in java

Thinking about using our OCA 8 book to study for the Java Foundations Junior Associate exam? It covers most of the topics. See what other topics you need to learn and where to read about that. One of those topics is iterating through a list.

Since Java 5, the most common way to iterate though a list (if you don’t need the loop index) is:

List<String>  list = Arrays.asList("sheep", "deer", "rat");
for (String name : list) {
   System.out.println(name);
}

Before Java 5 came along, there was another way:

List list = Arrays.asList("sheep", "deer", "rat");
 Iterator it = list.iterator();
 while (it.hasNext()) {
   String name = (String) it.next();
   System.out.println(name);
 }

But we are told to use generics in new code which would give us:

List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 while (it.hasNext()) {
   String name = it.next();
   System.out.println(name);
 }

Why would you do this? Shrug. For reading old code I guess. But it is on the test so no time like the present to learn this idiom. See what is wrong here?

// DOES NOT COMPILE
 List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 while (it.next()) {
   String name = it.hasNext();
   System.out.println(name);
 }

This one reverses the order of hasNext/next. Remember that you have to check that the iterator has a next element before getting it. Now what do you think is the problem with this?

// BAD
 List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 System.out.println(it.next());

If the list is empty, this code throws an exception. Probably not what you had in mind. Instead, you should write:


 List<String> list = Arrays.asList("sheep", "deer", "rat");
 Iterator<String> it = list.iterator();
 if (it.hasNext()) System.out.println(it.next());

Summary

There’s not much you have to memorize. The key facts are:

  • Call hasNext() in an if statement or while loop
  • Call next() once you know there is a next element
  • If the Iterator doesn’t use generics, you must cast unless you want Object

Practice Questions

Question 1

Which correctly fill in the blanks?


 List<String> list = Arrays.asList("a", "b", "c");
 Iterator<String> it = list.iterator();
 while (it._____()) {
   String name = it.______();
   System.out.println(name);
 }

A: hasNext, next

B: next, hasNext

C: The code does not compile with either A or B

D: The code throws an exception after being completed with A or B

Question 2

Which correctly fill in the blanks?


 List<String> list = Arrays.asList("a", "b", "c");
 Iterator it = list.iterator();
 while (it._____()) {
   String name = it.______();
   System.out.println(name);
 }

A: hasNext, next

B: next, hasNext

C: The code does not compile with either A or B

D: The code throws an exception after being completed with A or B

Question 3

Which correctly fill in the blanks?


 List list = Arrays.asList("a", "b", "c");
 Iterator<String> it = list.iterator();
 while (it._____()) {
   String name = it.______();
   System.out.println(name);
 }

A: hasNext, next

B: next, hasNext

C: The code does not compile with either A or B

D: The code throws an exception after being completed with A or B

The answers are posted here.

why the paperless office doesn’t work for everyone

For years, we’ve been hearing about how the paperless office is the future. Now that we have tablets, the future has clearly arrived. Quick. Throw out the paper. Actually, hold on for a second.

Different people have different work styles. For some, a paperless office is great. For others, not so much. Let’s look at a few valueable uses of paper.

Notes while coding
Personally, I like to make notes while coding. Ideas of things I still need to do. Stray thoughts. Anything. This is very transient information.

Some I could put into //TODO s in Eclipse. Some are better free form. Some are just stray thoughts that I can’t put into Eclipse because I don’t want to break flow.

Manuals
We only have so much screen space. Some things, like the Java Docs, are great to have on screen.

Others, like learning a new technology from a book, can be nice to have on the table next to you. That also allows me to take notes (in the book) while writing.

I’ve seen teammates do the same with “how to” wiki pages they were unfamiliar with.

Proofreading
When I proofread, I do a mix of on my iPad and printed. I try to do a final read on printed paper. I find more errors that way.

I read a study that indicated there is actually some science to this.

Spatial memory
I use where information lies on the page as spatial memory. That way I can train my brain to location and not just data.

For example, when I’m giving a presentation I always print the slides and run through them in my hand. That extra tactial/spatial info helps me retain it better.

To do lists
My “master” to do list has been electronic for years. I do keep micro “to do” lists on paper though.

They are half baked thoughts of things that I am doing today. Or maybe a tiny piece of a task. Or what I am up to when getting up to take a break.

Contingency
What do you do when your laptop freees/crashes? (Thank you Windows. It is 2015 and I still need to have a plan for what to do when you freeze.)

I go to my paper at that point while I wait for the computer to get back to me. I can organize my thoughts on paper. Or read something I have printed.

The same holds from when I call the help desk and they are using my computer. I don’t have dead time with some paper around.

Conclusion
I’m not saying everyone needs paper. Just that I think we are a far way from getting of paper completely.

I do try to recycle. I use the backs of paper that served another purpose for my notes. And I print 2 pages to a side in Word or PowerPoint when reviewing. Trying to balance saving trees and productivity.

The e-office of the future is great. Maybe we will get there one day.

what does it mean to be OCP Java 8 Programmer II certified?

There are now three paths for one to become OCP 8 (Java Programmer II certified.) So what does it mean someone certified had to know to pass the exam. Well, that varies too.

The three paths

  1. Starting out with Java 8 – take the OCP 8 (IZ0-809)
    Pre-req: OCA 8 (1Z0-808)
  2. Holding a Java 7 Professional cert – take the Java 7 to 8 upgrade exam (IZo-810)
    Pre-req: OCP 7 (IZ0-804) or Java 7 upgrade exam (IZo-805)
  3. Holding any Java 6 or older Professional cert – take the Java 6 or earlier to 8 upgrade exam (IZo-813)
    Pre-req: SCJP/OCJP 6 (IZO-851) or Java 6 upgrade (IZ0-852) or SCJP/OCJP 5 (IZo-853) or Java 5 upgrade (1Zo-854) or Java 4 or lower Professional cert

What one would expect

It seems reasonable to assume some things here.

  1. People taking the OCP 8 directly should be tested on the topics that entail being Java 8 certified.
  2. vennPeople taking the upgrade from Java 7 should be tested on just the topics that were added in Java 8. This is the purple in the Venn diagram. There’s no reason to retest on the topics that the were already on the Java 7 exam. That’s the overlap in the Venn diagram. (This is a bit simplified. It’s really that the topics should be those on the OCA 8 or OCP 8, but not on the OCA 7 or OCP 7. Luckily the topics added on the OCA 8 are also on the OCP 8.)
  3.  venn2People taking the upgrade from older versions of Java have a more interesting situation. When taking a very old exam, lots of topics are different. For example, new topics include generics and the enhanced for loop for those upgrading from Java 5. These topics are so old that it is reasonable to assume the candidate knows this as these syntax changes are covered as part of questions on all sorts of topics. Since the exam changed a lot between Java 6 and 7, let’s just imagine all upgrade candidates in this group took the Java 6 exam. Which would imply the topics covered should be the purple OCP 8 circle except for the overlap with the OCP 6 circle. It doesn’t imply the topics covered in OCP 7 but not OCP 6 or 8 should be covered. After all, those topics were removed from the OCP 8 exam so shouldn’t be needed to get a Java 8 certification.

These assumptions turn out to not match what Oracle actually did. The rest of this blog post describes the surprises.

What people starting out with Java 8 were tested on, but those upgrading from Java 7 were not

Topics:

  1. The concept of immutability
  2. The concepts of deadlock, starvation, livelock, and race conditions. They are tested when upgrading indirectly but with less emphasis.

My thoughts: No big deal here

What people starting out with Java 8 were tested on, but those upgrading from Java 6 were not

  1. The concept of immutability
  2. The singleton pattern
  3. The concepts of deadlock, starvation, livelock, and race conditions. They are tested when upgrading indirectly but with less emphasis.
  4. The entire topic of JDBC

My thoughts: Leaving out the first three isn’t a big deal. Leaving out JDBC is bizarre. That’s a whole topic that is part of core Java. It started being needed for Java 7 (or 8) certification. Upgrading from an older version seems like it should require it. But nope.

What people upgrading from Java 7 were tested on, but those starting out with Java 8 were not

  1. The computeIfAbsent() and computeIfPresent() methods on Map
  2. Also merge()  [removed from main exam in October 2015 and removed from upgrade exam in November 2015]

My thoughts: Odd to add topics on an upgrade exam. These are so similar, I’d almost think they were implied on the Java 8 exam.

What people upgrading from Java 6 were tested on, but those starting out with Java 8 were not

  1. The computeIfAbsent() and computeIfPresent() methods on Map
  2. Also merge()  [removed from main exam in October 2015]
  3. IO.2 – DirectoryWatcher, FileVisitor and WatchService
  4. Locks package in concurrency API
  5. DecimalFormat and SimpleDateFormat

My thoughts: This is bizarre. These three NIO.2 classes, the locks package and the two format classes were on the OCP 7 exam, but not on the OCP 8 exam. Oracle changing their mind and taking something off the exam for a later version is perfectly reasonable. However, I would think that would mean you don’t put it on the upgrade exam! Why should the upgrade exam cover something that isn’t on the target exam.

Summary

If I was writing the objectives for the upgrade exam from Java 6,  I’d have dropped some topics and added JDBC. But I’m not writing the exam objectives so will remain puzzled.