Useful Java Snippets (3) logo

Welcome back again!

Hello all of you commenters. Welcome back to this ever growing popular series of Java enthusiasts. Yesterday we left off with the Java Collections framework and I showed you the List and Map, as well as some methods to iterate over these types. Today we will stay in the java.util package and discover even more beautiful things. As you will see this series is becoming more interesting every day!

Java.util contains utilities which make working with Arrays and Collections surprisingly easy, like comparing or converting from type to type and viseversa:

1. Comparing Arrays

// null arrays are equal
boolean[] bArr1 = null;
boolean[] bArr2 = null;
boolean b = Arrays.equals(bArr1, bArr2);                   // true

// Compare two boolean arrays
bArr1 = new boolean[]{true, false};
bArr2 = new boolean[]{true, false};
b = Arrays.equals(bArr1, null);                            // false
b = Arrays.equals(bArr1, bArr2);                           // true

// There are equals() methods for all eight primitive types
b = Arrays.equals(new byte[]{0}, new byte[]{0});           // true
b = Arrays.equals(new char[]{'a'}, new char[]{'a'});       // true
b = Arrays.equals(new short[]{0}, new short[]{0});         // true
b = Arrays.equals(new int[]{0}, new int[]{0});             // true
b = Arrays.equals(new long[]{0L}, new long[]{0L});         // true
b = Arrays.equals(new float[]{0F}, new float[]{0F});       // true
b = Arrays.equals(new double[]{0D}, new double[]{0D});     // true

// When comparing Object arrays, null elements are equal.
// If the elements are not null, Object.equals() is used.
b = Arrays.equals(new String[]{"a"}, new String[]{"a"});   // true
b = Arrays.equals(new String[]{null}, new String[]{null}); // true
b = Arrays.equals(new String[]{"a"}, new String[]{null});  // false
  1. Converting a Collection to an Array
// Create an array containing the elements in a list
Object[] objectArray = list.toArray();
MyClass[] array = (MyClass[])list.toArray(new MyClass[list.size()]);

// Create an array containing the elements in a set
objectArray = set.toArray();
array = (MyClass[])set.toArray(new MyClass[set.size()]);

// Create an array containing the keys in a map
objectArray = map.keySet().toArray();
array = (MyClass[])map.keySet().toArray(new MyClass[set.size()]);

// Create an array containing the values in a map
objectArray = map.values().toArray();
array = (MyClass[])map.values().toArray(new MyClass[set.size()]);
  1. Converting an Array to a Collection 
// Fixed-size list
List list = Arrays.asList(array);

// Growable list
list = new LinkedList(Arrays.asList(array));

// Duplicate elements are discarded
Set set = new HashSet(Arrays.asList(array));

Just like yesterday  this is Java 1.4 notation. Your compiler will probably complain  that the types should be set. It will however run since Java is downward compatible. With setting types I mean when you declare a List of Strings you should type:

List<String> myList = new ArrayList();

This is new since Java 5 but the smart programmers simply omit this and let Eclipse do the work since it will give you a warning when the type hasn’t been set. Just click the warning and Eclipse will suggest a few solutions. Click the right one and the code is corrected for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.