All about technology

A blog dedicated to technology, real world life and moral values.

How to Iterate backwards through a List?

How to Iterate backwards through a List?

Do you know how to iterate backwards in a ArrayList?

Didn’t know, there is a ListIterator, which can be sued to iterate backwards in a list.

List<String> theTestList = new ArrayList<String>(); 
theTestList.add("a"); 
theTestList.add("b"); 
theTestList.add("c"); 
ListIterator<String> listIterator = theList.listIterator(theList.size()); 
while (listIterator.hasPrevious()) { 
  System.out.println(liter.previous()); 
}

How to use Collections.EMPTY_LIST with Generics?

How to use Collections.EMPTY_LIST with Generics?

Today I stumbled upon a nice solution, to use the Collections.EMPTY_LIST with Generics. Just use the method emptyList with Generics instead of the constant value.

Returns the empty list (immutable). This list is serializable.

For example:

Collections.<Holiday> emptyList();

List<String> s = Collections.emptyList(); this example illustrates the type-safe way to obtain an empty list:

Tip to avoid NullPointerException while using String

Tip to avoid NullPointerException while using String

There are many occasions where the logic/code in Java class needs to compare between a constant string value and a variable.

For example: variable.equals(STRING_CONSTANT);

Here if the variable is not initialized, then the above piece of code will throw a null pointer exception.

To shun this null pointer exception you can re-state the above piece of code as:

CONSTANT_STRING.equals(variable);

Always Follow Your Heart

Always Follow Your Heart


Your time is limited.
So don’t waste it living someone else’s life.

Don’t let the noise of other’s opinions
Drown out your inner voice.

Follow you heart. Always..

Follow you heart. Always..

always-follow-your-heart

I Will Climb

I Will Climb

I will climb this mountain.
They have told me that it is too high, too far, too steep, too rocky and too difficult.
But it’s my mountain. I will climb it.
You will soon see me waving from the top or dead on the side from trying.

i-will-climb

Time Has Wings

time-has-wings