I'm in college, and I've recently started competing in programming competitions with my friends. These competitions involve solving algorithmic problems quickly. It's a lot of fun, but there's one problem: I'm forced to use Java. (My teammates use Java.)

Background: I'm a self-taught JavaScript programmer, and it hurts to write Java code. I find it very verbose and inflexible, and I feel slowed down when having to declare types and decide which of the eighty list data structure to use. I'm also frustrated about the lack of functional programming features and how verbose using regular expressions, arrays, and dictionaries are.

As an example, consider the problem of finding the length of the longest string of consecutive characters in a given string. So the string XX22BBBBccXX222 would give 4, for the string of four Bs. In Java, I'd have to loop through and manually count characters and manually keep track of the maximum. (That's at least as far as I'm aware -- I'm not as familiar with Java as I am with JavaScript.) In JavaScript, I'd find it like this:

var max = Math.max.apply(Math, str.match(/(.)\1*/g).map(function (s) { return s.length; }));

Much quicker and simpler, in my book.

The question: what are some Java features, techniques, or patterns well-suited for fast, algorithmic coding?

closed as too broad by user40980, gnat, user7007, jmort253, GlenH7 Nov 3 '13 at 20:55

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    There are none. – Euphoric Nov 3 '13 at 15:39
  • If you're looking for one-liner algorithms you're out of luck with most non scripted languages. Of course then all these competitions do is test your knowledge of the language rather than your general problem solving ability. – James Nov 3 '13 at 15:47
  • You will be unsatisfied by Core Java. However, there are other languages which run on JVM, which you may find satisfying. In addition, you may be able to find Python and C++ libraries that provide this succinctness. – rwong Nov 4 '13 at 1:45

I used to do a few programming competitions, and found that the most important things to know were:

  • The full APIs of your collection classes. Stacks, Sets, Maps and Queues are pretty critical to many algorithms.
  • All of the String processing tricks (regexes, string joining, parsing etc.). This is because Strings are often used as inputs / data types to manipulate
  • All of the variations of looping / iteration constructs - again these come up so frequently, you want to be able to select the right one and use it pretty much automatically.
  • Features of your IDE. Code completion, type checking, unit testing etc are very useful if you want to write code quickly while also being 99% confident that it is correct. The compiler/IDE can help you a lot.

The core of the problem is that you're not as familiar with Java as your friends.

Rather than try to look for a generic solution, I'd say the best solution here is the same as when trying to learn a language or codebase in other situations: More exposure.

In this case, the simplest thing would be to look at your friend's solutions to see what patterns they used, but might not be the most effective.

Another approach would be to take your Javascript and break it down into a more iterative form. For example:

Math.max.apply(Math, str.match(/(.)\1*/g).map(function (s) { return s.length; }));

There's 3 core conceptual pieces to this:

  • The regex for finding groupings
  • Converting each group to a length
  • Finding the maximum

Individually they're all pretty easy. Java has support for regex groups, all you need is a simple loop to get the lengths, and finding the maximum is just as easy. It can even be made simpler by combining those last two in Java - since all you need for output is the number, all you need is a single variable to hold the end result, rather than an entire array for all the string lengths.

Not the answer you're looking for? Browse other questions tagged or ask your own question.