Miscellaneous Java Algorithms

A great benefit of using the Java programming language is the huge class library available. Not only does Sun provide a large class library in their Java SDK (Software Development Kit), but there are extensive, freely available, libraries available from organizations like the Apache Project. If you can find the right class and use it in the correct way, you can avoid writing a great deal of code. But this begs the question... How do you find the right class and how do you understand how to use it, given that the Javadoc documentation is frequently less that complete?

A great reference to answer those "how do I do X in Java" questions is Java Cookbook by Ian F. Darwin. However, as extensive and useful as this book is, there are still algorithms that I have not found in it. Frequently these are just a few lines of code, but many hours can be spent pouring through the documentation and trying out code to find the right solution. Since I hate to do this twice, I've provided a few of these algorithms here.

You may use any software lined to by this page (or shown on this page) under the BSD license:

Copyright (c) 2012, Ian Kaplan
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Ian Kaplan, February 12, 2000
Updated: January 2015