Category Archives: CollectionFrameWork
ArrayList in java example using iterator
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListIterator {
public static void main(String args[]) {
ArrayList al = new ArrayList();
al.add(“Santosh”);
al.add(“Nag”);
al.add(“Rajesh”);
al.add(“Santosh”);
Iterator it = al.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Explain about Java.util.ArrayList class?
java.util
Class ArrayList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
Version:
1.2
======================================================
Constructors of java.util.ArrayList class:
1.ArrayList l = new ArrayList();
Creates an empty ArrayList Object with default initial capacity is 10.
Once ArrayList reaches its Max. Capacity then new ArrayList Object will be created with
New Capacity = currentCapacity*(3/2)+1
2. ArrayList l = new ArrayList( int initialcapacity);
Creates an empty ArrayList Object with the specified initial capacity.
3.ArrayList l = new ArrayList( Collection c);
Creates an equivalent ArrayList Object for the given Collection Object.
This constructor meant for interconversion between the Collection Object.
======================================================
Methods of java.util.ArrayList class :
1.public int indexOf(Object o)
2.public boolean addAll(Collection c)
Throws:NullPointerException
3.public void trimToSize()
4. public void ensureCapacity(int minCapacity)
5.public int size()
6.public boolean isEmpty()
7.public boolean contains(Object o)
8.public int lastIndexOf(Object o)
9.public Object clone()
10. public boolean add(E e)
11.public void add(int index, E element)
Throws:IndexOutOfBoundsException
12. public Object[] toArray()
13. public boolean remove(Object o)
14.public void clear()
15.public boolean addAll(int index, Collection c)
Throws:IndexOutOfBoundsExceptionNullPointerException
16. public E remove(int index)
Throws:IndexOutOfBoundsException
17. public E get(int index)
Throws:IndexOutOfBoundsException
18. protected void removeRange(int fromIndex, int toIndex)
Throws:IndexOutOfBoundsException
19. public E set(int index, E element)
Throws:IndexOutOfBoundsException
20. public T[] toArray(T[] a)
Throws:ArrayStoreExceptionNullPointerException
==================================
Description of java.util.ArrayList class :
1. ArrayList Object is dynamically resizable.
2. ArrayList Object maintains the duplicate elements.
3. ArrayList Object maintains in the list Insertion order.
4. ArrayList Object also accepts the heterogeneous elements.
5. ArrayList Object also maintains the elements based on index.
6. ArrayList Object accepts null pointer constant any number of times.
Advantages:
1. ArrayList Objects are Serializable Objects why because ArrayList implements Serializable interface.
2. ArrayList Objects are Cloneable Objects why because ArrayList implements Cloneable interface.
3. ArrayList also implements RandomAccess interface for frequent retrieval operations.
Disadvantages:
1. ArrayList is not best suitable for frequent insertion and deletion operations as it requires internally lot of shifting operations.
Example:
//ArraylistDemo.java
import java.util.*;
public class ArraylistDemo{
public static void main(String[] args){
ArrayList l = new ArrayList();
l.add(“A”);
l.add(10);
l.add(“A”);
l.add(null);
System.out.println(l);
l.remove(2);
System.out.println(l);
l.add(2,“M”);
l.add(“N”);
System.out.println(l);
}//main
}//class
javac ArrayListDemo.java
java ArraylistDemo
OUTPUT:
[A,10,A,null]
[A,10,null]
[A,10,M,null]
[A,10,M,null,N]
NOTE: Here toString() method provides like [].
ArrayList and Vector Classes implements RandomAccess interface so that we can access any random element with same speed.
What are the legacy classes in Collection Framework?
-
Enumeration
-
Dictionary
-
Hashtable
-
Vector
-
Stack
Q) Difference between Hashtable and HashMap ?
Hashtable :
-
All methods are synchronized.
-
Hashtable is thread safe.
-
Performance is poor.
-
Null is not allowed for both key and value, otherwise NullPointerException.
-
Introduced in 1.0 version and it is legacy.
HashMap :
-
No method is synchronized.
-
HashMap object is not thread safe.
-
Performance is good.
-
Null is allowed as both key and value.
-
Introduced in 1.2 and not legacy.
Explain about LinkedList? Or Constuctors , methods of LinkedList ? Or Explain bout java.util.LinkedList with an example?
java.util
Class LinkedList<E>
java.util.AbstractCollection<E>
java.util.AbstractSequentialList<E>
java.util.LinkedList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
Since:
1.2
Method OF LinkedList :
1. boolean add(E e)
Throws:
UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
IllegalStateException
2. boolean addAll(Collection c)
Throws:
UnsupportedOperationException
ClassCastException
NullPointerException
IllegalArgumentException
IllegalStateException
3. boolean remove(Object o)
Throws:
ClassCastException
NullPointerException
UnsupportedOperationException
4. boolean removeAll(Collection c)
Throws:
UnsupportedOperationException
ClassCastException
NullPointerException
5. void clear()
Throws:
UnsupportedOperationException
6. boolean contains(Object o)
Throws:
ClassCastException
NullPointerException
7. boolean containsAll(Collection c)
Throws:
ClassCastException
NullPointerException
8. boolean retainAll(Collection c)
Throws:
UnsupportedOperationException
ClassCastException
NullPointerException
9. int size()
10. boolean isEmpty()
11. Iterator<E> iterator()
12. Object[] toArray()
13. <T> T[] toArray(T[] a)
Throws:
ArrayStoreException
NullPointerException
14. boolean equals(Object o)
15. public int hashCode()
Constructors of LinkedList :
-
LinkedList l = new LinkedList();
-
LinkedList l = new LinkedList(Collectio c);
About LinkedList :
-
The underlying data structure is resizable Array or Growable Array.
-
LinkedList object also maintains elements insertion order.
-
LinkedList object also allows duplicate elements.
-
Null insertion is possible.
-
Heterogeneous objects are allowed.
-
Implements Serializable and Cloneable interface but not RandomAcess.