This discussion is archived
1 Reply Latest reply: Jun 16, 2013 1:43 PM by rp0428 RSS

Order of retrival in HashSet

shivasha Newbie
Currently Being Moderated

Hi,

 

1) In what order (default) the values are retrieved from HashSet?

 

2) ArrayList is displaying the values as they inserted (maintaining order) is it correct?

  • 1. Re: Order of retrival in HashSet
    rp0428 Guru
    Currently Being Moderated

    shivasha wrote:

     

    Hi,

     

    1) In what order (default) the values are retrieved from HashSet?

     

    2) ArrayList is displaying the values as they inserted (maintaining order) is it correct?

    1) - A HashSet doesn't return anything by 'default'; you need to call a method to do that. If you call the 'remove' method multiple times then the order will be the same as the order that you call the 'remove' method in. You specify what element you want and the 'remove' method returns it if it exists.

     

    2) - Before you use ANY Java class you should read the API documentation and then learn HOW to use the class.

     

    http://docs.oracle.com/javase/6/docs/api/java/util/HashSet.html

    >

    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.

    >

    The Java documentation will answer many of these basic questions.