Friday, January 17, 2014

Collections: When to use them (brief summary)

Collection is a data structure actually, an object that can hold references to other objects.

Collection Framework interfaces:

1. Lists - Lists of things (ex. Lists of students).
2. Sets - Set is a collection where all the values are unique.
3. Maps - Map is a collection that holds key/value pairs. A unique key is mapped to a specific value, where both the ‘key’ and ‘value’ in are objects.

Terminologies:
1. Ordered - You can iterate through the collection in a specific order.
2. Sorted - Collection is sorted in the natural order (alphabetical for Strings).

LISTS

Arraylist - uses arrays internally for data management.

to be used when:

  • you need a resizable-array implementation of List interface
  • you need an ordered collection
  • should be considered when there is more data retrieval that adding or deleting of elements.
Vector - also implemented on similar lines of ArrayList, but some of the methods in Vector are synchronized and Vector has more methods when compared to ArrayList.



to be used when:
  • you need an ordered collection
  • should be considered when thread safety is a concern.
LinkedList - it is a LinkedList implementation. The LinkedList class uses an inner class called Entry. 

to be used when:
  • you need an ordered collection
  • faster insertion/deletion and slower retrieval when compared to ArrayList.
MAPS

HashMap - stores elements in an array of type Entry.

to be used when:
  • you need an unsorted and unordered Map
  • you need a map that allows null key and null values
  • methods are not synchronized. thread safety is not a concern
Hashtable - is implemented in the same fashion as HashMap. 


to be used when:
  • you need an unsorted and unordered Map
  • should be considered when security is important
  • you need a map that allows null key and null values
  • methods are synchronized. thread safety is a concern
  • has more utility methods than HashMap
TreeMap 

The elements in a TreeMap are sorted by the keys. 
As with every Map, only objects can be stored as keys and values in a TreeMap.
A TreeMap allows null values.
All keys that go into the TreeMap should implement the Comparable interface.

SETS

All Set implementations use Map internally. It has the same usage with Map though take note that objects in Sets should be unique.

HashSet - uses HashMap internally.
LinkedhashSet - uses LinkedHashMap
TreeSet - uses TreeMap 

reference: COLLECTIONS – WHAT HAPPENS WITHIN -Compiled by Sharad Ballepu

Thursday, November 21, 2013

Setting up your Linux Environment (Ubuntu) for JAVA Development


This tutorial is for starters. although this may also help professionals. I am using Ubuntu 12.04 LTS and I thought of creating a tutorial on how to setup your Linux environment for java development.

Installing JDK 6


  • download jdk from here
  • create folder jvm in /usr/local
  • move the downloaded jdk to /usr/local/jvm
  • change the permission of jdk -> sudo chmod 700 jdk-6u38-linux-i586-rpm.bin
  • extract jdk using command -> ./jdk-6u38-linux-i586-rpm.bin
  • create a link for the jdk -> ln -s jdk-6u38 java
  • add the jdk in your enviromnent PATH. I usually add it in .bashrc
          you can add the following in your .bashrc:

          export JAVA_HOME=/usr/local/jvm/java
          export JDK_HOME=$JAVA_HOME
          export PATH=$PATH:$JAVA_HOME/bin

Installing JDK 7

Just execute the following commands to install jdk7
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

Installing Subversion

sudo apt-get install subversion

Installing Git

sudo apt-get install git-core

Installing Maven

  • Go to Apache Maven website and follow the instructions
  • place maven in /usr/local/apache-maven
  • if you have your own settings.xml, create .m2 folder in your home directory and place your settings.xml
  • add maven in your environment path
    

Tuesday, October 2, 2012

Programmer Tip: #1 Constants First

Based on my experience, programmers always commit this mistake in using constants in the code. I always see this problem even in large projects made by experienced programmers. So I decided to choose this topic for my first blog. Check this code.

public static final String CONSTANT_VALUE = "Value";
    
public static boolean functn(String compare) {
    if(compare.equals(CONSTANT_VALUE)) {
        return true;
    }
    return false;
}

Did you notice something wrong with this code? Well, Yes you're right. What if a null value is passed to the functn method? This code will produce a NullPointerException. So to avoid getting a NullPointerException some programmers will check if the passed value is null or add some try-catch to handle the exception like this

public static final String CONSTANT_VALUE = "Value";
    
public static boolean functn(String compare) {
    if(compare != null 
       && compare.equals(CONSTANT_VALUE)) {
        return true;
    }
    return false;
}

public static boolean functnWithTryCatch(String compare) {
    try {
        if(compare.equals(CONSTANT_VALUE)) {
            return true;
        }
        return false;
    } catch (NullPointerException e) {
        return false;
    }
}

Note: declare your constant variables as "static"

The right way of doing it is just to put the constant variable first and make the "compare" the parameter in your operation. This is because the constant variable always has a value and will never turn to null. Even if the passed parameter is null, this will not produce any NullPointerException and behave properly. See example below

public static final String CONSTANT_VALUE = "Value";
    
public static boolean functn(String compare) {
    if(CONSTANT_VALUE.equals(compare)) {
        return true;
    }
    return false;
}

Notice that the code is now: if(CONSTANT_VALUE.equals(compare))

Hope you learned something. Sorry if the design and the words used is not that good. Busy eh :p I'll improve this page pag di na ako busy. Thanks ;)