• Post Reply Bookmark Topic Watch Topic
  • New Topic
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  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

No, as Jeanne has already sad.

A HashMap defaults to load factor=75% and initial size=0x10. It contains an array of buckets accessed via the hashCode like this: bucketsArray[h & c − 1], where c is the capacity (length of array) and h the hash code after rehashing. Rehashing won't alter small numbers like 1 2 3. The hash code of an Integer is equal to its int value. So the pair with key 3 sorts to bucketsArray[3]. There won't be any collisions for three numbers in sequence, nor further rehashing until the size passes 16 × (75%), so in this unusual situation, it is possible to predict the order of traversal of the Map.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!