What is a collection framework?

What is a collection framework?

  • A collection framework is a class library to handle the group of objects.
  • Collection framework is implemented in java.util package.
Interface type
Implementation classes
Set
HashSet
List
Stack
LinkedList
ArrayLIst
Vector
Que
LInkedList
Map
HashMap
HashTable


Why Should we use collection frame even though we have an array to store group of objects?
 It is possible to store a group of  onjects into an array
Let us take an example where we want to store 100 object of employee class into an array.


Employee arr[]=new Employee[100];
For(int i=0; i<100;i++)
{
          Arr[i]=new Employee(data);
}

So, here we can observe that how can we store group of object into an array and retrieve them easily. But we have some inconveniences by using this mechanism, they are as follows
We cannot store different objects into the same array, because we knows that an array can store one data type of elements.
 Adding the objects at the end of an array is easy, but inserting and deleting the elements in the middle of an array is very difficult/complicated.
 Retrieving elements from an array is easy  but after retrieving the elements, if we want to process them, then there is no methods available to carry out this.
Due to have of above problems, programmers  want a better mechanism to store a group of objects. That mechanism is collection framework to handle group of objects of different type.

What is a set?

  • A set represents a group of elements arranged just like an array.
  • The set will grow dynamically when the elements are stored into it.
  • A set will not allow duplicate elements.
  • If we try to pass same element that is already available in the set, then it is not stored into it.
  • Set will not maintain the same order of elements as in which they were entered.

What is lists in collection framework?

  • Lists are like Sets.
  • They store a group of elements,
  • But Lists allow duplicate values to be stored.
What is the difference between ArrayList and Vector?
ArrayList
Vector
It is not synchronized
Vector is synchronized
In case of single thread, ArrayList is faster than Vector
In case of multiple threads, Vector is advisable, with single thread model Vector becomes slow
ArrayList increases it’s size by half every time
Vector increase it’s size double

What is a queue in collections framework?

  • A queue represents arrangements of elements in FIFO(Firs In First Out) order.
  • This means that element that is stored as a first element into the queue will be removed first from the queue.

What is map in collection framework?

  • Maps stored elements in the form of key and value pairs.
  • If key is provided then its corresponding value can be obtained.
  • Of course key should have unique values.
What is the difference between HashMap and HashTable?
Hash Map
Hash Table
Hash Map is not synchronized
Hash Table is synchronized
In case of single thread, Hash Map is faster than Hash Table
In case of multiple threads Hash Table is advisable, with single thread model it becomes slow
Hash Map allows null keys and null values
Hash table does not allow null keys or null values
Iterator in Hash Map is fail-fast means it will produce exception if concurrent updates made to Hash Map
Enumeration for Hash table is not fail-fast means even if concurrent updates are done to Hash Table, Enum will not produce any incorrect results

What is the difference between Iterator and ListIterator?

  • Both are useful to retrieve elements from a collection.
  • Iterator can retrieve the elements only in forward direction.
  • But ListIterator can retrieve the elements in forward and backward direction also.
  • So ListIterator is preferred than Iterator
·         What is the difference between Iterator and Enumeration?
Iterator
Enumeration
It is useful to retrieve object
From a collection
It’s also useful to retrieve object
From a collection
Iterator has methods whose
Names are easy to follow
Enumeration has methods whose
Names are difficult to remember
Iterator has an option to remove
Objects from a collection
Which is not available
So Iterator is preferred than Enumeration

How can we assign multiple values to a single key in HashMap?
HashMap can be used to store values in key-value pair format, but some times you might be want store multiple values for the same key like
for key A, you want to store Apple and America
for key B, you want to store Bat and Bangladesh
for key C, you want to store Cat and China
the following code snippet will show how to store multiple values for the same key.
HashMap-single key multiple values using LIST
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SingleKeyMultipleValuesUsingList
{
 public static void main(String[] args)
 {
  // create map to store
  Map<String, List<String>> map = new HashMap<String, List<String>>();
 
               // create list one and store values
  List<String> l1 = new ArrayList<String>();
  l1.add("Apple");
  l1.add("America");
 
               // create list two and store values
  List<String> l2 = new ArrayList<String>();
  l2.add("Bat");
  l2.add("Bangladesh");
 
               // create list three and store values
  List<String> l3 = new ArrayList<String>();
  l3.add("Cat");
  l3.add("China");
 
               // put values into map
  map.put("A", l1);
  map.put("B", l2);
  map.put("C", l3);
 
               // iterate and display values
  System.out.println("Fetching Keys and corresponding MULTIPLE Values n");
  for (Map.Entry<String, List<String>> entry : map.entrySet())
  {
   String key = entry.getKey();
   List<String> values = entry.getValue();
   System.out.println("Key = " + key);
   System.out.println("Values = " + values + "/n");
  }
 }
}
HashMap-single key multiple values using Collections
import java.util.Set;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMa;

public class SingleKeyMultipleValuesUsingApacheCollections
{
 public static void main(String[] args)
 {
  // create multimap to store key and values
  MultiMap multiMap = new MultiValueMap();
 
                // put values into map for A
  multiMap.put("A", "Apple");
  multiMap.put("A", "America");
 
                // put values into map for B
  multiMap.put("B", "Bat");
  multiMap.put("B", "Bangladesh");
 
                // put values into map for C
  multiMap.put("C", "Cat");
  multiMap.put("C", "China");
 
                // retrieve and display values
  System.out.println("Fetching Keys and corresponding MULTIPLE Values n");
 
                // get all the set of keys
  Set<String> keys = multiMap.keySet();
 
                // iterate through the key set and display key and values
  for (String key : keys)
  {
   System.out.println("Key = " + key);
   System.out.println("Values = " + multiMap.get(key) + "/n");
  }
 }
}

Which is the best one from the following i.e List l =new ArrayList() and ArrayList al=new ArrayList()
The First one List l = new ArrayList() 
allows you to use the List1 to refer to any object that is a sub class of list 


The second one ArrayList al = new ArrayList() 
will allow you to refer to only object that is an ArrayList Object

Logically the second way should be faster. But, since the binding happens 

at runtime, it shouldnt matter really. Its actually more to do with 
polymorphism. When you know you are going to be referencing object that may 
be sub classes and want to reuse the object ref.


It's simple, List is a Interface and "Arraylist, LinkedList, Vector" are your implementation. Each one with its own benefit, when you use the List any moment you can change your implementation, your application becomes more flexible. 


Example: 
List list = new ArrayLista(); 
for(String s : list){ 
... 


list = new LinkedList(); 
for(String s : list){ 
... 
}
 

No comments:

Post a Comment