Saturday, December 28, 2013

Java Reflection

Reflection in Java is a feature that makes it possible to inspect classes, interfaces, methods at run-time without knowing the names of classes, interfaces, methods at compile-time. It is also possible to create objects, invoke methods, get/set field values using reflection.

References
Java Reflection Tutorial by Jakob Jenkov
Java Reflection API


Fear leads to Anger, Anger leads to Hate, Hate leads to Suffering

"Fear leads to Anger, Anger leads to Hate, Hate leads to Suffering."
Master Yoda, Star Wars

“Anger is an acid that can do more harm to the vessel in which it is stored than to anything on which it is poured."
― Mark Twain

"Holding anger is a poison. It eats you from inside. We think that hating is a weapon that attacks the person who harmed us. But hatred is a curved blade. And the harm we do, we do to ourselves."
― Mitch Albom, The Five People You Meet in Heaven

“Men in rage strike those that wish them best.”
― William Shakespeare

The intoxication of anger, like that of the grape, shows us to others, but hides us from ourselves.
CHARLES CALEB COLTON, Lacon

When your rage is choking you, it is best to say nothing.
OCTAVIA E. BUTLER, Fledgling

Whatever's begun in anger ends in shame.
BENJAMIN FRANKLIN, Poor Richard's Almanack, 1734

To be angry is to revenge the fault of others upon ourselves.
ALEXANDER POPE, "Thoughts on Various Subjects"

Speak when you are angry and you will make the best speech you will ever regret.
AMBROSE BIERCE, The Devil's Dictionary

An angry man is always a stupid man.
CHINUA ACHEBE, Anthills of the Savannah

Anger glances in the breasts of wise men; but rests in the bosom of fools.
WELLINS CALCOTT, Thoughts Moral and Divine

I was angry with my friend:
I told my wrath, my wrath did end.
I was angry with my foe:
I told it not, my wrath did grow.
WILLIAM BLAKE, Songs of Experience

“The best fighter is never angry.”
― Lao Tzu

Happiness comes from within, not without

“Happiness depends upon ourselves.”
“Folks are usually about as happy as they make their minds up to be” 

“All happiness depends on courage and work.”

“Happiness is having a large, loving, caring, close-knit family in another city.”

“Of all forms of caution, caution in love is perhaps the most fatal to true happiness.”

“Happiness in intelligent people is the rarest thing I know.” 

“Happiness is when what you think, what you say, and what you do are in harmony.” 

Friday, December 27, 2013

Java Collections

A Collection is an object that groups together several objects. The Java Collections framework comprises of...
  • Interfaces
  • Implementations
  • Algorithms

Interfaces & Implementations
The different collection implementations conform to one or more of the generic interfaces shown in the interface hierarchy below...
Interface Hierarchy

Some important Collection interface methods
  • size(), isEmpty()
  • add(), remove(), contains()
  • iterator(), toArray()
  • Bulk operations - addAll(), removeAll(), retainAll(), containsAll()
Bulk operations are so called because they operate on entire collections.

Set interface models the mathematical set abstraction. 
  • Duplicate elements are not allowed
  • No additional methods are defined in this interface
  • equals(), hashcode() methods can be used to compare different sets even though their implementations differ
  • Implementations
    • HastSet - hash table implementation (best performance for most scenarios)
    • TreeSet - Red-Black tree implementation
    • LinkedHashSet - hash table with a linked list running through it
List is an ordered collection
  • Positional access methods - get(), set(), add(), addAll(), remove(), indexOf(), lastIndexOf()
  • ListIterator<>, subList()
  • Implementations
Queue interface models the queue abstraction
  • Typically FIFO type of data structure (except in case of Priority Queues)
  • Two types of methods
    • Methods that throw exception on failure - add(), remove(), element()
    • Methods that return special value on failure - offer(), poll(), peek()
  • Implementations
Deque pronounced as deck is a double-ended queue. I like to think of it as a queue-stack hybrid
  • Permits operations at both ends of the queue
  • Two types of methods
    • Methods that throw exception on failure - addFirst/Last(), removeFirst/Last(), getFirst/Last()
    • Methods that return special value on failure - offerFirst/Last(), pollFirst/Last(), peekFirst/Last()
  • Implements the Queue interface - add(), remove(), offer()... are also available
  • Stack operations supported - push(), pop(), peek()
  • Implementations
Map is an object that maps keys to values. It models the mathematical function abstraction. 
  • No duplicate keys allowed
  • Methods - put(), get(), remove(), containsKey(), containsValue()
  • Collections views - keySet(), values(), entrySet()
  • Implementations
    • HastMap - hash table implementation 
    • TreeMap - Red-Black tree implementation
SortedSet maintains elements in ascending order or according to the order specified by the Comparator provided at creation time. Several additional methods are provided to take advantage of the ordering. Useful for modelling word lists, membership rolls etc

SortedMap maintains the keys in ascending order or according to the order specified by Comparator provided at creation time. Useful for modelling dictionaries, directories etc


Algorithms
Algorithms provide useful computations like sorting and searching over the elements of the collection. They are polymorphic and can be used across the different collection implementations.