Showing posts with label deprecated. Show all posts
Showing posts with label deprecated. Show all posts

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

Friday, 25 June 2010

Displaying the Date

I always always get in trouble when using Date, because of the huge number of deprecated methods it has. We should use Calendar almost exclusively.

Here's a simple example of a bad way and a good way to display a nice date:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DisplayDate
{

    public static void main(String args[]) 
    {
        // worse way
        Date myDate = new Date();
        int month = myDate.getMonth();
        int year = myDate.getYear();
        int day = myDate.getDate();
        System.out.println("Current date : " +
            day + "/" + (month + 1) + "/"
            + (year + 1900));
        Calendar cal = Calendar.getInstance();

        // bad way
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("Current date : " +
            day + "/" + (month + 1) + "/" + year);

        // good way
        SimpleDateFormat simpleDateFormat = 
            new SimpleDateFormat("dd/MM/yyyy");
        String s =
           simpleDateFormat.format(cal.getTime());
        System.out.println("Current date : " + s);

    }
}

References

Javadoc - SimpleDateFormat
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html