Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Collection(regarding order elements)

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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 ]
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic