• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Chishlom's question

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's the question 20 in mock 3.
Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
a. TreeMap
b. HashMap
c. LinkedHashMap
d. The answer depends on the implementation of the existing instance.
e. None of the above.
the answer is c.
and the conclusion:
The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection. The iteration order of a LinkedHashMap is determined by the order in which elements are inserted. When a new LinkedHashMap is created by passing a reference to an existing Collection to the constructor of a LinkedHashMap the Collection.addAll method will ultimately be invoked. The addAll method uses an iterator to the existing Collection to iterate through the elements of the existing Collection and add each to the instance of the new LinkedHashMap. Since the iteration order of the LinkedHashMap is determined by the order of insertion, the iteration order of the new LinkedHashMap must be the same as the interation order of the old Collection.
i answered d since in the question. Dan mention that the existing instance is of a Map, and that's mean it may be HashMap that isn't sorted.
May somene comment me?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every map has an iteration order that depends on the type of the map, for example, a HashMap has an unknown iteration order, but a TreeMap has an iteration order that depends on a comparator or on the method compareTo() of the object being inserted (if it implements java.lang.Comparable).
Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance?
A LinkedHashMap has the fast feature of a HashTable and the ability to preserve the insertion order. A simple HashMap does not have this ability. addAll() method will be called, this method gets each object of one collection (based on the iteration order) and inserts in another collection. Since a LinkedHashMap preserves the insertion order, the order of the iteration with the objects of a linkedHashMap will be the same of the collection that you used to create this LinkedHashMap!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohamed,
Leandro is correct. The LinkedHashMap will make a copy of any Map and preserve the iteration order regardless of the implementation of the original Map.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic