What is the difference in:

Set<Object> linksSet = new LinkedHashSet<Object>();
LinkedHashSet<Object> linksSet = new LinkedHashSet<Object>();

Actually, I want to create a Set, that would hold the objects in the same order, I insert into it.
Many thanks for the help, in advance.

In the first case all you are asserting about linksSet is that it is a Set. Anyone using linksSet will only be able to use Set's methods. Maybe later you will want to change the implemention eg to a TreeSet for some reason. By defining things that way you can change the implementation without breaking any code that uses linksSet.
(Having said that, in this particular example there aren't significant extra methods in LinkedHashSet that are not in Set. However the general principle applies that you should declare your variables in the least implementation-dependant way you can, to allow for future cdhanges.)

Actually...
If you want to hold a number of items and access them according to their order of insertion maybe you should look at one of the List classes, eg LinkedList or ArrayList

Hi, Thanks for reply. So it means, if I use line #1 and add values, it won't be in correct order right?
You're recommending to use #1, and not #2 (LinkedHashSet<Object> linksSet = new LinkedHashSet<Object>();) Is this the reason, order is getting disturbed?

[quote]However the general principle applies that you should declare your variables in the least implementation-dependant way you can, to allow for future cdhanges.[/quote] So how would maintain insertion order in the Set? I prefer not to user List, to avoid changing lot of code.

No. The order is determined by the precise type of Set that you create, not by how you declare the variable that refers to it.
If you don't want to use Lists, and the order MUST be the insertion order then #2 is appropriate - the class will maintain the insertion order, and the type of the variable will confirm that that's what you intended.

"#1" Set<Object> linksSet = new LinkedHashSet<Object>();
"#2" LinkedHashSet<Object> linksSet = new LinkedHashSet<Object>();

Means #1 is not correct but #1 is standard declaration, then how to achieve insertion order without using #2, I am trying to know this.

no, #1 IS correct.
If you want to guarantee insertion order is maintained, use a List, not a Set.
While some Sets may be implemented to maintain insertion order, that's not the purpose of a Set.

Yeah, but actually what we need insert should be "in the order" and "without duplicates".

option #2 is perfectly OK for this purpose.
option #1 will also work, but makes it less clear that insertion order is important.

No matter how you declare or refer to it, a new LinkedHashSet() will always be in insertion order and without duplicates - that's guaranteed by the API doc.

If I try any example with LinkedHashSet, objects are in order but when in the project, when using Hibernate mapping file, watch/debug of that variable clearly shows it is not keepingthe objects in the order.

During watch/debug, it shows linksSet=PersitentSet.
May be I conclude, Hibernate mapping is forcing the disorder, if yes, how to force the order during insert into table, is the question.
Thanks for all the help here.

I have no expertise in Hibernate, so I can't answer your latest question, other than to suggest using a List, which will definitely be ordered.

commented: This is correct post, problem is something with Hibernate PersistentSet but behavior of another member "jwenting" is cheap, unprofessional a very lowly thoughts as well! +3

impossible to say, but sure does change the game... And yes, you can't force Hibernate to use a specific implementation class.
And usually what you're getting back from a query would be either a Collection or a List, if I remember correctly.

Yeah, for the display it is a Set, during retrievel TreeSet (using Comparator) is putting the sort order (ASC) based on primary key but this to work correctly on primary key, insert order in table should be correct.

Thanks JamesCherrill, trying it with "List".

And with a List it'd do the same, as it creates the Collection based on the result of a database query.
Instead of trying to force something the wrong way, ensure that those database queries yield the results you need.

And no, that's not related to the "insert order in table". It's related to the SQL/JPQL you use to retrieve the data from the table.

Time to learn how to properly use your tools rather than floundering about, trying random things, and digging yourself an ever deeper hole.

commented: Only theory, no practical reply, no reasoning +0

Actually retrievel is alright in ASC order of primary key, this is done.

But when user enteries don't get inserted in (primary key) sequence, even when using LinkedHashSet is the actual question.
Hibernate is overriding LinkedHashSet behavior.

Resolved it by putting correct change in .hbm file, no change is needed in .java files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.