Load Factor

Ques) What is LoadFactor?

Rep) LoadFactor is useful to determine the point where the capacity of the Hashtable (HashMap) will be doubled For Hashtable initial capacity * loadfactory–>11 * 0.75=8

This class implements a hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.To successfully store and retrieve objects from a Hashtable , the objects used as keys must implement the HashCode method and the equals method.

The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the Hashtable exceeds the product of the load factor and the current capacity, the capacity is increased by calling the rehash method.
Therefore after starting 8th pair key values, the capacity of the Hashtable will be “Doubled” –>11*2=22.

Hashtable ht=new Hashtable(100);

|__ It is the inital capacity of the hashtable

Collection FrameWork

Q) What is Collection FrameWork?

Res) A Collection FrameWork represents a class library to handle groups of objects

–> Collection framework is implemented in Java.Util.Package.

–> Collection does’t store copy of all the object, but it stores references of all objects.

–> Collection classes will act upon only referenced datatypes they do not act upon primitive datatypes.

ArrayList Class

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you may not know util run time precisely how large of an array you need. To handle this situation, the collections framework defines ArrayList.

In essence, an ArrayList is a variable-length array of object references. Thjat is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the arraya may be shrunk.

It is dynamically growing array that stores objects. It is not Synchronized and Vector is a Synchronized

Note: You can’t store primitive data type. we can store only object into ArrayList.

ArrayList has the constructors shown here:

–> ArrayList();  //This constructor builds an empty array list

–> ArrayList(Collection c)  // This constructor builds an array list that is initialized with the elements of collection c.

–> ArrayList(int capacity//This Contructor builds an array list that has the specified initial capacity.

1) To Creat  an ArrayList

ArrayList arl=new ArrayList();  //creating an object

ArrayList Arl=new ArrayList(20);  //creating an object with the size

2) To add objects use add() method

arl.add(“APPLE”);

arl.add(2,”APPLE”);

3) To Remove objects use remove().

arl.remove(“APPLE”);  //remove an string

arl.remove(2);  //removing the string  of specified location

4) To Know Number of objects, use size()

int n= arl.size()   // It displays the size of the array list.

Example of using an ArrayList.

//Demonstrating ArrayList.

import java.util.*;

class Arraylist Demo  {
public static void main(String args[])  {

ArrayList list=new ArrayList();  //creating an object for ArrayList class
System.out.println(“Initial size of al:”+al.size());

//add  elements to the array list
al.add(“C”);
al.add(“B”);
al.add(“A”);
al.add(“D”);
al.add(“E”);
al.add(“T”);
al.add(1,”A2″);

System.out.println(“Size of al after additions: “+ al.size());

//display the array list
System.out.println(“Content of al:”+al);

//Remove elements from the array list
al.remove(“F”);
al.remove(2);

System.out.println(“size of al after deletion:” +al.size());
System.out.println(“contents of al:”+al);
}
}

Write a Short notes on Vectors

VECTORS

C and C++ programmers will know that generic utility functions with variable arguments can be used to pass different arguments depending upon the calling situations. Java does not support the concept of variable arguments to a function. This feature can be achieved in Java through the use of the Vector class contained in the java.util package. This class can be used to create a generic dynamic array known as Vector that can hold objects of any type and any number. The objects do not have to have to be homogenous. Array can be easily implemented as vector. Vectors are created like arrays as follows:

Vector intVect  = new Vector();   //declaring without size

Vector list        = new Vector(3);  // declaring with size.

Note that a vector can be declared without specifying any size explicitly. A vector can accommodate an unknown number of items. Even, when a size is specified, this can be overlooked and different number of items may be put into the vector. Remember, in contrast, an array must always have its size specified.

Vector possess a number of advantages over arrays.

1.  It is convenient to use vectors to store objects.

2. A vector can be used to store alist of objects that may vary in size.

3.  We can add and delete objects from the list as and when required.

A major constraint in using vectors is that we cannot directly store simple data types in a vector; we can only store objects. Therefore, we need to convert simple tyhpes to objects. This can be done using the wrapper classes.

The vector class supports a number of methods that can be used to manipulate the vectors created. Important ones are listed

Method Call and task performed

list.addElement (item) Adds the item specified to the list at the end.

list.elementAt(10) Gives the name of the 10th object.

list.size() Gives the number of objects present.

list.removeElement(item) Removes the specified item from the list.

list.removeElementAt(n) Removes the item stored in the nth position of the list.

list.removeAllElements() Removes all the item stored in the nth position of the list.

list.copyInto(array) Copies all items from list to array.

list.insertElementAt(item,n) Inserts the item at nth position.

Example of working with vectors:

import java.util.*;

class LanguageVector
{
public static void main(String args[])
{
Vector list=new Vector();  //creating an object for vector class
int length=args.length;

for(int i=0;i<length;i++)
{
list.addElement(args[i]);;
list.insertElementAt(“COBOL”,2);
int size=list.size();
String listArray[]=new String[size];
list.copyInto(listArray);
System.out.println(“List of Languages”);

for(int i=0;i<size;i++)
{
System.out.println(listArray[i]);
}
}
}