• 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

Collection

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rashi Gulati
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for your help.
Regards
Rashi
 
reply
    Bookmark Topic Watch Topic
  • New Topic