• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Algorithm efficiency for making a Set

 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know a more efficient way of making a set of numbers? succinct != efficient
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using a java.lang.Set.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean this? The interface in Java for using Sets?
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Using autoboxing you can use a Set<Integer>.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always been confused about interfaces. How do I use them? This does not work because it is an interface so what does?



Looks like most people use HashSet... why? Why not Abstract?
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AbstractSet? Maybe because it's abstract, and can't be instantiated?
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I think i see using this example i found.


The print out seems to be inorder postorder preorder. So I guess you would use HashSet, TreeSet and LinkedHashSet depending on what order you wanted to insert them and retrieve them. Am I close?
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashSet is the fastest but has no predetermined order. TreeSet is slower but you have full control over the order of elements; usually it uses the results of Comparable's compareTo method but with a Comparator you can have any order you want. LinkedHashSet is a bit of a mix - the speed of HashSet while maintaining an order. Unlike TreeSet this order cannot be controlled much - it's either insertion order or retrieval order.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So then this is a better (more efficient) way of writing my first bit of code?
 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for "more efficient" you would have to time it, if you have an operating system which can actually time the tiny, tiny, interval that code would take to run. And of course that's supposing you meant "minimum execution time" when you said "efficiency" and not something else.

However by using TreeSet your resulting code does something different than what your original code did. The original code put the numbers into the array in the order they were received. A TreeSet stores them in numerical order. That's not the same. Use a LinkedHashSet if you want it to keep the entries in the order they were inserted in.

As for "better", you're halfway there. You still have the confusing fancy footwork for the loop index, only it's more confusing now. If you want to repeat trying to add an entry to the set until the set has 6 entries, then write code which does that in a straightforward way:
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I ended up doing this but I think it is the same.




OOPS! No... no it is not the same.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely not. This will already stop the loop the first time you add a new number, because add returns true and the loop condition is false. Change it to "while (add..." will also not work because then it stops on the first duplicate number; that could be as soon as the second iteration.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I was not printing the results and so did not notice the incorrect/unwanted behavior. I was hasty in my reply. I was between two ideas and I accidentally merged them. Turns out my brain is a poor repository. I ended up using your code because it was cleaner than the code I was using. This is what I had [that worked].



this forces a tight loop inside a bodyless while (which gives a warning in the compiler) to fall through the while only when a number was added.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic