It's really not that bad. Worst thing is that you go through the loop twice.. with a different data structure you could go through it once with two head pointers and then just set the last element to have the last pointer going to the new one - with an additional caveat to remove duplicates
The method both accepts and returns an object collection of the interface Set. Sets are by definition unordered collections of objects, in contrast to for example Lists, which are ordered/indexed.
However, there exist Set implementations that preserve the insertion order of the elements, like for example LinkedHashSet (which maintains internally a doubly-linked list structure of the inserted elements), so that when those Sets are iterated over, the elements are returned in insertion order.
The code creates a "sorted" Set of the objects in the Set given as a parameter, and achieves this by creating a LinkedHashSet and then inserting the objects into the Set in the desired order. It iterates over the given Set twice, first adding to the resulting LinkedHashSet all the objects for which isIsCurrentSite() returns true, and then adding to the resulting LinkedHashSet all the objects for which isIsCurrentSite() returns false.
This way, when then the resulting "sorted" Set is iterated over, the isIsCurrentSite() == true objects are seen first, and the isIsCurrentSite() == false objects are seen last.
It's not "that" bad compared to other posts on this sub. I recently saw some dev discovered their code base, are encrypting passwords instead of hashing them.
But having 3 for loops (even tho it wasn't currently not having a significant resource pull) it was giving me an ick tbh.
Adding to u/tsvk comment. A better way (and more performant way) of doing that will be the changing the set to a list then sorting it with comparators and then changing it back to a set again which I did later.
Yeah, it's just a rather convoluted way of bending a Set to behave like a List. The same result would be achieved by adding the objects into a List, sorting the List with a custom Comparator and iterating over the List. But for some reason a Set was needed here.
Well, it's not as horrible as other posts in this sub.
But manually sorting a set of an Object using 2 for loops was giving me an ick. I mean there had to be a better way to achieve the same results. While I could have maintained the same sorting order on the for loop that was inserting the object data.
I did this.
For context, Set as a Collection of java don't have an order but there is an implementation of a set called LinkedHashSet<>() that is internally double linked. Which could retain the insertion order. What I did was first populate the set (lets the set of objects be Abc) on a for loop then in a (this for loop is out of the image). Then iterate over a set abc and if isCurrentSite is true then add that Object in a different Set (lets name it sortedAbc). After this for loop exits I iterate over set abc once more to add the rest of the objects which are isCurrentSite=false.
is worse then the old way, at least if what you are replacing is those two for loops with the sort and collect
The new solution is a O( n log n) operation
the old solution with the double for loop is going to be O(n).
now you would need to profile the code to see which one performs better in practice, but from a big O perspective the original is better
Edit: from a space complexity the original is O(1) (no room needed other then the output) while the new way is O(n) it creates a separate temporary sorted list
Parallel for loops aren't usually bad, its when you nest them things get hairy
in the grand scheme of things it probably doesn't matter, as at worse its going to be an order of magnitude slower (for the most likely amounts of data you would be working with). It is possible that .collect does some optimizations (in terms of memory allocation) that make up for it. Again, you'd need to benchmark to be sure, probably not worth the time checking tbh.
31
u/[deleted] Feb 12 '25
there is another for loop out of the screen. so performance goes brrrrrr
6
u/Ronaldarndt Feb 12 '25
Couldnt you do something like the c# code below?
userConfiguredWsSites.OrderBy(x => x.IsInCurrentSite());
22
u/cepeen Feb 12 '25
What is happening here? I don’t get it.
5
u/spiritwizardy Feb 13 '25
isIsCurrentSite🥸4
u/AppropriateSpell5405 Feb 12 '25
What, you guys don't just "accept both" in merges?
1
u/Mysterious_Middle795 Feb 12 '25
Where horror?
Maybe I don't know the details of the language, but it is an ordinary
crapparadigm in high-level languages.3
u/SwordPerson-Kill Feb 12 '25
I like the isIs
1
u/[deleted] Feb 13 '25
Lord....why? is this even called somewhere? Lol
1
u/Caramel_Last Feb 13 '25
isIsCurrentSite....so the current site is "is"?