Here is the code with the doc strings left in
Java has the JIT compiler and it is a fairly smart compiler, you can do all of the tricks in the world, but you are not guaranteed that it will have any impact because Java is going to optimize things behind the scenes for you and in spite of you. So the question is: do you have any bottlenecks that show in your system, are there segments that you see unexpected delays?
My best words on optimization in Java comes down to this:
Write dumb code, code that is textbook example and quality--don't try to be tricky--the compiler will usually do a much better job at optimization than you can.
If there is obvious delays in segments of your code, then work on optimization, run performance tests and etc in those sections, but unless you are seeing a problem--you are usually chasing your tail because Java is optimizing behind the scenes and your efforts may have little to no impact.
J Awesome wrote:Hi I have the following code that I have written but I want to optimize it if possible, or change it up a little.
Well, the very first question then is: Does it work? Every time and for any input? Because there's absolutely no point in changing or optimizing anything until it does.
Second question: What does "change it up a little" mean? Phrases like that are vague, and suggest that you don't have a proper goal in mind - and that's a really bad way of working. So I'd definitely define exactly what you mean by "change it up" before you alter a single line of code.
A friend helped me with some of the concepts behind it and I would like to see other ways it an be implemented as well.
OK, well there are a couple of things missing:
1. Your class extends TextProcessor, but we have no idea what it does.
2. It uses a TextReader, but we have no idea what that does either.
3. Your program appears to do an awful lot of things with "words", yet you don't have a Word class.
4. You appear to put words straight into your HashSet, which means that "hello" and "Hello" will be treated as different words. Is that what you want?
Now to "efficiency":
1. You might find that a HashMap<String, AtomicInteger> is a bit faster, since you can increment AtomicIntegers without having to create a new object every time.
2. You appear to populate all your Collections very specifically, including separate ones for longest and shortest.
If you used a List instead of a Collection, and had a Comparator that compares by word length, you could simply plough hash.keyset() straight into it and then sort it by length. Then the shortest words will be at the start and the longest ones at the end.
It may actually take longer than what you're doing, but I suspect you'll then have a more useful collection for doing other things.
Finally, going back to a Word class, here's a rudimentary one that might give you some ideas:Do you see the idea? You now have a class that:
(a) Compares with other "words" lexically.
(b) Has an equals() method that is consistent with both compareTo() and hashCode().
(c) Returns its length and a vowel count.
which are all things that you seem to need.
My version treats "Hello" and "hello" as the same word, but you don't have to. The idea is to create a class that does what YOU want a "word" to do.