Saturday, 10 June 2017

ArrayList in java


ArrayList is a class that extends AbstractList class and implements List interface in java. 

It uses dynamic array for storing the elements thus gaining upper hand in comparison to Array (which is static in nature and has fixed length).


Key feature of ArrayList:

1. It implements List interface, RandomAccess interface, Collection interface, Iterable interface, Cloneable interface and Serializable interface.
2. It is a part of java.util package.
3. ArrayList can expand and shrink automatically as per the need because of its dynamic array storage capability.
4. ArrayList is not synchronized (else it would have been roughly equivalent to Vector).
5. It permits all element, including null.
6. ArrayList is generally slow while manipulating the elements as during insertion or deletion lot of shifting needs to be done.


Now let’s look at the ways in which you can create ArrayList in java. 

·         ArrayList<E> myList = new ArrayList <E>();     //<E> denotes generics
·         ArrayList<E> myList = new ArrayList <E>(Collection c);//<E> denotes generics

Simple Example:


package javaRadarArrayList;

import java.util.ArrayList;

public class SimpleArrayList {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            System.out.println(javaRadarList);
      }


}




Now let’s look at different ways in which you can use the ArrayList that you created. 
Click on each link to read about them.

ArrayList operations

·         Sort the elements in ArrayList
·         Search element in ArrayList

Traverse ArrayList


Convert

·       ArrayList to LinkedList



ArrayList or LinkedList. Which one to use?(Click to see the difference between two and decide)


How to synchronize ArrayList?

So far we have learned that ArrayList is non-synchronized but still what to do if we need to synchronize it.
So java provides a solution to synchronize your ArrayList. Use synchronizedList() method provided by Collections utility class.

                Collections.synchronizedList(List list)


Saturday, 3 June 2017

Can we call run() directly on thread without calling start() ?

Yes, we can.

But if we do so then we must be aware that run( ) will not execute in separate call stack in this case. Rather it executes onto the current call stack.

Lets have a look at the below example:

call run() directly on thread by java radar
Separate call stack not allotted to thread1 and thread2 on directly calling run()

In the above example we can see that even though there is sleep() called but context switching doesn't happen. Because both thread object thread1 and thread2 run in current call stack(i.e main stack). So on directly calling run(), thread1 and thread2 are treated as normal object and not the thread object. That is why, first thread1 completes its execution(0,1,2) is printed and then run() of thread2 gets chance to execute.

Now call start() on both the thread and see the difference. You will notice both thread object running in separate call stack and also going through context-switching. Look at below example:

call start on thread by java radar
Calling start() on thread internally calls run()



Here we can also see that we don't need to call run() on thread instance as we did in our first example. start() does this task internally.


You may also want to know about:

Wednesday, 31 May 2017

What is marker Interface in java?

Marker Interface doesn't have any method or field or constant declared within it. So a marker interface is an empty interface.

Example :

  • Serializable
  • Cloneable, 
  • RandomAccess interface and 
  • Remote interface


Now one question that would immediately pop-up in your mind is,

Why would one declare an empty interface? 
What good is it of?

In general a common belief is that a marker interface is generally used to send some kind of signal to the JVM and instructs JVM to  treat the class implementing it in a special way.

Like, when a class implements Cloneable marker interface, it signals the compiler that the class allows creation of clones of its objects.

Generally, a marker interface is used with the instanceof operator to check if a reference type variable refers to an object whose class implements the marker interface.


You may also like to read about:



Traverse ArrayList using Enumeration

package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.ListIterator;

public class EnumerationTraversal {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            //creating enumeration to traverse over javaRadarList
            Enumeration<String> listEnumerator = Collections.enumeration(javaRadarList);

            System.out.println("List traversal using Enumeration");
            while(listEnumerator.hasMoreElements()){
                  System.out.println(listEnumerator.nextElement());
            }
      }

}

OUTPUT:
List traversal using Enumeration
Java
Jquery
Spring
Hibernate
EJB


Tuesday, 30 May 2017

Remove element from array list

package javaRadarArrayList;

import java.util.ArrayList;

public class RemoveAtIndex {

      public static void main(String[] args) {
            //create Array List
            ArrayList<String> javaRadarList = new ArrayList<String>();
            //Add elements to ArrayList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");
           
            System.out.println("Before Removal");
            for(String list : javaRadarList){
                  System.out.println(list);
            }
           
            //Remove element from 2nd position
            javaRadarList.remove(1);
           
            System.out.println("After Removal");
            for(String list : javaRadarList){
                  System.out.println(list);
            }

      }

}

OUTPUT:

 Before Removal
Java
Jquery
Spring
Hibernate
EJB
After Removal
Java
Spring
Hibernate
EJB

Compare the result and you will find that element at 2nd position(Jquery) will not appear in After Removal result.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.