Monday, December 13, 2010
JDK7 and Java SE7
Sunday, November 21, 2010
Iterator vs Enumeration
Differences between Iterator & Enumeration:
Enumeration is twice as fast as Iterator and uses very less memory. Enumeration is very basic and fits to basic needs. But Iterator is much safer as compared to Enumeration, b’ coz it always denies other threads to modify the collection object which is being iterated by it. Whenever a second thread tries for that Iterator will throw a ConcurrentModificationException. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly.
See what happens to this code segment,
Vector
aVector.add("I");
aVector.add("am");
aVector.add("really");
aVector.add("good");
Enumeration
Iterator
// Traversal using Iterator
while(anItr.hasNext())
{
if (
// Means, Iterator won't allow object modification while it is
// getting traversed. Even in the same thread.
aVector.remove(index);
System.out.println(anItr.next());
}
// Traversal using Enumeration
while(anEnum.hasMoreElements())
{
if (
System.out.println(anEnum.nextElement());
}
But Iterator provides a safer way to remove elements from the underlying collection during the iteration with well-defined semantics. See the implementation of Iterator. But here the remove() is supported by only those implementations of Collection that supports element removal.
public interface Iterator
{
boolean hasNext();
Object next();
void remove(); // Optional
}
So the above program part can be re-writen as,
while(anItr.hasNext())
{
System.out.println(anItr.next());
if (
// Note:
// Before using anItr.remove(), the Iterator should
// point to any of its elements. The remove() removes the
// element which the Iterator corrently pointing to.
// Otherwise it will throw IllegalStateException
}
Note that Iterator.remove() is the only safe way to modify a collection during iteration. In Enumeration, there is “no safe way” to remove elements from a collection while traversing.
Thursday, October 28, 2010
AutoBoxing in java : A boon when used carefully!
I am sure this is a very known mistake that should be avoided, but, it costed me an hour to figure out why the application I am working on was not behaving appropriately. I was looping through a list to find out an object that has to be removed. And, then the index was to be used to remove the object. The Objects in the list had over-ridden the equals and hashcode method,and I could not use the 'indexOf' or 'remove' on the list to find the object and remove it. My mistake was to use Object Integer instead of native int. Well every thing worked fine, but When called remove on the list, the List was trying to remove the integer object instead of object at that integer index! Solution was simple as you al know!! Just an interesting point to be taken down in my diary.
private List
private void removeStockWithStockName(String symbol) {
Integer removalIndex = 0;
for(Stock stock : stocksSubscribed){
if(symbol.equalsIgnoreCase(stock.getSymbol())){
break;
}
removalIndex++;
}
stocksSubscribed.remove((int)removalIndex);
}
Thursday, July 15, 2010
Our men in uniform are fighting the nation's battle. We should not demoralise them
This is not the time to demoralise our men in uniform. They are fighting the nation's battles in extremely adverse conditions. Our politicians keep screwing up continuously, and we then send in the forces to bring back some semblance of order. But the human rights rabble-rousers would like us to believe that our armed forces are little more than criminals. This is completely unacceptable.
There is more rape and murder in lawless Delhi than in Kashmir. But we want to paint the armed forces as the villains of Kashmir. Libertarians need to think about the rights of uniformed Indians as much as civilians.
Let's be clear why the armed forces are there in the first place. Kashmir is where embattled secularism is trying to hold its o...
Thursday, June 3, 2010
Expectations and Disappointments...
May be I am too unsuited for what a "social" relationship means today's practical & professional era. I probably cannot change myself, because whenever I tried to, I felt as if I am "faking" to be something which I am not.... But, I do force myself occasionally, because there are some situation where I cannot afford to make others changes, instead of change myself. But it happens occasionally for small span and very hard do ...... :(
Sunday, January 17, 2010
My first blog!
Anyways, I never thought blogging was a very good idea, except if you want to do some tik-tak on the keyboard even in your free time!!
But still, here I am, with "My first blog"!