aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collection" Watch "Collection" New topic
Author

Collection

Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
Hi
I am really confused i was going through collection and i came across an example in which 3,2,7,6,8,9 is added to the list as i have read that in list only sorted data is stored then how can we add data like this.And can somebody suggest me in which situations to use Set,List,Map.
Regards
Rashi
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
I am confused as well but with your question...
You can store pretty much anything in a List as long as it derives from Object which primitives do not so you need to wrap them in an Object (in your case an Integer would do).
In Tiger (J2SDK 1.5) this is being addressed by the introduction of autoboxing and autounboxing which will handle that wrapping and unwrapping for you behind the scene.


42
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
Hi Jeroen
Sorry for confusing you but actually i don't have much knowledge about Collection I was wandering when they say that the list always store sorted data then can we input the data in the list which is not sorted.
I hope iam making some sense.
Regards
Rashi
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Unless you have a List implementation which is sorted (there isn't one by default, I wrote my own) the order in which the data exists in the List is the same order in which it is inserted.
There is no checking of sorting, nothing is enforced.
In fact, there is (unless you use generics in JDK 1.5) not even a check on the type of data you're inserting and you can happily mix all kinds of stuff inside a List (which can get to be a problem as you can imagine).
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
Thanks Jeroen for clearing my doubt i have one more doubt i have written a piece of code
import java.util.*;
public class TestHashSet {
public static void main(String args[]) {
Set set = new HashSet();
set.add("Bill");
set.add("Henrry Ford");
set.add("Bill");
set.add("Brian");
set.add("Gates");
System.out.println(set);
System.out.println("size"+set.size());
Iterator itr = set.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:[Brain,Bill,Henrry Ford,Gates]
Size:4
Brain
Bill
Henrry Ford
Gates
Now my question is why it is not showing the set values in the order they are inserted in set.Using the same example for list i got the result in order in which i have inserted the value then why not for set.
Regards
Rashi
Karthik Gurunathan
Greenhorn

Joined: Jan 26, 2004
Posts: 19
Hi Rashi,
As far as I know when you use hashset..you cannot guarentee the order in which it will be added in the list...I guess its using the hashcode to order the list....am I right folks...It may be platform or SDK version dependant as well.
Thanks
karthik
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Karthik,
You are 100% right.
According to HashSet API definition
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element
So Rashid if you really want to have elements in a sorted order use TreeSet which basically aligns your elements in an ascending order.
HTH,
Ramnath
Feed an Opportunity.Starve a Problem
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
Thanks guys for your help.
Regards
Rashi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection