programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Paul Clapham
• Devaka Cooray
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Frits Walraven
Bartenders:
• Carey Brown
• salvin francis
• Claude Moore

# OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide Chapter 3 Question 25

Greenhorn
Posts: 13

What is the result of the following?

A. {1=10, 2=20}
B. {1=10, 2=20, 3=null}
C. {1=10, 2=20, 3=3}
D. {1=13, 2=20}
E. {1=13, 2=20, 3=null}
F. {1=13, 2=20, 3=3}
G. The code does not compile.
H. An exception is thrown.

The book says F is the correct answer.

When I run this code I do get the same output as answer F, but is the order of the output of the toString()-method of a HashMap of Integers to Integers guaranteed?

author & internet detective
Posts: 39244
727

Frenkel Smeijers wrote: but is the order of the output of the toString()-method of a HashMap of Integers to Integers guaranteed?

Good question! The order in a HashMap is not guaranteed. If you want guaranteed order, you'd use a TreeMap which sorts.

Ranch Hand
Posts: 380
2
Why is 3 = 3 if the value was null?  Where is the 3 coming from?  Is that a special case for HashMap<Integer,Integer>?

Nevermind I just didn't know how the merge function works sorry.

Sheriff
Posts: 24374
55

Al Hobbs wrote:Why is 3 = 3 if the value was null?  Where is the 3 coming from?  Is that a special case for HashMap<Integer,Integer>?

The 3 is the second parameter. It's not a special case -- check out the documentation for the Map.merge() method. I had to do that because I wasn't familiar with the method.

Marshal
Posts: 64171
215

Frenkel Smeijers wrote:. . .  is the order of the output of the toString()-method of a HashMap of Integers to Integers guaranteed?