• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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 ]
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic