Help more people learn by sharing this post!

Tag Archives for " java "

Sorting Java Collections

Java collections are most of the data structures that come with the language. They share the same basics methods thanks to the collection interface. One of the most common things you may want to do with a collection, besides adding and removing elements, is sorting them.

Let’s see an example with ArrayList
Continue reading

Java: Regular Expressions

Regular expressions, regexp for short, allow us to build expressions for pattern matching. If you aren’t familiar with them you may want to check out some resources here.

In Java you can leverage the power of regexp using the pattern and matcher classes, you can import java.util.regex.* to make them available to your program. Let’s see some examples:

This is a simple method that takes in a pattern and a string to compare against and returns either true or false. We define patterns as strings, for example if we want to see if a word starts with any character but ends with “ello” we would call our method like this:

Which in this case will return true, as an alternative you could also use the String method startsWith()

Example of java regular expressions

Now lets say we want to find all words that match a certain pattern in a piece of text, rather than just comparing against a single word.

This method is a little more involved than the first one. First we need to compile our regexp and get a pattern object then we call the matcher method on our pattern object with the string we want to match against, and what we get is a matcher object which contains the results. Finally we loop over the results and print them to the screen.

Here is a possible call to our findWithPattern method.

For more details check the official documentation.

Ruby vs Java – Strings

Let’s see how we can do some basic operations with strings with 2 languages, starting with how we declare a string variable. This is Ruby vs Java!

As you may know Java is a strong typed language, which means you need to declare the variable type, Ruby infers the type from the contents.

– Length

This one is almost identical, in fact you could use () with Ruby but it’s not required so we leave it out.

– Obtaining individual characters

You can use a Ruby string like an array, in Java you will need to use the charAt method.

– Comparing

Notice how you can’t use == for comparing strings in Java.

– Replacing

These will only replace the word ‘strings’ once, if you wanted to do it for all the repetitions of the word you need to use gsub and replaceAll.

It’s important to remember that Java strings are immutable, meaning that they can’t be modified in place, but you can assign the return value to the same variable, which internally will create a new variable and assign the new value.

On Ruby while they are mutable but you still need to assign the output of sub/gsub because these methods don’t change the string, some methods in ruby have a variant that does change the variable, these usually end with ! (an exclamation mark) like sort! and uniq! in this case we could use sub!/gsub!

And finally here are the links for the documentation for the String class for Ruby and Java:

http://ruby-doc.org/core-1.9.3/String.html

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html