Friday, January 22, 2016

Java 7 Features That I Like


If Generics is my most favorite feature of Java 5, Java 7 has several features that I like because they make my life easier as a developer and my codes cleaner and shorter. Here are my favorite Java 7 features in no particular order:

Using String in switch statements

This feature is part of my wishlist since I saw that C# has this capability of using String type in switch statements. Before Java 7, the variable used in switch statement can only be integer, byte, short, char and enum. Now let’s take a look at the conventional way of comparing strings using if-else statements.


package com.melodycancode.java;

public class StringComparisonDemo1 {

 public static void main(String[] args) {
  String selectedOS = "Linux";
  
  if ("Windows".equals(selectedOS)) {
   System.out.println("You're using Windows operating system.");
  } else if ("Mac OS".equals(selectedOS)) {
   System.out.println("You're using Mac OS operating system.");
  } else if ("Linux".equals(selectedOS)) {
   System.out.println("You're using Linux operating system.");
  } else {
   System.out.println("Your operating system is not supported");
  }
 }
}

In Java 7, the above code can be simplified using String in switch statement.

package com.melodycancode.java;

public class StringComparisonDemo2 {

 public static void main(String[] args) {
  String selectedOS = "Linux";
  
  switch(selectedOS) {
   case "Windows":
    System.out.println("You're using Windows operating system.");
    break;
   case "Mac OS":
    System.out.println("You're using Mac OS operating system.");
    break;
   case "Linux":
    System.out.println("You're using Linux operating system.");
    break;
   default:
    System.out.println("Your operating system is not supported");
    break;
  }
 }
}

Take note though that string comparison in switch statement is case-sensitive.


Monday, January 4, 2016

Java Programming: Collections.emptyList() vs Collections.EMPTY_LIST


In Java, we both have Collections.emptyList() and Collections.EMPTY_LIST but many are confused about the difference of these two.

Collections.EMPTY_LIST returns a non type safe List, which means that you can add any type of objects to it.


List list = Collections.EMPTY_LIST;

Collections.emptyList() was introduced in Java 1.5 and returns a type safe List<T>. You can assign this list to a generic collection so you wouldn’t get the “unchecked” compiler warning. The compiler will automatically detect the type parameter of the generic method based on the target type. 

List<String> list = Collections.emptyList();

Take note though that both Collections.emptyList() and Collections.EMPTY_LIST return an immutable list (e.g. you cannot modify the list by adding, updating or deleting items). So if you want to return an empty list that you can still modify later on, then don’t use those two, instead use this approach:


List<String> list = new ArrayList<String>();

The downside of this approach is that new ArrayList<T>() will always create a new instance (extra memory consumption) while Collections.emptyList() and Collections.EMPTY_LIST will return the same static instance each time it is called.

Aside from the list collection, set and map also have the equivalent approach for setting an empty instance. 


Set set = Collections.EMPTY_SET;
Set<String> set = Collections.emptySet();
Map map = Collections.EMPTY_MAP;
Map<String, Integer> map = Collections.emptyMap();