Wednesday 24 October 2012

J-Fall 2012

The NLJUG[1] is once again organising J-Fall 2012[2]. It takes place on the 31st of October 2012 in Nijkerk, in the Dutch province of Gelderland.

I shall be visiting, and see what new things I can learn.

I hope to write some blogs about it. As such I thought I'd post my current programme here.
Time: 08:00 - 08:50 Early Bird sessions
Title: Your Product Owner is Just Better at Pretending
Speaker(s): Erwin van der Koogh

Time: 09:20 - 10:10 General Session
Title: Keynote - Oracle

Time: 10:40 - 11:30 Parallelsessions
Title: Java EE 7 Platform Overview and Highlights
Speaker(s): David Delabassee

Time: 11:35 - 12:25 Parallelsessions
Title: Java EE Multi-tenancy in Practice
Speaker(s): Frans van Buul

Time: 13.35 - 14.20 General Session
Title: Keynote

Time: 14:25 - 15:15 Parallelsessions
Title: Hands-on Lab: RRRADDD! ... Really Ridiculously Rapid Application Development (Domain-Driven)
Speaker(s): Dan Haywood, Jeroen van der Wal

Time: 15:45 - 16:35 Parallelsessions
Title: Hands-on Lab: MongoDB
Speaker(s): Maikel Alderhout

Time: 16:40 - 17:30 Parallelsessions
Title: Scala Through the Eyes of Java (8)
Speaker(s): Urs Peter

References

[1] NLJUG - Nederlandse Java Users Group
http://www.nljug.org
[2] J-Fall 2012
http://www.nljug.org/jfall/

Saturday 20 October 2012

Vector Deprecated?

Someone asked me to explain a comment of mine where I mentioned that the Vector class isn't used (much) any more.

Back in the old days, when I was programming and I needed a collection (there were no generics in sight yet), I'd use the Vector class as a convenient way of having a list of objects to maintain. Vector, at the time, could be filled with instances of supertype Object and I got stuck with a lot of casting down to the appropriate type I wanted.

The Java class Vector has been a part of the JDK since version 1.0.

In J2SE 1.2 (JDK 1.2) the Collections framework was introduced.

The following javadoc comments can be found on the Vector class.
As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.[1]

Also the following text is displayed in Netbeans if you do try to use the Vector class.[4]
"This inspection reports any uses of java.util.Vector or java.util.Hashtable. While still supported, these classes were made obsolete by the JDK1.2 collection classes, and should probably not be used in new development."

Basically, what I'm saying is, that Vector class was implemented before the Collections framework. As such it does not follow the Collections framework naming scheme of List,Set and Map. It also has a number of methods, for backwards compatibility, that are not to be found in any of the other collection classes.

If you do need synchronization, the Vector is indeed synchronized, yet it is synchronized on each and every operation. This is fine, if you do not need to execute a lot of operations as an atomic transaction. But, for instance, if you need to iterate over all the items in the Vector, it is always better to get a lock before the iteration and release it after the iteration.

So, in short:
  • use ArrayList, if you do not care for the synchronization aspect of Vector
  • use Collections.synchronizedList[2] if you need an array that is synchronized
  • use CopyOnWriteArrayList if you are dealing with multiple concurrent read operations and only a few write operations *)
  • use a native array, if you do not care for synchronization and performance is an issue.
*) be careful, as a write/add on a CopyOnWriteArrayList (as the name says) will create an entirely new Array populated with the old items and the new/added item. For large arrays this might be slow.

In the javadoc of synchronizedList there is a good example of iterating over it. I shall just write it down here for completeness.
List list = Collections.synchronizedList(new ArrayList());
      ...
synchronized (list) {
    Iterator i = list.iterator(); // Must be in synchronized block
    while (i.hasNext())
        foo(i.next());
}
As always in these things, the requirements you have, determine what kind of Collection you need to use.

Updates


23/10/2012: Updated because of the comment below on CopyOnWriteArrayList.

28/01/2013: Update: the same goes for HashTable (old) and HashMap (new).

References

[1] Vector
http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
[2] public   static  <T> List<T> synchronizedList(List<T> list)
http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#synchronizedList%28java.util.List%29
[3] Why is Java Vector class considered obsolete or deprecated?
http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated
[4] Is Vector an obsolete collection class?
http://www.coderanch.com/t/515352/java-developer-SCJD/certification/Vector-obsolete-collection-class
[5] Vector or ArrayList -- which is better?
http://www.javaworld.com/javaqa/2001-06/03-qa-0622-vector.html

Collections Empty List Factory Method

You can create an empty list like so:
List<String> s = Collections.emptyList();
However, the following doesn't work:
setStrings(Collections.emptyList());
Because the compiler cannot determine what the type should be at compile time. In the first example type inference comes to the rescue. In the second example, not soo much.

What, however, does work is the following syntax I came across:
setStrings(Collections.<String> emptyList());
It's a little odd at first, but looking at the source code of emptyList() reveals:
public static final <T> List<T> emptyList() {
    return (List<T>) EMPTY_LIST;
}
The <T> in front of List<T> indicates the type that is supposed to be inferred.

Please do not use EMPTY_LIST directly, if possible. It doesn't use generics.

References

[1] Java: Collections.emptyList() returns a List<Object>?
http://stackoverflow.com/questions/306713/java-collections-emptylist-returns-a-listobject