Thursday, May 8, 2008

Difference between String and StringBuilder

C# and Java What is the difference between String and StringBuilder classes?

String is an object. Both String and StringBuilder class stores strings

String :

String is immutable i.e. Read only, non updatable.

Example: String somename = "Lavakumar";

We cannot change a String object after creating one.It means we can’t change the value internally.

If you want change the value of somename object a new object is created then the reference of somename object is changed to new object. The old object is collected by GC ( garbage collecter).

Example: String somename = "sangeetham";

New object with value “sangeetham” is created. Then the reference of somename variable changed from “Lavakumar” object to “sangeetham” object. And the old object “sangeetham” is collected by GC (garbage collector).

Example:

using System;

sing System.Text;

public class string

{

static void Main()

{

DateTime start = DateTime.Now;

string x = "";

for (int i=0; i < 100000; i++)

{

x += "~";

}

DateTime end = DateTime.Now;

Console.WriteLine ("Time taken: {0}", end-start);

}

}

It takes nearly 10 seconds. Double the number of iterations, and it takes over a minute.

The problem is that strings not changeable (immutable). Just because we're using "+=" here doesn't mean the runtime actually appends to the end of the existing string. In fact, x += "!"; is absolutely equivalent to x = x+"!";. The concatenation here is creating an entirely new string, allocating enough memory for everything, copying all the data from the existing value of x and then copying the data from the string being appended ("~"). As the string grows, the amount of data it has to copy each time grows too, which is why the time taken didn't just double when I doubled the number of iterations.

String Builder :

String Builder is mutable i.e. subject to change.(we can change the value, updatable)

Default some space is allocated for String Builder.

If you are using for loop definitely go to stringBuilder.

It gives better performance when we are adding more than 10 strings.

In this there is no object creation for each addition. So String Builder is faster than String.

String builder initially some default memory.

By using StringBuilder.Append() method we can append Strings.

Example:

using System;

using System.Text;

public class stringbuild

{

static void Main()

{

DateTime start = DateTime.Now;

StringBuilder builder = new StringBuilder();

for (int i=0; i < 100000; i++)

{

builder.Append("~");

}

string x = builder.ToString();

DateTime end = DateTime.Now;

Console.WriteLine ("Time taken: {0}", end-start);

}

}

It takes nearly 30-40ms only. The time taken is roughly linear in the number of iterations (i.e. double the iterations and it takes twice as long). It does this by avoiding unnecessary copying - only the data we're actually appending gets copied. StringBuilder maintains an internal buffer and appends to that, only copying its buffer when there isn't room for any more data. (In fact, the internal buffer is just a string - strings are immutable from a public interface perspective, but not from within the mscorlib assembly.) We could make the above code even more efficient by passing the final size of the string (which we happen to know in this case) to the constructor of StringBuilder to make it use a buffer of the right size to start with - then there'd be no unnecessary copying at all. Unless you're in a situation where you have that information readily to hand though, it's usually not worth worrying about - StringBuilder doubles its buffer size when it runs out of room, so it doesn't end up copying the data very many times anyway.

visualized comparison of both:

clip_image002[6]

                                                                                     no of iterates

clip_image001( blue)is the performance of the pure String approach.

clip_image002(red) is StringBuilder at its basic settings.

clip_image003(green) represents a StringBuilder initialized to the size of the final string.

StringBuilder performance can sometimes be a bit tricky because there’s kinda “break-even-point” you always keep in mind.

Conclusion:
If you have to concatenate a string more than 10 times, it’s better to use StringBuilder.

Read more.....(Thanks to yoda.arachsys)

Read more.....(Thanks to bka-bonn.de)

Friday, May 2, 2008

Java difference between thread start vs run methods

It is very impotent to know the difference between thread start run methods. both will executes the same run method. There is some very small but important difference between using start() and run() methods. Look at two examples below:

The result of running examples will be different.

Example : 1

Thread one = new Thread();
Thread two = new Thread();
one.run();
two.run();

In This Example  the threads will run sequentially: first, thread number one runs, when it exits the thread number two starts.

Example : 2

Thread one = new Thread();
Thread two = new Thread();
one.start();
two.start();

In This Example   both threads start and run simultaneously.

Example : 3

public class TesTwo extends Thread {
    public static void main(String[] args) throws Exception{
        TesTwo t=new TesTwo();
        t.start();   
        t.run();
        doIt();       
    }
public void run()

{
    System.out .println("Run");
}
    private static void doIt()

{
        System.out.println("doit. ");
    }
}

Output:

Run
Run
doit.

In this example the start(public void run())  and run methods will executes simultaneously.Because after execution of start statement there are two simultaneous ways.

           t.start(); 

      clip_image002clip_image001

t.run();      public void run()

the two lines will executes simultaneous . so the output will print Run Run in output. after completion of public void run method then controller will return to doIt method. At last the method do It will execute.

Example : 4

public class TesTwo extends Thread {
    public static void main(String[] args) throws Exception{
        TesTwo t=new TesTwo();       
        t.run();
        t.start();
        doIt();       
    }
public void run(){
    System.out .println("Run");
}
    private static void doIt() {
        // TODO Auto-generated method stub
        System.out.println("doit. ");
    }
}

Output:

Run
doit.
Run

In this example the  run method will executes synchronously.so the in output it prints first line  "Run". Next start and doit methods will executes simultaneously.Because after execution of start statement there are two simultaneous ways.

           t.start();  

      clip_image002clip_image001

doIt();      public void run()

the two lines will executes simultaneous . so the output will print " doit. Run" in output second and third line.

Conclusion: The start() method call run() method asynchronously does not wait for any result, just fire up an action), while we run run() method synchronously - we wait when it quits and only then we can run the next line of our code.

Start()--> asynchronous.

run() -->synchronous.

Wednesday, April 2, 2008

java Difference between LinkedList ArrayList TreeList:

There are some differences between an java.util.ArrayList and java.util.LinkedList. These differences are not so important for any small applications that processes small amount of data. Then they can very important for any small application that processes huge amount of data. These do affect the performance of a application if not analyzed properly.

An empty LinkedList takes 2-3 times less memory than an empty ArrayList. This becomes important if you plan to keep an adjacency list of nodes for each node in the graph (graph can be like a TreeSet. Tree is a type of graph) and you know beforehand that the nodes will have at most one or two adjacent nodes and the total number of nodes in the graph can be quite large.


Although both ArrayList and LinkedList allow insertion/deletion in the middle through similar operations (ie; by invoking add(int index) or remove(index)), these operations do not offer you the advantage of O(1) insertion/deletion for a LinkedList. For that, you must work through a ListIterator.


The important thing to remember when comparing LinkedList and ArrayList is that linked lists are more faster when inserting and removing at random locations in the list multiple times. If you want to add to the end of the list then an ArrayList would be the best choose.

I will try to supply an answer for the LinkedList vs ArrayList issue. The first thing to do is to look at what interfaces these two implement. This might hint us at their purpose, as Sun usually bound purposes into interfaces.

// lang java
public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

public class LinkedList
extends AbstractSequentialList
implements List, Queue, Cloneable, Serializable
We can ignore Cloneable and Serializable as all of the JCF implement those. It’s also quite obvious that both of them implement the List interface, as they need to provide list functionability (backwards iteration, sub listing, etc).
Now for the differences: ArrayList is implementing RandomAccess while LinkedList implementing Queue. You could already tell something about the usage of the two classes.
ArrayList:
Now for some implementation notes. The ArrayList is actually encapsulating an actualy Array, an Object[]. When you instanciate ArrayList, an array is created, and when you add values into it, the array changes its size accordingly. This gives you strengths and weaknesses:
• Fast Random Access
You can perform random access without fearing for performence. Calling get(int) will just access the underlying array.
• Adding values might be slow When you don’t know the amount of values the array will contain when you create it, a lot of shifting is going to be done in the memory space when the ArrayList manipulates its internal array.
• Slow manipulation When you’ll want to add a value randomly inside the array, between two already existing values, the array will have to start moving all the values one spot to the right in order to let that happen.
LinkedList
The LinkedList is implemented using nodes linked to each other. Each node contains a previous node link, next node link, and value, which contains the actual data. When new data is inserted, a node is inserted and the links of the surrounding nodes are updated accordingly. When one is removed, the same happens - The surrounding nodes are changing their links and the deleted node is garbage collected. This, as well, gives strengths and weaknesses:
• Fast manipulation As you’d expect, adding and removing new data anywhere in the list is instantanious. Change two links, and you have a new value anywhere you want it.
• No random access Even though the get(int) is still there, it now just iterates the list until it reaches the index you specified. It has some optimizations in order to do that, but that’s basically it.
Conclusion:
ArrayList is very useful when a well defined set of data is needed in a List interface as opposed to an array. It can be dynamically changed, but try not to do so frequently throughout the life of the application. LinkedList is there for you to do just that: Manipulating it is very easy, and as long as its used for iteration purposes only and not for random accessing, it’s the best solution. Further, if you need random accessing from time to time, I suggest toArray for that specific moment.
Another point I didn’t raise here is the Queue issue. LinkedList implements extended abilities to the normal List interface which allows it to add and remove elements from its beginning and end. This makes the LinkedList perfect for Queue and Stack purposes - Although in Java 5 they already added a Stack class.

Java Collections Lists (ArrayList LinkedList TreeList) and difference:

The list Interface extends Collection and it stores a sequence of elements as in list , all these elements can be easily accessed with their serial numbers or sequences and any element can be inserted to the list. But we have the situations when we get UnsupportedOperationException if the collection can’t be modified. List interface is implemented by three concrete classes

1) Array List

2) Linked List

3) Tree List

1. ArrayList

This class extends AbstractList and implements the List interface.This class can be said to be the improved version of the arrays as in arrays we have the limitation of the fixed size which has to be known and defined well in advance and the n nothing much can be done with respect to the increase and decrease in the size of the array , so we have got the extended class from collections framework whch gives us the flexibility of having the variable array structure which can be dynamically increased or reduced as per the programmers need. An ArrayList is a variable-length array of object references.

ArrayList has three constructors shown here:

a) Array list ():
This constructor builds an empty array list.

b) ArrayList(Collection c):
This constructor builds an array list that is initialized with the elements of the collection c.

c) ArrayList (int capacity):
This constructor builds an array list that has the specified initial capacity. The capacity grows automatically as elements are added to an array list.

import java.util.ArrayList;
import java.util.List;

public class CreateArrayList {

public static void main(String args[]) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
}
}The above code will create an ArrayList object and store three String objects in the ArrayList object.

import java.util.ArrayList;
import java.util.List;


public class DisplayArrayList {

public static void main(String args[]) {
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");

for(int i = 0; i < 3; i++) {
System.out.println(list.get(i));
}
}
}Output of the above code

1
2
3
Below code is an example where the arraylist contains many Employee objects. Try this code a see how it works.

import java.util.ArrayList;
import java.util.List;

public class Employees {

public static void main(String args[]) {
List employeesList = new ArrayList();
employeesList.add(new Employee("Sam", 100000));
employeesList.add(new Employee("Rohan", 200000));
employeesList.add(new Employee("John", 300000));
employeesList.add(new Employee("Willey", 400000));

int size = employeesList.size();
for (int i = 0; i < size; i++) {
Employee employee = (Employee) employeesList.get(i);
System.out.println(employee.toString());
}
}
}

class Employee {

private String name;

private long sal;

public Employee(String name, long sal) {
this.name = name;
this.sal = sal;
}

public String getName() {
return name;
}

public long getSal() {
return sal;
}

public String toString() {

return "Name : " + getName() + "\n" + "Salary of " + getName() + ": "
+ getSal();
}
}


2. LinkedList :

LinkedList Class: This Class extends AbsractSequentialList and implements the List Interface. It as it defines automatically by it’s term provides a Linked-list structure.
It has two constructors which are mentioned below:

LinkedList ( ) LinkedList(Collection c)

LinkedList ( ): This constructor builds an empty Linked List. LinkedList(Collection c): This builds a linked list which is initialized with elements of collection .

Try the below code

import java.util.LinkedList;
import java.util.List;


public class LinkedListExample {

public static void main(String args[]) {

List linkedList = new LinkedList();
linkedList.add("1");
linkedList.add("2");
linkedList.add("3");
}
}
3. TreeList :

public class TreeList extends AbstractList (Code)
A List implementation that is optimised for fast insertions and removals at any index in the list.
This list implementation utilises a tree structure internally to ensure that all insertions and removals are O(log n). This provides much faster performance than both an ArrayList and a LinkedList where elements are inserted and removed repeatedly from anywhere in the list.

The following relative performance statistics are indicative of this class:

get add insert iterate remove
TreeList 3 5 1 2 1
ArrayList 1 1 40 1 40
LinkedList 5800 1 350 2 325

ArrayList is a good general purpose list implementation. It is faster than TreeList for most operations except inserting and removing in the middle of the list. ArrayList also uses less memory as TreeList uses one object per entry.
LinkedList is rarely a good choice of implementation. TreeList is almost always a good replacement for it, although it does use sligtly more memory.

Difference between LinkedList ArrayList TreeList:
There are some differences between an java.util.ArrayList and java.util.LinkedList. These differences are not so important for any small applications that processes small amount of data. Then they can very important for any small application that processes huge amount of data. These do affect the performance of a application if not analyzed properly.

An empty LinkedList takes 2-3 times less memory than an empty ArrayList. This becomes important if you plan to keep an adjacency list of nodes for each node in the graph (graph can be like a TreeSet. Tree is a type of graph) and you know beforehand that the nodes will have at most one or two adjacent nodes and the total number of nodes in the graph can be quite large.


Although both ArrayList and LinkedList allow insertion/deletion in the middle through similar operations (ie; by invoking add(int index) or remove(index)), these operations do not offer you the advantage of O(1) insertion/deletion for a LinkedList. For that, you must work through a ListIterator.


The important thing to remember when comparing LinkedList and ArrayList is that linked lists are more faster when inserting and removing at random locations in the list multiple times. If you want to add to the end of the list then an ArrayList would be the best choose.

I will try to supply an answer for the LinkedList vs ArrayList issue. The first thing to do is to look at what interfaces these two implement. This might hint us at their purpose, as Sun usually bound purposes into interfaces.

// lang java
public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

public class LinkedList
extends AbstractSequentialList
implements List, Queue, Cloneable, Serializable
We can ignore Cloneable and Serializable as all of the JCF implement those. It’s also quite obvious that both of them implement the List interface, as they need to provide list functionability (backwards iteration, sub listing, etc).
Now for the differences: ArrayList is implementing RandomAccess while LinkedList implementing Queue. You could already tell something about the usage of the two classes.
ArrayList:
Now for some implementation notes. The ArrayList is actually encapsulating an actualy Array, an Object[]. When you instanciate ArrayList, an array is created, and when you add values into it, the array changes its size accordingly. This gives you strengths and weaknesses:
• Fast Random Access
You can perform random access without fearing for performence. Calling get(int) will just access the underlying array.
• Adding values might be slow When you don’t know the amount of values the array will contain when you create it, a lot of shifting is going to be done in the memory space when the ArrayList manipulates its internal array.
• Slow manipulation When you’ll want to add a value randomly inside the array, between two already existing values, the array will have to start moving all the values one spot to the right in order to let that happen.
LinkedList
The LinkedList is implemented using nodes linked to each other. Each node contains a previous node link, next node link, and value, which contains the actual data. When new data is inserted, a node is inserted and the links of the surrounding nodes are updated accordingly. When one is removed, the same happens - The surrounding nodes are changing their links and the deleted node is garbage collected. This, as well, gives strengths and weaknesses:
• Fast manipulation As you’d expect, adding and removing new data anywhere in the list is instantanious. Change two links, and you have a new value anywhere you want it.
• No random access Even though the get(int) is still there, it now just iterates the list until it reaches the index you specified. It has some optimizations in order to do that, but that’s basically it.
Conclusion:
ArrayList is very useful when a well defined set of data is needed in a List interface as opposed to an array. It can be dynamically changed, but try not to do so frequently throughout the life of the application. LinkedList is there for you to do just that: Manipulating it is very easy, and as long as its used for iteration purposes only and not for random accessing, it’s the best solution. Further, if you need random accessing from time to time, I suggest toArray for that specific moment.
Another point I didn’t raise here is the Queue issue. LinkedList implements extended abilities to the normal List interface which allows it to add and remove elements from its beginning and end. This makes the LinkedList perfect for Queue and Stack purposes - Although in Java 5 they already added a Stack class.

Set

Set:
A Set represents a mathematical set.
It is a Collection that, unlike List, does not allow duplicates.
There must not be two elements of a Set, say e1 and e2, such that e1.equals(e2).
The add method of Set returns false if you try to add a duplicate element.

import java.util.HashSet;
import java.util.Set;
public class MainClass {
public static void main(String[] a) {
Set set = new HashSet();
set.add("Hello");
if (set.add("Hello")) {
System.out.println("addition successful");
} else {
System.out.println("addition failed");
}
}
}

This tutorial is about Sets (java.util.Set interface). I assume that the audience have mathematical background of set theory.

Sets cannot contain duplicate elements and you cannot add elements at a certain position unlike List. The classes implementing the Set interface are:AbstractSet, HashSet, LinkedHashSet, TreeSet

Set has 3 general purpose implementations:

- HashSet
- TreeSet
- LinkedHashSet

HashSet does not offer any ordering of elements. So HashSet is not the right choice if you value oriented operations are required. For that, TreeSet is proffered. But it is true to say that HashSet is used more often than TreeSet because HashSet is more faster and the reason is obvious i.e no ordering of elements. LinkedHashSet is something in between HashSet and TreeSet. It is as fast as HashSet and is implements using HashSet and LinkedList. The benefit of LinkedHashSet is that it provides ordering feature but the cost of ordering is not even close to what provided by TreeSet.

HashSet

HashSet implement Cloneable, Collection, Serializable and Set interface.
Since HashSet implements Cloneable interface, it means that HashSet can use clone() method to make a copy (field to field) of its object. HashSet is also serializable, so it can be saved on disk and then retrieved later for use.
JobStateReasons and LinkedHashSet are direct subclasses of HashSet.

HashSet has 4 constructors:

HashSet()
HashSet(Collection c)
HashSet(int initialCapacity)
HashSet(int initialCapacity, float loadFactor)

If you use default HastSet constructor, it will create an empty HashSet of default initial capacity (16) and load factor (0.75).

Add() method of the HashSet add the object into the storage if it is not already present. Duplicates are not allowed as it’s a set.

Lets review the code below. We will just add unique values to a HashSet and will display the contents of HashSet.
Code:
String [] strArray = new String[10];

for loop strArray.length with i
strArray[i] = "Element" + i;

HashSet s = new HashSet();

for loop strArray.length with i{
if(!s.add(strArray[i]))
System.out.println("Duplicate found : " + strArray[i]);
}

System.out.println(s.size() + " distinct words detected : " + s );
}
Output:
Code:
10 distinct words detected : [Element6, Element7, Element1, Element9,
Element3, Element8, Element2, Element0, Element5, Element4]
As you know, HashSet does not allow duplicates, so lets try this in the following example.
Code:
String [] strArray = new String[10];
for loop strArray.length with i
strArray[i] = "Element" + i;

strArray[1] = "Element2";
strArray[7] = "Element3";
strArray[8] = "Element4";

HashSet s = new HashSet();

for loop strArray.length with i{
if(!s.add(strArray[i]))
System.out.println("Duplicate found : " + strArray[i]);
}

System.out.println(s.size() + " distinct words detected : " + s );
}
Output:
Code:
Duplicate found : Element2
Duplicate found : Element3
Duplicate found : Element4
7 distinct words detected : [Element6, Element9, Element3, Element2, Element0, Element5, Element4]
We tried to add duplicates into the HastSet which is not allowed. Thing to note is, that no exception will be raised in case of duplicates. Also note the order of entries in HashSet. The order of elements is not maintained.

Creating a HashSet through a Collection

HashSet(Collection c) constructor is used to store a collection as a HastSet. It is very useful in scenarios where you have some collection (it can be a List (e.g. Vector/ArrayList) and you want to store it into a HashSet. Lets review the following example where a HashSet is created using a Vector.
Code:
Vector vec = new Vector();
vec.add("String1");
vec.add("String2");
vec.add("String3");
HashSet hs = new HashSet(vec);
Writing/Retriving HashSet to disk

Since HashSet implements Serializable interface, its objects can be written and retrieved from the disk. In other words, we can save the state of a HashSet and can retrieve it when needed.
Code:
HashSet hs = new HashSet();
hs.add("String1");
hs.add("String2");
hs.add("String3");

FileOutputStream f_out = new FileOutputStream("C:\\hashset.data");
ObjectOutputStream obj_out = new ObjectOutputStream (f_out);

obj_out.writeObject (hs);
System.out.println("HashSet written on the disk.");

FileInputStream f_in = new FileInputStream("C:\\hashset.data");
ObjectInputStream obj_in = new ObjectInputStream (f_in);
HashSet hashset =
new HashSet((HashSet)obj_in.readObject());

System.out.println("HashSet fetched from the disk.");

Iterator it = hashset.iterator();

while(it.hasNext())
System.out.println(it.next().toString());
Union and Intersection Example

Following is an interesting example of union and intersection. I assume that you are familiar with basics of set theory and know what union and intersection are.
Code:
public static void main(String[] args) {
Set s1 = new HashSet();
s1.add("Australia");
s1.add("Sweden");
s1.add("Germany");

Set s2 = new HashSet();
s2.add("Sweden");
s2.add("France");

Set union = new TreeSet(s1);
union.addAll(s2);

print("union", union);

Set intersect = new TreeSet(s1);
intersect.retainAll(s2);

print("intersection", intersect);
}

protected static void print(String label, Collection c) {

System.out.println("--------------" + label + "--------------");

Iterator it = c.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
Output:
Code:
--------------union--------------
Australia
France
Germany
Sweden
--------------intersection--------------
Sweden
TreeSet

Treeset implements Cloneable, Collection, Serializable, Set and SortedSet interfaces. This class maintains order of the elements in it according to the natural order of the elements which was missing in the HashSet.

TreeSet is not synchronized so if a TreeSet is concurrently accessed by threads then thread modifying the contents must be synchronized externally.

TreeSet provides following constructors:

TreeSet()
TreeSet(Collection c)
TreeSet(Comparator c)
TreeSet(SortedSet s)
Lets dig deep into TreeSet using example. In the following example, we created an empty TreeSet using default constructor. Then we added some values into it.
Code:
TreeSet ts = new TreeSet();
ts.add("String1");
ts.add("String3");
ts.add("String2");

Iterator it = ts.iterator();

while(it.hasNext())
System.out.println(it.next().toString());
Output:
Code:
String1
String2
String3
We used iterator to print the contents of the TreeSet. Thing to note is the order of storage. Order of storage is ascending one.

We cannot insert element at a particular index and also cannot fetch element from a particular index. This is done in List. Also duplicates are not allowed in TreeSet. This is a property of Sets. If you try to add duplicate value, no exception will be thrown. Lets do this with an example:
Code:
TreeSet ts = new TreeSet();
ts.add("Australia");
ts.add("Germany");
ts.add("Zambia");
ts.add("Australia");
ts.add("Zambia");

Iterator it = ts.iterator();

while(it.hasNext())
System.out.println(it.next().toString());
Output:
Code:
Australia
Germany
Zambia
Output suggests two things. The objects are stored in sorted order and duplicates are not allowed.

Contents of TreeList can be cleared using clear() method. Following example creates a TreeSet from a Vector and then clears the contents.
Code:
Vector vec = new Vector();
vec.add("String3");
vec.add("String1");
vec.add("String3");

TreeSet ts = new TreeSet(vec);
Iterator it = ts.iterator();

while(it.hasNext())
System.out.println("TreeSet: " + it.next().toString());
ts.clear();
while(it.hasNext())
System.out.println("TreeSet_Cleared: " + it.next().toString());
Output:
Code:
TreeSet: String1
TreeSet: String3
Creating a TreeSet through a Collection

A TreeSet can be created using any collection. TreeSet(Collection c) is the constructor to use in such condition. Any collection for example: Vector, ArrayList, HashSet or any can be used to create a TreeSet. Following is an interesting example:
Code:
Vector vec = new Vector();
vec.add("String3");
vec.add("String1");
vec.add("String3");

Iterator it = vec.iterator();

while(it.hasNext())
System.out.println("Vector: " + it.next().toString());

TreeSet ts = new TreeSet(vec);
it = ts.iterator();

while(it.hasNext())
System.out.println("TreeSet: " + it.next().toString());
Output:
Code:
Vector: String3
Vector: String1
Vector: String3
TreeSet: String1
TreeSet: String3
Vector is List that can contain duplicates and its not ordered. In the example above, we created a TreeSet from a Vector that contained an unordered list of objects along with duplicates. Some of you might thing that its not possible to create a TreeSet from it but it is possible. The output of the above example shows that a TreeList is created from the Vector with all the duplicated removed. Furthermore, contents of newly created TreeList are ordered.

Can we create a TreeList from an exiting HashSet? Since HashSet is also a collection, so answer is yes. The following example first creates a HashSet, populates it and then creates a TreeSet using already created HashSet.
Code:
HashSet hs = new HashSet();
hs.add("String2");
hs.add("String3");
hs.add("String1");
hs.add("String2");

TreeSet ts = new TreeSet(hs);

Iterator it = ts.iterator();

while(it.hasNext())
System.out.println("TreeSet: " + it.next().toString());
Output:
Code:
TreeSet: String1
TreeSet: String2
TreeSet: String3
TreeSet with generics

Java 5 introduced generics which adds more power to Java. TreeSet stores objects in it, but that object can be of any type. It can be a String, Integer, Float or even a custom object. With generics, you can specify the type of objects that are allowed to be stored in a TreeSet. In the following example, I tried to store an integer into a Vector that is bounded by String.
Code:
TreeSet ts = new TreeSet();
ts.add(1);
ts.add(2);
ts.add(3);
ts.add("String2");
Output:
Code:
The method add(Integer) in the type TreeSet is not applicable for the arguments (String)
TreeSet Example

You now will have some understanding of TreeSet. Review the example below. It has some interesting lines of code.
Code:
SortedSet sortedSet = new TreeSet(Arrays
.asList("one two three four five six seven eight".split(" ")));
System.out.println(sortedSet);
Object low = sortedSet.first();
Object high = sortedSet.last();
System.out.println(low);
System.out.println(high);
Iterator it = sortedSet.iterator();

System.out.println("low: " + low);
System.out.println("high: " + high);
System.out.println(sortedSet.subSet(low, high));
System.out.println(sortedSet.headSet(high));
System.out.println(sortedSet.headSet(low));
System.out.println(sortedSet.tailSet(low));
System.out.println(sortedSet.tailSet(high));
Output:
Code:
[eight, five, four, one, seven, six, three, two]
eight
two
low: eight
high: two
[eight, five, four, one, seven, six, three]
[eight, five, four, one, seven, six, three]
[]
[eight, five, four, one, seven, six, three, two]
[two]
As we already know, ordering is important in TreeList. This is shown in the example below. We also see methods like first, last, headset, tailSet which produced interesting results.

Method headSet() returns a view of the portion of this set whose elements are strictly less than toElement.
Method tailSet() returns a view of the portion of this set whose elements are greater than or equal to fromElement.

Differences between HashSet and TreeSet

Many developer are confused about sets and ask when to use HashSet and when to use TreeSet. Keep following points in mind for deciding the right set.
- HashSet is much faster than TreeSet.
- HashSet offers no ordering guarantees.
- TreeSet is preffered when performing operations in the SortedSet.
- HashSet can be tuned by specifying the right initial capacity using the constructor HashSet(int initialCapacity).
- HashSet can also be tuned used load factor parameter. If you choose right value for this, it can boost the performance a bit but there there won't be a big difference.
- TreeSet has no tuning parameters.

LinkedHashSet

LinkedHashSet handles collisions by using hash-bucket approach to avoid linear probing and rehashing. It uses doubly-linked list to maintain the insertion order. LinkedHashSet can accept the null entry and it is not synchronized.

LinkedHashSet implements Cloneable, Collection, Serializable and Set interface. It’s a set to no duplicates are allowed. LinkedHashSet has following 4 constructors:

LinkedHashSet()
LinkedHashSet(Collection c)
LinkedHashSet(int initialCapacity)
LinkedHashSet(int initialCapacity, float loadFactor

Thing will become obvious when you start using LinkedHashSet. Lets take an example to make things easier to understand.
Code:
LinkedHashSet lhs = new LinkedHashSet();

lhs.add("String1");
lhs.add("String9");
lhs.add("String7");
lhs.add("String6");
lhs.add("String7");

Iterator it = lhs.iterator();

while(it.hasNext())
System.out.println("LinkedHashSet: " + it.next().toString());
Output:
Code:
LinkedHashSet: String1
LinkedHashSet: String9
LinkedHashSet: String7
LinkedHashSet: String6
We declared a LinkedHashSet and populated it. Then we used iterator to print the contents of LinkedHashSet. The output suggests that the order of insertion is maintained and duplicates are not allowed.

Set Example

Now we know what HashSet, TreeSet and LinkedHashSet are and when to use these. Lets review the following example that will use all of the 3 sets and will clear a lot of confusions:
Code:
package set;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class HastSetTest {

static void fillSet(Set set) {
set.addAll(Arrays.asList("Zambia Australia Germany France".split(" ")));
}

public static void testSet(Set set) {
System.out.println(set.getClass().getName().replaceAll("\\w+\\.", ""));
fillSet(set);
fillSet(set);
fillSet(set);
System.out.println(set);
set.addAll(set);
set.add("Germany");
set.add("Germany");

System.out.println(set);
System.out.println("s.contains(\"Zambia\"): " + set.contains("Zambia"));
}

public static void main(String[] args) {
testSet(new HashSet());
testSet(new TreeSet());
testSet(new LinkedHashSet());
}
}
Output:
Code:
HashSet
[Germany, France, Zambia, Australia]
[Germany, France, Zambia, Australia]
s.contains("Zambia"): true
TreeSet
[Australia, France, Germany, Zambia]
[Australia, France, Germany, Zambia]
s.contains("Zambia"): true
LinkedHashSet
[Zambia, Australia, Germany, France]
[Zambia, Australia, Germany, France]
s.contains("Zambia"): true
The output suggests that no duplicate is added in any set. The order of elements is all of the 3 sets is different. LinkedList maintained the order of insertion. TreeSet maintained alphabetic order whereas HashSet does not kept any order. This is what we talked about above and this example confirms what we discussed.

Google
 

blogger templates 3 columns | Techzilo