0

I know HashSet internally work as HashMap and HashMap internally use LinkedList as FIFO etc. So my Question is when I insert values in StudentRecord class object in pattern like..

    StudentsRecord record=new StudentsRecord(1, "Pramod", "UNA");
    StudentsRecord record2=new StudentsRecord(2, "Pankaj","Lucknow");
    StudentsRecord record3=new StudentsRecord(3, "Santosh","Chennai");

    HashSet<StudentsRecord> set=new HashSet<StudentsRecord>();
    set.add(record);
    set.add(record2);
    set.add(record3);

    Iterator<StudentsRecord> iterator=set.iterator();
    while(iterator.hasNext())
    {
        StudentsRecord result=(StudentsRecord)iterator.next();
        System.out.println(result.getId()+","+result.getName()+","+result.getAddress());
    }

After this why my result does not follow FIFO or LIFO order pattern? My Output is:

3,Santosh,Chennai
1,Pramod,UNA
2,Pankaj,Lucknow
2
  • As far as I understand, LinkedList is being used when collision occures Commented Apr 17, 2015 at 5:39
  • hi @Pramod Maurya, if you get your answer then mark it correct answer. So any other viewer can get proper solutions. Thanks. :-) Commented Apr 21, 2015 at 7:03

4 Answers 4

3

Use LinkedHashSet instead of HashSet. because LinkedHashSet maintains insertion order.

public static void main(String[] args) {
    StudentsRecord record=new StudentsRecord(1, "Pramod", "UNA");
    StudentsRecord record2=new StudentsRecord(2, "Pankaj","Lucknow");
    StudentsRecord record3=new StudentsRecord(3, "Santosh","Chennai");


    Set<StudentsRecord> set=new LinkedHashSet<StudentsRecord>();
    set.add(record);
    set.add(record2);
    set.add(record3);


    Iterator<StudentsRecord> iterator=set.iterator();
    while(iterator.hasNext())
    {
        StudentsRecord result=(StudentsRecord)iterator.next();
        System.out.println(result.getId()+","+result.getName()+","+result.getAddress());
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Not answering why? Please refer Tim B's comment below.
1

LinkedHashMap can be used for this purpose.

It is same as HashMap, except that when you iterate over it, it presents the items in the insertion order.

Basically it preserves the insertion order.

from JavaDoc

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Comments

0

LinkedHashSet will preserve insertion order for you.

Comments

0

Since none of the other answers actually answered "why":

HashMap stores objects into buckets, and then only uses the lists in the bucket. This is how it is able to find objects fast, as it can go straight to the correct bucket and then only needs to scan the entries inside that bucket.

In this example 3. has been added into an earlier bucket than 2 or 3 and as a result comes back in a different order.

HashMap and HashSet should never be relied upon to provide any particular ordering. You can use LinkedHashMap to retain insertion order or TreeMap to sort the entries.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.