Tuesday, 28 February 2012

TreeSet in Java with Examples

In TreeSet the elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.
Objects that implement Comparable interface can be used as the element in TreeSet.Use the following constructors for it.
  • public TreeSet()
  • public TreeSet(Collection<? extends E> c)
  • public TreeSet(SortedSet<E> s)
The Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

You can also create a TreeSet according to the specified comparator.Use the following constructor for it.
  • public TreeSet(Comparator<? super E> comparator)
If the specified comparator is null, the natural ordering of the elements will be used.

Sunday, 26 February 2012

Set operations: union, intersection, difference, symmetric difference, is subset, is superset - using TreeSet in Java

The mathematical set operations using Java Collection-Framework  is given below

import java.util.TreeSet;
import java.util.Set;
import java.util.TreeSet;

public class Main {
  public static  Set union(Set setA, Set setB) {
    Set tmp = new TreeSet(setA);
    tmp.addAll(setB);
    return tmp;
  }

  public static  Set intersection(Set setA, Set setB) {
    Set tmp = new TreeSet(setA);
   tmp.retainAll(setB);
    return tmp;
  }

Is StringBuffer really thread-safe ?

May I hope that you have gone through  Difference between StringBuffer and StringBuilder before reading this topic.

All the public methods within StringBuffer are synchronized, which means a method invocation on a StringBuffer instance acts as an atomic operation.i.e.a method execution by one thread can't be interrupted by another thread.

But one point to remember is that,two or more sequential method invocations on a StringBuffer instance by a thread is not synchronized i.e. it is not an atomic operation.

Consider one example

Saturday, 25 February 2012

What's the difference between a StringBuffer and StringBuilder?

StringBuffer is in older versions of the Java API and it is thread-safe.String buffers are safe for use by multiple threads. The public methods are synchronized so that each method invocation on a StringBuffer instance acts as an atomic operation.For example two threads can't append one data to a StringBuffer instance at the same time.i.e. one thread can invoke append(String) only after the other thread has returned from the append(String) method.

StringBuilder was introduced in java 1.5.StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized whereas StringBuilder is not synchronized( which implies it isn’t thread safe).

So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence of synchronization.

Wednesday, 22 February 2012

Converting from ArrayList to Array & from Array to ArrayList in Java

From ArrayList to Array

Java ArrayList provides two methods to get the array back from your ArrayList.
The methods are given below
  • public Object[] toArray()
  • public Object[] toArray(Object[] a)
Let us see the difference between these two methods

1) public Object[] toArray()

Returns an array containing all of the elements in this collection. If the collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.