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.
Edited 2 Years Ago by peter_budo: Code formatting
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.)
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.
Edited 2 Years Ago by guideseeq
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.
Edited 2 Years Ago by JamesCherrill
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.
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.
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...
Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click Dim txtB1 As New TextBox Dim i For i = ...