aspose file tools*
The moose likes Beginning Java and the fly likes Algorithm efficiency for making a Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Algorithm efficiency for making a Set" Watch "Algorithm efficiency for making a Set" New topic
Author

Algorithm efficiency for making a Set

Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Does anyone know a more efficient way of making a set of numbers? succinct != efficient
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

How about using a java.lang.Set.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
you mean this? The interface in Java for using Sets?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Yes. Using autoboxing you can use a Set<Integer>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

AbstractSet? Maybe because it's abstract, and can't be instantiated?
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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

Joined: Jun 05, 2009
Posts: 82
So then this is a better (more efficient) way of writing my first bit of code?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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

Joined: Jun 05, 2009
Posts: 82
Well I ended up doing this but I think it is the same.




OOPS! No... no it is not the same.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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

Joined: Jun 05, 2009
Posts: 82
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.
 
wood burning stoves
 
subject: Algorithm efficiency for making a Set