Important classes implementing List interface in java
ArrayList
- The underlying data structure is resizable array or growable array.
- Duplicates are allowed.
- Insertion order is preserved.
- Heterogenous objects are allowed.Except
TreeSet
andTreeMap
heterogeneous objects are allowed everywhere insideCollection
. - Null insertion is possible.
Constructors in ArrayList
ArrayList l = new ArrayList();
- The above initialization creates an empty
ArrayList
object with default initial capacity 10. - Once
ArrayList
reaches its max capacity; then newArrayList
object will be created with new capacity = (currentCapacity * (3/2)) + 1. - The reference variable is reassigned to new object and the old
ArrayList
object is available for garbage collection.
ArrayList l = new ArrayList(int initialCapacity);
- The above initialization creates an empty
ArrayList
object with specified initial capacity.
ArrayList l = new ArrayList(Collection c);
- The above initialization creates an equivalent
ArrayList
object for the givenCollection
.
Demo Implementation:
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args){
ArrayList l = new ArrayList(); // Warning comes if we're not using generics. To remove use
// ArrayList<String> l = new ArrayList<String>();
l.add("A");
l.add(10); // auto-boxing(primitive to object conversion) performed internally.
l.add("A");
l.add(null);
System.out.println(l); // l is reference to ArrayList object.
// If we're trying to print any object reference internally toString() method will be invoked.
l.remove(2);
System.out.println(l); // [A, 10, null]
l.add(2, "M");
l.add("N");
System.out.println(l); // [A, 10, M, null, N]
}
}
Note 1: ArrayList
and Vector
classes implements RandomAccess
interface, so that any random element we can access with the same speed.
Note 2: RandomAccess
interface is present in java.util
package and it doesn’t contain any methods. It is a marker interface., where required ability will be provided automatically by the JVM.
Demo Implementation
ArrayList l = new ArrayList();
LinkedList l2 = new LinkedList();
System.out.println(l instanceof Serializable); // true
System.out.println(l2 instanceof Serializable); // true
System.out.println(l instanceof Cloneable); // true
System.out.println(l2 instanceof Cloneable); // true
System.out.println(l instanceof RandomAccess); // true
System.out.println(l2 instanceof RandomAccess); // false
Pros and Cons of using ArrayList
ArrayList
is the best choice if frequent operation is retrieval operation becauseArrayList
implementsRandomAccess
interface.ArrayList
is the worst choice if frequent operation is insertion or deletion in the middle.
Differences between ArrayList and Vector
ArrayList | Vector |
---|---|
1. Every method present in the ArrayList is non-synchronized. |
1. Every method present in the Vector is synchronized. |
2. At a time, multiple threads are allowed to operate on ArrayList object and hence it is not thread-safe. |
2. At a time, only one thread is allowed to operate on Vector object and hence it is thread-safe. |
3. Relatively, performance is high because threads are not required to wait to operate on ArrayList object. |
3. Relatively, performance is low because threads are required to wait to operate on Vector object. |
4. Introduced in 1.2v and it is non-legacy. | 4. Introduced in 1.0v and it is legacy class. |
How to get Synchronized version of ArrayList object
ArrayList
is not synchronized by default. But we can get synchronized version of ArrayList
object by using synchronizedList()
method of Collections
class.
ArrayList l = new ArrayList();
List l1 = Collections.synchronizedList(l); // l is not-synchronized while l1 is synchronized.
Note: We can get synchronized version of Set
and Map
object by using the following methods of Collections
class i.e. synchronizedSet()
and synchronizedMap()
methods.
LinkedList
- The underlying data-structure is doubly-linked list.
- Insertion order is preserved.
- Duplicate objects are allowed.
- Heterogeneous objects are allowed.
- Null insertion is possible.
LinkedList
implementsSerializable
andCloneable
but notRandomAccess
interface.LinkedList
is the best choice if frequent operation is insertion or deletion in the middle.LinkedList
is the worst choice if frequent operation is retrieval operation.
Constructors in LinkedList
LinkedList l = new LinkedList();
- The above initialization creates an empty
LinkedList
object.
LinkedList l = new LinkedList(Collection c);
- The above initialization creates an equivalent
LinkedList
object for the givenCollection
.
Specific methods of LinkedList class
- Usually, we can use
LinkedList
to develop stacks and queues. To provide support for this requirementLinkedList
class defines specific methods.
void addFirst(Object o);
void addLast(Object o);
Object getFirst();
Object getLast();
Object removeFirst();
Object removeLast();
Demo Implementation
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.add("Mandy8055");
l.add(30);
l.add(null);
l.add("Mandy8055"); // [Mandy8055, 30, null, Mandy8055]
l.set(0, "Software"); // [Software, 30, null, Mandy8055]
l.add(0, "Sappy"); // [Sappy, Software, 30, null, Mandy8055]
l.removeLast(); // [Sappy, Software, 30, null]
l.addFirst("CCC"); // [CCC,Sappy, Software, 30, null]
System.out.println(l);
}
}
Vector
- The underlying data structure is resizable/growable array.
- Insertion order is preserved.
- Duplicates are allowed.
- Heterogeneous objects are allowed.
- Null insertion is possible.
- It implements
Serializable
,Cloneable
andRandomAccess interfaces
. - Every method present in the
Vector
is synchronized and henceVector
object is thread-safe.
Constructors
Vector v = new Vector();
- The above initialization creates an empty
Vector
object with default initial capacity 10. - Once vector reaches its, max-capacity, then a new Vector object will be created with new_capacity = current_capacity * 2.
Vector v = new Vector(int initialCapacity);
- The above initialization creates an empty
Vector
object with specified initial capacity.
Vector v = new Vector(int initialCapacity, int incrementalCapacity);
Vector v = new Vector(Collection c);
- The above initialization creates an equivalent
Vector
object for the givenCollection
c. This constructor is meant for inter-conversion betweenCollection
objects.
Vector-specific methods
void addElement(Object o);
boolean removeElement(Object o);
void removeElementAt(int index);
void removeAllElements();
int size();
int capacity();
Enumeration elements();
Demo Implementation
import java.util.*;
public class VectorDemo1 {
public static void main(String[] args){
Vector v = new Vector(); // Warning comes because of lack of type safety. Use generics
// Vector v = new Vector(10, 5);
// Vector v = new Vector(24);
System.out.println(v.capacity());
for(int i = 1; i <= 10; i++){
v.addElement(i); // auto-boxing will be performed automatically.
}
System.out.println(v.capacity());
v.add("A");
System.out.println(v.capacity());
System.out.println(v);
}
}
Stack
- It is the child class of
Vector
. - It is a specially designed class for Last In First Out(LIFO) order.
Stack-specific methods
Object push(Object o);
Object pop();
Object peek();
boolean empty();
int search(Object o); // returns the offset of o if o is present inside the stack else returns -1.
Implementation Demo
import java.util.Stack;
public class StackDemo {
public static void main(String[] args){
Stack s = new Stack();
s.push("A");
s.push("B");
s.push("C");
System.out.println(s);
System.out.println(s.search("A"));
System.out.println(s.search("Z"));
}
}