When you're dealing with production code, it's nice to have a convenient sort utility method that works with Arrays and Collections. I think you might find this class useful, especially if you can use import static!
Code Pretty Print Script
Thursday, November 21, 2013
Tuesday, November 19, 2013
Converting Strings to Wrapper Types
Are you dealing with a lot of numeric data stored in strings? Don't you wish there were some simple utility wrappers to use? Try these:
Monday, November 18, 2013
Swapping Without Temporary Storage
Can you swap values in place (without temporaries) in Java? Yes!
This classic technique continues to work.
This classic technique continues to work.
Sunday, November 17, 2013
Even-Odd Optimization
When you want to test whether something is even or odd, don't use the expensive modulo operation like this -
for (int i = 0; i < 100; ++i) { if (i % 2 == 0) { System.out.println("i = " + i + " is even."); } else { System.out.println("i = " + i + " is odd."); } }You should probably use this instead -
Saturday, November 16, 2013
Stack (Unbound)
Implementing an abstract data type like an unbounded Stack allows its' use with a two stack arbitrary precision machine. This Stack provides peek(), pop() and push().
Inspired by Dan's question on Stack Overflow.
Given this "program", the Stack will have one number at the top (i.e. 12).
Inspired by Dan's question on Stack Overflow.
Given this "program", the Stack will have one number at the top (i.e. 12).
( 3 * 4 ) .
Friday, November 15, 2013
Reflections
Thursday, November 14, 2013
Mocking Up Text
Do you need to generate a significant ammount of text dynamically? Perhaps you're creating mock data to test your UI; maybe you've been using Lorem Ipsum; perhaps you know about "=rand()" in Word; or you could try a LoremIpsumGenerator.
Wednesday, November 13, 2013
It's all in the punctuation‽ #SaveTheDay
Following up from my earlier post about a Hex codec and in honor (or honour) of the upcoming Doctor Who 50th anniversary (ten days and counting), I proudly present a Dalek codec.
Tuesday, November 12, 2013
Need to write the same content to multiple OutputStream(s)?
Are you tired of writing the same content to multiple OutputStream(s)? Do you wish there was an equivalent to the tee command in Java?
Monday, November 11, 2013
Encoding and Decoding Hex
I was thinking about Hex Encoding/Decoding the other day (and the upcoming Doctor Who anniversary), and I came up with this solution. It may not be the fastest, but isn't it pretty?
Sunday, November 10, 2013
Safely Handling Strings
Dealing with a lot of Strings? Leading and trailing white space got you down? Wish there was a consistently easy approach to ensure that they're all trimmed and to guard against null?
Saturday, November 9, 2013
That API returns an Enumeration, but I wanted an Iterator!
You're working on a Servlet, and you need to iterate the parameter names but "getRequestParameterNames()" returns an Enumeration. You could use hasMoreElements and nextElement but it's completely inconsistent with your other (modern) Collections. There has to be an easier way!
Friday, November 8, 2013
Reverse the Polarity
Sometimes, you need to reverse the polarity of a Comparator and it should be smart so that you can reverse the Comparator over and over again without consuming more resources.
Thursday, November 7, 2013
Help! I have a Comparable but I need a Comparator
Are you tired of re-implementing a Comparator every time you encounter a Comparable? Regardless of whether you're working with String(s), Integer(s), Float(s) or "Anything"‽ else Comparable just use this ComparableComparator.
Clean Up After Yourself
When you open a lot of dynamic resources, are you tired of maintaining the finally block with excessive try - close - catch boilerplate? Does your project contain a lot of code that looks like this? Wouldn't it be nice if you could close(Connection, Statement, ResultSet) or close(ResultSet, Statement, Connection) or in fact call close on "Anything"‽ with a close method using "duck" typing?
Wednesday, November 6, 2013
Avoid Intermediate (Deep) Copies
The following code is provided for example purposes only!
For your enjoyment, three approaches to a singular problem; serializing a JSONObject to a Web client.
For your enjoyment, three approaches to a singular problem; serializing a JSONObject to a Web client.
Subscribe to:
Posts
(
Atom
)