Monday, September 8

Why Iterator interface provides remove() method but no add() method?

The Interface Iterator provider as three method that are hasNext(), next() and remove(). 
This means that we can remove any element from a collection while iterating over it. so why we cannot add element to the collection. Why they don't provide add() method.

Answer is, that they can't allow you to concurrently read and add element to a collection. The Iterator work on any collection whether it Set, List or Map. So it does know very much the on which underlying it's going to work and we know all the collection implementation maintain ordering of elements based on some algorithm. For example  TreeSet maintains the order of elements in by implementation Red-Black Tree data structure. Therefore if we tried to add an element to TreeSet using the iterator at given index or position of iterator. it might corrupt the state of the underlying data structure. So, the add()method could change structural state of a data structure. 

Thursday, August 7

Difference between ClassNotFoundException vs NoClassDefFoundError

ClassNotFoundException Vs NoClassDefFoundError


ClassNotFoundException :  ClassNotFoundException occurs when class loader could not find the required class in class path. So, basically you should check your class path and add the class in the classpath.

NoClassDefFoundError : This is more difficult to debug and find the reason. This is thrown when at compile time the required classes are present , but at run time the classes are changed  or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath , but one of the classes which are required by this class , are either removed or failed to load by compiler .So you should see the classes which are dependent on this class.

Tuesday, August 5

Why Map dosen't implement Collection Interface?

Why doesn't Map extend Collection?

Reson 1)

Collection assume elements of one value. Map assumes entries of key/value pairs. They could have been engineered to re-use the same common interface however some methods they implement are incompatible e.g.
Collection.remove(Object) - removes an element.
Map.remove(Object) - removes by key, not by entry.

You could model a Map as a collection of entries, which is what Map.entrySet() does.

There are some methods in common; size(), isEmpty(), clear(), putAll/addAll() but these are unlikely to have much value as a stand alone interface. (Again Map.entrySet() can be used instead)

 

 Reson 2)

This was by design. We feel that mappings are not collections and collections are not mappings. Thus, it makes little sense for Map to extend the Collection interface (or vice versa).

Monday, August 4

Concept of Serialization

 What is Serialization?

Serialization is an API for encoding an objects as byte-stream and reconstructing objects from their byte-stream encodings. The serializing is process of encoding objects as byte-stream and reverse process is called deserializing.

Implementation Of Serializable

Implementation Serializable to an object allow's class instances to be serialized can be as simple as adding the words 'implements Serializable' to its declaration.

Friday, July 4

Regex for Validation

What is Regex?

Regex (or regular expression) is sequence of special characters that  match or find string by using pattern.
(Pattern is sequence characters in regex). Which can be use to search, validate or edit text and data.

Wednesday, February 19

Overloading vs Overriding in Java

Overloading and overriding are completely different. Only the notion about interface (function) name is same. Overloading is the ability to use same interface name but with different arguments. Purpose of functions might be same but the way they work will differ based on the argument types. Overriding is applicable in the context of inheritance. When there is a need to change the behavior of a particular inherited method, it will be overridden in the sub-class.

Ads Inside Post