when and where to use different java collection framework

Knowing when and where to use different java collection framework is like having a month baby and not sure of what to feed him with .
When the baby is crying, what will you feed him with ?

Anything or breast milk ?

The baby will no doubt take either of the two food options, remember the baby is subjected to good / bad health , fast/stunted growth,… depending on the type of food intake and the enviroment.

When you feed the baby wrongly, he will no doubt take the food and react to it, just like when you applied wrong collection inerface or classes , the system takes it and react to it ( though it might work , but inefficiently ).
There are lot of questions to ask oneself when choosing the collection framework

1. Their performance in adding, removing, modifying and manipulation

2. Does it allow concurency

3. Does it accepts null as default value, allow unduplicate…

Step 1 : Use interface and not class for variables declaration
List<User> listUser = new ArrayList<>(); not ArrayList<User> userList = new ArrayList<>();
When your code depends on interface , you can easily use different implementation without changing the code that implement it, with ease.

class MyTEST
{

   public List<MyTEST> testList (List<MyTEST> data )
   {
      return data;
   }

   public static void main (String[] args)
   {
      MyTEST a = new MyTEST();
      ArrayList<MyTEST> myTESTS = new ArrayList<>();
      LinkedList<MyTEST> myLinks = new LinkedList<>();

      a.testList(myTESTS);
      a.testList(myLinks);
   }
}

In order to ensure code flexibility , user interface for your code dependency types and parameter
Hope this make your day better ?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: