Sep 9 2011

Remove Multiple Null Values From A List in Java

I’ve had situations where I’ve needed a list of foreign keys (fks) that I get from a result set and from that list make additional queries. Sometimes for whatever reason there are null values in the list and I have to remove them. You might had a similar problem where you needed to remove multiple occurring value from a lists in java. There are a few ways you can approach this problem. You you can remove each occurrence of a list element one at a time. The example below removes any null element in the list one at a time.

while(ids.contains(null)) {
	ids.remove(null);
}

There is another approach you can use to remove all instances of a given value from a list in one go. Instead of the remove method you can use the removeAll as in the following code sample.

ids.removeAll(Collections.singleton(null));

Of course, instead of null values you can remove all instances of any given value from a list based on your business needs.


Sep 8 2011

Launch Default Web Browser in Java

For the longest time I’ve used the BrowserLauncher library to open the default web browser to a specified web page from Java. BrowserLauncher is simple to use, just import edu.stanford.ejalbert.BrowserLauncher and call openURL method with the desired website URL.

Since Java 1.6, the JDK has introduced the java.awt.Desktop class to do the same so you don’t need an additional third party jar. The Desktop class has the ability to launch the desktop’s default email client and default web browser given a URI. Here is how you can launch the desktop’s default web browser in Java.

// Launch your default email client with ...
URI email = new URI("mailto:myemail@mydomain.com");
Desktop.getDesktop().mail(email);

// Launch your default web browser with ...
URI url = new URI("http://www.mydomain.com");
Desktop.getDesktop().browse(url);

Jun 27 2011

Where To Download Previous Versions of Java

Earlier this week I received the following Skype message from a co-worker.

[10:10:15 AM] i can not find an where to download official Java 6 update 23
[10:10:21 AM] mostly its from third party sites

I can’t even begin to state how many things I find wrong from the above message. Normally I would just reply with a link to Let Me Google That For You, such as the following search for Java 6 Update 23 download. I decided against being a wise guy, and found the Java archive site because it seen this question come up before for other software packages. Not all, but most software vendors such as Oracle, MySQL, Perforce, and others make available previous versions of their tools or software. It’s usually in a small link at the bottom of the main download the reads software archive, older version, previous releases, or something to that effect.

In the case of Java, Oracle has a Java Technology Products Download page where you can find the older versions of the JDK, JRE, and other Java framework and toolkits.


Apr 29 2011

Can You Spot the Infinite Loop?

Can you spot the infinite loop in the following snippet of code?

int i = 0;
{
    i++;
}while(i < 5);

The above code caused a critical bug in a application I was working on. At first sight, the code looks okay, especially since it compiles. It’s a do-while loop with a condition that seems that it would break when the variable i is equals or greater than 5. The variable i is set and incremented correctly but unfortunately this causes an infinite do nothing loop. Did you spot the problem? An important keyword is missing from the do-while loop, the do. Let me add comments to explain each statement as the compiler sees it…

// Initialize the variable i
int i = 0;

// Create a block and increment i by one
{
    i++;
}

// Infinite loop with an empty code block
while(i < 5);

The problem is that the while loop can have zero or one code block of statements (where a single statement can be a code block). If the state of what you are testing in the condition does not change because of the test, or because of the statement you are looping over, then you have an infinite loop. In the above code, because of a badly written do-while loop, this loop does not have statements that update the variables in the condition and so this while loop never breaks out.

The correct code would be the following…

int i = 0;
do {
    i++;
}while(i < 5);

Apr 27 2011

How To Print The Alphabet in Java?

In Java, the primitive value of type char can be converted to the primitive int. An an integer within the range of a character can be converted to a char. For example, the ASCII character code for the character A is 65, for B is 66, etc. Because of a char value can be interchanged with the integer value that represents its character code I can create a loop that starts from the letter A and stops at the character Z and I increment one character at a time. Here is the code snippet to print each letter of the alphabet in a loop.

for(char c = 'A'; c <= 'Z'; c++) {
    System.out.print(c);
}

Nov 25 2010

Retweet October 2010

From time to time I just blast tweets about software development, project planning, team dynamics, or whatever else comes to mind. Here is a synopsis of recent tweets and rants. If you want to follow the conversation follow me at techknow and/or juixe and I’ll be sure to follow back.

Software Development

  • If rhythm is a dancer, then algorithm is a break dancer.
  • If you were a Java language reserve word what word would you be? I would be volatile!
  • I get personally insulted when I am prompted to prove my humanness with a captcha. It makes me wanna bust a cap-tcha on some webdeveloper!!!
  • Is there group think in the development community? Yes, but we build patterns around group think and call it Best Practices.

Team Leadership

  • Change your perspective and you can change the world.
  • If you don’t have an original idea you can remix existing ideas in original ways!
  • Go hard, go home, go big. Pick two.
  • If a web site’s end users are not paying customers, then the end users are the product that web site then sell to their advertisers.
  • Why do people compensate their inability to communicate with the belief that others just know what they mean, you know what I mean?
  • Hype is the food of lemmings.
  • The plan was to have no plan, the backup plan was to leave the backup plan at home.
  • Most people are natural born followers, it’s human nature.
  • The toughest competition doesn’t always come from a competitor trying to build a clone of your product but from market shift in the industry

Product Placement

  • In terms of hardware, mobile, and even operating systems Microsoft is anywhere between 3-5 years behind the leader.
  • Startups age really fast in Internet time, by my calculation Digg is already and Old Media Company.
  • Like what percent of Tumblr’s posts are reblogs and reposts?
  • I want JJ Abrams to do a movie about do a remake of The Muppets in an alternate time line like he did for Star Trek.
  • I want an iPhone that transforms into an iPad when I need a bigger screen.
  • Google is an advertising company with great search technology. Facebook is a virtual share cropping company with great social technology.
  • Who collects more personal data and knows more about a given user, Facebook or Google?
  • Apple should add a few filters to their iPhone camera app.

Mini Meme Machine

  • Scotty and Christopher Walken Mashup: Captain, I’m giving her all’s she got.  She needs more cowbell.
  • You know who would be great in a reality television show? A prison gang! Imagine, Real World San Quentin.
  • r-EPO, the performance-enhancing drug of champions!
  • Monetize common sense because people don’t have it.
  • I want to trademark the & char so that I could file a trademark infringement to all law firms with names of the form Dumb Dumber & Dumbest.
  • The best part of a bagel is the creme cheese.
  • My all time historical hero is Johannes Kepler.
  • Your life comes with terms of service, batteries not included, void where prohibited.
  • In Silicon Valley, everyone drinks the kool-aid but using bottled artisan water from a 10,000 year old glacier.
  • In Silicon Valley, everyone is more interested in their piece of the pie than in the recipe of success.

Quote

  • I didn’t mean for it to be released so quickly because I wanted to control peoples’ being offended by it. – Mark Zuckerberg
  • I think people might be slightly offended but whatever, maybe there’s a way to control that. – Mark Zuckerberg
  • Quitting while you are ahead is not the same thing as quitting. – American Gangster
  • Living at home with your parents is a very powerful contraception. – David Willetts
  • We should start a new social media web 2.0 holiday: Friend, Fan, and Follower Appreciation Day!!!