Vishakha Lele

Greenhorn

Posts: 5

posted 9 months ago

Well, if you want the result to be a

If the original sets shouldn't be modified, you can do:

Or:

`LinkedHashSet`, you can just do the following:If the original sets shouldn't be modified, you can do:

Or:

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

posted 9 months ago

I moved your first line out of the code tags because it wasn't code. Look at line 68; there is no need for empty space there.

That method for combining the two sets looks very complicated, particularly the while loop. I am a little worried about the combining two trees like that. You start with two already sorted Sets and iterate them starting from their smallest elements. Let's imagine your original tree set had this architecture:-You get that by adding things in random order, and we are imagining you started with the middle element. Now no element in that tree is more than two levels from the root. Now you are using the Iterator and you add elements starting from the smallest. So you start with your Set having this structure and then you add 13.Then you keep adding.Now, you might be calling that a binary search tree, but every node has its left field pointing to

Go and find the src.zip file in your Java® installation folder unzip it and open the source for java/util/TreeSet.java and java/util/TreeMap.java. If you follow the details of the TreeSet#addAll method, you find it behaves differently for a TreeSet from other implementations. I haven't followed the details, but I am pretty sure that adding starts from the middle elements of (??the recipient??) set, so the new elements are fitted into the original tree structure as close as possible to their correct positions and maintain the correct structure of the tree. That is why you should use the built‑in methods wherever possible. They have been refined over twenty‑one years of experience to make sure they work correctly.

Stephan: nice solution to create a linked set with concat(). Is the toCollection method Collections.toCollection? OP may not know about the static import you would use for that method. Also where does concat() come from?

That method for combining the two sets looks very complicated, particularly the while loop. I am a little worried about the combining two trees like that. You start with two already sorted Sets and iterate them starting from their smallest elements. Let's imagine your original tree set had this architecture:-You get that by adding things in random order, and we are imagining you started with the middle element. Now no element in that tree is more than two levels from the root. Now you are using the Iterator and you add elements starting from the smallest. So you start with your Set having this structure and then you add 13.Then you keep adding.Now, you might be calling that a binary search tree, but every node has its left field pointing to

`null`, and its structure will behave exactly the same as this data structure:-It will behave as if you had turned it into a (sorted) linked list; searches and insertions will no longer run in log*n*time but in linear time.Go and find the src.zip file in your Java® installation folder unzip it and open the source for java/util/TreeSet.java and java/util/TreeMap.java. If you follow the details of the TreeSet#addAll method, you find it behaves differently for a TreeSet from other implementations. I haven't followed the details, but I am pretty sure that adding starts from the middle elements of (??the recipient??) set, so the new elements are fitted into the original tree structure as close as possible to their correct positions and maintain the correct structure of the tree. That is why you should use the built‑in methods wherever possible. They have been refined over twenty‑one years of experience to make sure they work correctly.

Stephan: nice solution to create a linked set with concat(). Is the toCollection method Collections.toCollection? OP may not know about the static import you would use for that method. Also where does concat() come from?

Stephan van Hulst

Saloon Keeper

Posts: 8580

161

posted 9 months ago

I have updated my post to give the static imports for both. I prefer to use static imports when working with stream operations, it makes the statement flow more like a story.

Campbell Ritchie wrote:Stephan: nice solution to create a linked set with concat(). Is the toCollection method Collections.toCollection? OP may not know about the static import you would use for that method. Also where does concat() come from?

`toCollection()`comes from

`Collectors`, and

`concat()`comes from

`Stream`.

I have updated my post to give the static imports for both. I prefer to use static imports when working with stream operations, it makes the statement flow more like a story.