Tuesday, 3 June 2014

HashMap

HashMap is a Map based collection class that is used for storing Key & value pairs

I would recommend you to go through my post 'How HashMap works in Java
'.

HashMap permits null values and null key.

The HashMap performs the get and put operations in constant time.

HashMap has two parameters that effects the Performance
a. Capacity
b. Load Factor

Capacity
capacity is the number of buckets in the hash table.

Load Factor
The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. By default Load Factor for HashMap is 0.75.

For Example let us assume
initial capacity is 10 (I.e, Number Of Buckets)
Load Factor is 0.75
Each Bucket has the capacity to store 100 data items.

So when all the items reached 0.75 * 100 * 10 = 750, then the internal data structures rehashed and the Capacity(Number Of Buckets) increases depends on implementation. The same logic applies in future also.

HashMap is not synchronized, So user has to taken care while performing put kind of operations.

Collections.synchronizedMap returns a synchronized map backed by the specified map.

Note
HashMap implements Serializable and Cloneable interface.

public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable


Prevoius                                                 Next                                                 Home

Monday, 2 June 2014

ArrayList Vs LinkedList

  1. ArrayList is faster than LinkedList to access elements randomly. ArrayList has direct references to every element in the list, so it can get the n-th element in constant time. LinkedList has to traverse the list from the beginning to get to the n-th element.
  1. LinkedList is faster than ArrayList for deletion.ArrayList is slower because it needs to copy part of the array in order to remove the slot that has become free. LinkedList just has to manipulate a couple of references.
  1. Access time for ArrayList: O(1). Access time for LinkedList: O(n).
  1. Deletion time for ArrayList: Access time + O(n). Deletion time for LinkedList: Access time + O(1).

Prevoius                                                 Next                                                 Home

ArrayList Vs Vector

  1. Vector is synchronized by default, where as ArrayList is not.

  2. ArrayList is way faster than Vector. Since Vector is synchronized and thread-safe it pays price of synchronization which makes it little slow.
  1. Vector is a legacy class and initially it was not part of Java Collection Framework.
Prevoius                                                 Next                                                 Home

Sunday, 1 June 2014

ArrayList : containsAll : Search for a Collection of elements

public boolean containsAll(Collection<?> c)
Returns true if this list contains all of the elements of the specified collection.

import java.util.*;
class ArrayListContainsAll{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  ArrayList<Integer> myList1 = new ArrayList<> ();
  ArrayList<Integer> myList2 = new ArrayList<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to myList1 */
  for(int i=5; i < 10; i++){
   myList1.add(i);
  }
  
  /* Add Elements to myList2 */
  for(int i=5; i < 12; i++){
   myList2.add(i);
  }
 
  
  System.out.println("Elements in the myList are");
  System.out.println(myList+"\n");
  
  System.out.println("Elements in the myList1 are");
  System.out.println(myList1+"\n");
  
  System.out.println("Elements in the myList2 are");
  System.out.println(myList2+"\n");
  
  System.out.print("Is myList contains all the elements of myList1 ");
  System.out.println(myList.containsAll(myList1));
  
  System.out.print("Is myList contains all the elements of myList2 ");
  System.out.println(myList.containsAll(myList2));
 }
}

Output
Elements in the myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in the myList1 are
[5, 6, 7, 8, 9]

Elements in the myList2 are
[5, 6, 7, 8, 9, 10, 11]

Is myList contains all the elements of myList1 true
Is myList contains all the elements of myList2 false


Prevoius                                                 Next                                                 Home

ArrayList : trimToSize : Trim Capacity to SizeArrayList : trimToSize : Trim Capacity to Size

public void trimToSize()
Trims the capacity of this ArrayList instance to be the list's current size.

Prevoius                                                 Next                                                 Home

ArrayList : toArray (T[] a) : Copy List Data to Array of Specific Type

<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence the run time type of the returned array is that of the specified array.

import java.util.*;
class ArrayListSpecificArray{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  Integer myArray[];
  Integer retArray[];
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myArray = new Integer[size];
  retArray = new Integer[size];
  
  retArray = myList.toArray(myArray);
  
  System.out.println("Elements in the list are");
  System.out.println(myList);
  
  System.out.println("Elements in myArray are");
  for(int i: myArray)
   System.out.print(i +" ");
   
  System.out.println("\n Elements in the retArray are");
  for(int i: retArray)
   System.out.print(i +" ");
   
 }
}

Output
Elements in the list are
[10, 123, 321, 456]
Elements in myArray are
10 123 321 456
 Elements in the retArray are
10 123 321 456

1. If the Array size is more than the number of elements in the list, then the element in the array immediately following the end of the list is set to null.
import java.util.*;
class ArrayListSpecificArray{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  Integer myArray[];
  Integer retArray[];
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myArray = new Integer[2*size];
  retArray = new Integer[2*size];
  
  retArray = myList.toArray(myArray);
  
  System.out.println("Elements in the list are");
  System.out.println(myList);
  
  System.out.println("Elements in myArray are");
  for(Integer i: myArray)
   System.out.print(i +" ");
   
  System.out.println("\n Elements in the retArray are");
  for(Integer i: retArray)
   System.out.print(i +" ");
   
 }
}

Output
Elements in the list are
[10, 123, 321, 456]
Elements in myArray are
10 123 321 456 null null null null
 Elements in the retArray are
10 123 321 456 null null null null

2. toArray() throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this list

import java.util.*;
class ArrayListSpecificArrayStore{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  Float myArray[];
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myArray = new Float[size];
  
  myList.toArray(myArray);
  
  System.out.println("Elements in myArray are");
  for(float i: myArray)
   System.out.print(i +" ");
 }
}

When you tries to run the above program, run time throws “ArrayStoreException”. Since the list is of type Integer, where as the destination is of type Float. Both are incompatible.

Output
Exception in thread "main" java.lang.ArrayStoreException
        at java.lang.System.arraycopy(Native Method)
        at java.util.ArrayList.toArray(ArrayList.java:393)
        at ArrayListSpecificArrayStore.main(ArrayListSpecificArrayStore.java:18)

3. toArray() throws NullPointerException if the specified array is null
import java.util.*;
class ArrayListSpecificArrayNull{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  Integer myArray[];
  myArray = null;
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myList.toArray(myArray);
  
  System.out.println("Elements in myArray are");
  for(Integer i: myArray)
   System.out.print(i +" ");
 }
}

Since “myArray” is initialized to null, so at run time NullPointerException thrown.

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.ArrayList.toArray(ArrayList.java:390)
        at ArrayListSpecificArrayNull.main(ArrayListSpecificArrayNull.java:17)


Prevoius                                                 Next                                                 Home

ArrayList : toArray() : get the List Data as object Array

public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence. The returned array is totally independent, Updating the Array won't affect List.

import java.util.*;
class ArrayListToObjectArray{
 public static void main(String args[]){
  ArrayList<Float> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10.01f);
  myList.add(123.01f);
  myList.add(321.01f);
  myList.add(456.78f);
  
  Object myArr = myList.toArray();
  
  System.out.println("Elements in the Array are");
  for(Float f1 : myList)
   System.out.println(f1);
 }
}

Output
Elements in the Array are
10.01
123.01
321.01
456.78


Prevoius                                                 Next                                                 Home