• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Collection(regarding order elements)

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Which of the following classes allow elements to be accessed in the order that they were added?

a. HashMap
b. HashSet
c. Hashtable
d. TreeMap
e. TreeSet
f. LinkedHashMap
g. LinkedHashSet

Answers are f and g.

LinkedHashMap and LinkedHashSet elements are ordered. But TreeSet and TreeMap elements are also ordered. Why those options are not included in the answer. Please help me.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because with TreeSet or TreeMap, the elements are sorted, so for example :

LinkedHashSet<Integer> lhs = new LinkedHashSet<Integer>();
lhs.add(3);
lhs.add(2);
lhs.add(1);
for(int n : lhs) {
System.out.print(n + " ");
}

Will result in :
3 2 1

But :

TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(3);
ts.add(2);
ts.add(1);
for(int n : ts) {
System.out.print(n + " ");
}

Will result in :
1 2 3

The TreeSet and TreeMap keep the elements in SORTED order, not in ENTERED order.

Hope this helps,

Cheers,
--JA
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Joel. I changed that coding to work on JDK1.4.




But I don�t know why add() method is not working and why do I need to import those java.util classes explicitly? Please let me know what part i am missing here.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But I don�t know why add() method is not working and why do I need to import those java.util classes explicitly? Please let me know what part i am missing here.


The add() method takes an object as input. In Java1.4, autoboxing has not been introduced. So an int can't be added to the Collection. You have to use add(new Integer(1)).

You don't need to import each of those java.util classes explicitly, you can do
import java.util.*;
[ August 04, 2006: Message edited by: wise owen ]
 
Die Fledermaus does not fear such a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic