-4

Our existing code using HashSet to store data's.I know Nature of the Set interface is giving random order.But i need the same insertion order.Is there any way to get the insertion order from the set???

Please guide me get out of this issue?

I also used LinkedHashSet, it also giving elements in some other order and not insertion order that i wanted...

Set attachmentSet=new HashSet();
        attachmentSet.add("dfsdfsd");
        attachmentSet.add("erwerwer");
        attachmentSet.add("vcvcvcv");
        attachmentSet.add("ytytyt");

        Iterator attachItr=attachmentSet.iterator();
        while(attachItr.hasNext())
        {
            System.out.println("SET Item::"+attachItr.next());
        }

LinkedHashSet newCopy = new LinkedHashSet();
        newCopy.add("dfsdfsd");
        newCopy.add("erwerwer");
        newCopy.add("vcvcvcv");
        newCopy.add("ytytyt");
        Iterator attachItr2=copy.iterator();
        while(attachItr2.hasNext())
        {
            System.out.println("NEW LinkedHashSet Item::"+attachItr2.next());
        }
Saravanan
  • 11,372
  • 43
  • 143
  • 213
  • 4
    With the little bugfix - `Iterator attachItr2 = newCopy.iterator()` - I get the requested output. The `LinkedHashSet` specifically maintains the insertion order. What are the problems you are seeing? – Seelenvirtuose Sep 08 '14 at 09:40
  • You want the items to be sorted in the set (based on hashcode) or you want the items in the order you have inserted them?. `LinkedHashSet` ought to work for the latter case. – TheLostMind Sep 08 '14 at 09:40
  • @TheLostMind:i want the insertion order from the HashSet.how can i get it? – Saravanan Sep 08 '14 at 09:44
  • @Thanks Seelenvirtuose: I did not see that.Could you plz help me to get the elements from the HashSet in Insertion Order? – Saravanan Sep 08 '14 at 09:45

3 Answers3

1

I would say that it is not possible because of the way hash set works:

When you insert something into a hash set, its hash is computed (a special unique value obtained through some maths) and it is then stored as a mapping from its hash to its value. No 'history' of insertion is kept.

If you do want to achieve what you describe, you better choose another data structure such as a linked list.

Edit: as suggested by llogiq and Sasikumar Murugesan, a good data structure that could fit your needs and keep the rest of your software as is would be to use a LinkedHashSet. This allows your software to still use HashSet elsewhere.

Belgorath
  • 66
  • 6
1

Seelenvirtuose has it right: LinkedHashSet maintains insertion order. Also your code has an error, in that you iterate copy instead of newCopy. Perhaps that is the reason why you did not see the correct ordering?

Also if you wanted to test that it does not sort by natural order, it would be good to add elements so that natural order differs from insertion order.

llogiq
  • 13,815
  • 8
  • 40
  • 72
1

You have to use LinkedHashSet to retain insertion order and see below sample code

LinkedHashSet lhs = new LinkedHashSet();

    lhs.add("a");
    lhs.add("b");
    lhs.add("c");
    lhs.add("d");
    lhs.add("e");

    Iterator iterator=lhs.iterator();

    while(iterator.hasNext())
    {
      String value=(String)iterator.next();

      System.out.println("Value :"+value);
    }

Output: a b c d e

Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • 2
    You could also still declare and use `lhs` as `HashSet` if you need it for another interface (e.g. `HashSet lhs = new LinkedHashSet();`) – Holloway Sep 08 '14 at 09:56