Sunday, May 4, 2014

Java Paper

  1. The static initializer block will be called on loading of the class, and will have no access to instance variables or methods. It is often used to create static variables.
  2. The non-static initializer block(anonymous block defined outside all member functions) on the other hand is created on object construction only, will have access to instance variables and methods, and  will be called at the beginning of the constructor, after the super constructor has been called (either explicitly or implicitly) and before any other subsequent constructor code is called. I've seen it used when a class has multiple constructors and needs the same initialization code called for all constructors. Just as with constructors, you should avoid calling non-final methods in this block. The non-static initializer is the only way to do initialization in an anonymous inner class, as those can't have constructors.                                                     
 For example:

Compare this straightforward one-liner, which places data and assignment together:

private static final Map<String, String> map = Collections.unmodifiableMap(    
new HashMap<String, String>() {
{        
put("foo", "bar"); put("one", "two"); // etc    
}
});

With this mess, which must create a separate object due to final only allowing one assignment:

private static final Map<String, String> map;
static {    
Map<String, String> tempMap = new HashMap<String, String>();    
tempMap.put("foo", "bar");    
tempMap.put("one", "two"); // etc    
map = Collections.unmodifiableMap(tempMap);
}
 

--------------------------------------------------------------------------------------------------------------------------
How to remove elements from a collection while iterating:

Iterator: remove( ):

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next(). The behaviour of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

Working Example:

while(iterator.hasNext()){
iterator.next();
iterator.remove();
 }

 

Throws:
IllegalStateException: - if the next method has not yet been called, or the remove method has already been called after the last call to the next method
Example for IllegalStateException:

while(iterator.hasNext()){
// next() is not called
iterator.remove();
 }

while(iterator.hasNext()){
iterator.remove();
//next() is called after remove()
iterator.next();
 }

Note:
  1. Enumeration has2 methods: hasMoreElements(), nextElement()
  2. Iterator has 3 methods: hasNext(), next(), remove(). It is an interface
  3. ListIterator: A sub-interface of Iterator, and allows the programmer to traverse the list in either direction, modify the list during iteration
  4. If you want to get iterator for Map, then you can get it by calling on entrySet() of Map
  5. If you try to remove elements using for loop, you will get: java.util.ConcurrentModificationException