Saturday, 1 December 2012

Collection Important questions

Convert ArrayList to String 

Here is the simple exam how we can convert an array list to string 
import java.util.ArrayList;
import java.util.List;
 
public class Java4s {
 
public static void main(String args[]){
 
    List al = new ArrayList<String>();// creating list
 
    al.add("One");
    al.add("Two");
    al.add("Three");
    al.add("Four");
    al.add("Five");
 
    String[] stringArrayObject = new String[al.size()];// creating string array
    al.toArray(stringArrayObject);//converting list to string array
 
    for(String temp : stringArrayObject)
    System.out.println(temp);
 
}
}
 

Difference between Arraylist and Vector

  • ArrayList and Vector both having same data structure internally, which is Array
  • Vector is by default synchronized, means at a time only one thread can access its methods from out side, where as ArrayList is non-synchronized means N number of threads can access at a time
  • But we can make ArrayList as synchronized by using Collections class, see it bellow
  • Both Vector and ArrayList  have capability to re-size dynamically, means Vector will Doubles the size of its array when its size increased, but ArrayList increased by Half only

    How to make ArrayList Synchronized

    As we discussed above ArrayList is not synchronized by default, so will see how to make it as Synchronized.
    We have direct method in Collections class to make ArrayList synchronized..

    List li = new ArrayList()
    Collections.synchronizedList(li)

    ArrayList vs Vector Speed and Performance Differences

    Always ArrayList will shows better performance compared to Vector,  except Synchronization both are almost same in their performance.  Vector is Synchronized means thread safe, only 1 thread can access so its very slow compared to ArrayList, because in our real time projects we should not require synchronized methods always.
    What am saying is always try to use ArrayList rather Vector if its not required any Synchronization in your requirements,  even if so you know how to make ArrayList as synchronized right (just like above).
    Let us see how to sort values in java collections, List/ArrayList.  We can achieve this by using Collections class, Collections class contains a method sort(Collection Object), just give your collection object to the sort() method that’s it, consider this example….

    Example:
    mport java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
     
    public class SortCollection {
     
        public static void main(String... args)
        {
     
                List li = new ArrayList();
     
                li.add("India");
                li.add("United States");
                li.add("Malaysia");
                li.add("Australia");
                li.add("Lundon");
     
                Collections.sort(li);// this is the method of collections interface to sort
     
                for(String temp: li)
                {
                    System.out.println("Countries : "+temp);
                }
        }
    }

    Set (Interface)

  • Set is an un-ordered collection which doesn’t allows duplicate (no-duplicate) elements
  • We can iterate the values by calling iterator() method
Set s = new HashSet();
Iterator iter = s.iterator();

List (Interface)

  • List is an ordered collection which allows duplicate elements
  • We can iterate the values by calling iterator() method
List li = new ArrayList();
Iterator iter = li.iterator();

Map (Interface)

  • In Map we used to store the data in key and value pairs, we may have duplicate values but no duplicate keys
  • In Map we don’t have iterator() method, but we can get the keys by calling the method keySet()
    Map m; // insert values
    Set s = m.keySet();
    // Get Map keys into the Set and then iterate this Set object normally
    // m.keySet() returns Set object with Map keys
    Iterator iter = s.iterator();


 

 

 

No comments:

Post a Comment