Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 13. Maps > A List Map - Pg. 329

Maps equals() method has been implemented or that it will even work as needed when comparing itself with a DefaultEntry . (For the authoritative discussion on equals() , see Effective Java [Block, 2001].) Therefore, rather than cross your fingers and hope for the best, you've instead taken the key/value pairs and added them to the list as instances of DefaultEntry , which you know implements the equals() method and is the same class as the expected entries: private void checkIterator(Iterator i) { List entries = new LinkedList(); for (i.first(); !i.isDone(); { Map.Entry entry = (Map.Entry) i.current(); entries.add(new DefaultEntry(entry.getKey(), entry.getValue())); } try { i.current(); fail(); } catch (IteratorOutOfBoundsException e) { // expected } assertEquals(4, entries.size()); assertTrue(entries.contains(A)); assertTrue(entries.contains(B)); assertTrue(entries.contains(C)); assertTrue(entries.contains(D));