Whenever we want a group individual of Object which can have duplicates and insertion order is preserved then we use List
Following are the characteristics of List
- Child interface of Collection interface
- Duplicates are allowed
- Insertion Order is preserved
Method in List Interface
List interface is mostly used by index and index play most important role in the List interface
As it is the child of Collection interface it has all the method of Collection interface and list specific method the are:
- public void add(int i ,Object o);
- Add Object o at index i
- public boolean addAll(inti,Collection c);
- Add group of object at index i
- public Object get(int i)
- return the object at index i
- public Object remove(int i)
- Element at specified index is removed
- public int indexOf(Object o);
- Return first index of this Object
- public int lastIndexOf(Object o);
- Return last index of the Object o
- public ListIterator listIterator()
- To access element of list one by one
- public Object set(int i,Object o);
- To update Object at specified index i
As we can see every method is related to index so plays an important role.
Implementation classes of List
- ArrayList(1.2 v)
- Linked List (1.2v)
- Vector(1.0v)
- Stack(1.0v)
Note Vector and Stack come in 1.0 v and also called as legacy classes
ArrayList
- Underlying data structure is growable array
- Duplicates are not allowed
- Insertion order is preserved
- Heterogeneous object are allowed
- Null insertion is allowed
- Implements Serializable and Cloneable interface
- Implements RandomAccess interface
- It is a Marker interface means do not have any methods
- It is used to access element randomly within same time
Constructor
- ArrayList al = new ArrayList();
- Create emptyt ArrayList and default initial capacity 10
- If the ArrayList reaches its max capacity then new ArrayList capacity is
- (current capacity *3/2)+1
- ArrayList al = new ArrayList(int initalCapacity);
- Create ArrayList with given initial capacity
- This is useful when you know that total elements are to be inserted
- ArrayList al = new ArrayList(Collection c)
- This is used to make ArrayList from any constructor
Example:
import java.util.*;
class ArrayListDemo{
public static void main(String...s){
ArrayList al = new ArrayList();
al.add("A");
al.add("C");
al.add("Z");
al.add(null);
System.out.println(al);//[A,C,Z,null]
al.remove(2);
System.out.println(al);//[A,Z,null]
al.add(2,"M");
al.add("N");
System.out.println(al);//[A,Z,M,null,N]
}
}
Question: Why theoutput is in square brackets as we know whenever we print aur reference than toString() method of Object class is called and every Collection class has overriden this method
public toString(Object o){
return "["+o+"]";// inside the square brackets
}
When is the Best and worst choice of ArrayList
Best choice :
Whenever our frequent operation is retrival operation then ArrayList is Best choice beacause it implements RandomAccess interface.
Suppose there are 10000 elements in the ArrayList and when you perform
get(1000) it take 1 sec
get(10) it take 1 sec
Worst choice :
Whenever our frequent operation is updation and deletion in the middle then its is a Worst choice
Suppose we have 1 crore elements in ArrayList and we want to add element in first index then to place the element there is a need to shift places and every element is shifting places so there are several shift operation to add and same for remove operation
Difference Between ArrayList And Vector class
Difference Between ArrayList And Vector class
Differences
|
ArrayList
|
Vector
|
1
|
Every method is not synchronized
|
Every method is synchronized
|
2
|
Performance is high
|
Performance is slow
|
3
|
Not thread safe Object
|
Thread safe object
|
4
|
Not legacy class
|
Legacy class
|
5
|
It come in 1.2 version
|
It came in 1.0 version
|
How to get synchronized version of ArrayList
Collections is the utility class that provide method to make our collection objects synchronized or thread safe :
ArrayList al = new ArrayList();
Collections.synchronizedList(al);
Complete signature is
public static List synchronizedList(List l);
Similarly we can have method to make Set and Map synchronized and they are:
public static Set synchronizedSet(Set set);
public static Map synchronizedMap(Map map);
Linked List class
Some features of Linked list
as the element are not stored in memory location and stored in form of nodes which contains address of the previous element and next element and thus easy to remove element by just removing the node and by adding the node element can be easily added
Some features of Linked list
- Underlying data structure
- Duplicates are allowed
- Insertion order is preserved
- Heterogeneous object are allowed
- Null insertion is also possible
- Serializable and cloneable intgerface are implemented
- Random access is not implemented
as the element are not stored in memory location and stored in form of nodes which contains address of the previous element and next element and thus easy to remove element by just removing the node and by adding the node element can be easily added
Methods
All the method of Collection method and there are some specific method of LinkedList
All the method of Collection method and there are some specific method of LinkedList
- public void addFirst(Object o);
- public void addLast(Object o);
- public Object getFirst(Object o);
- public Object getFirst(Object o);
- public Object removeFirst(Object o);
- public Object removeLast(Object o);
Constructor
There are two constructor in LinkedList class
- LinkeList ll = new LinkedList();
- It create empty LinkedList with the default capacity 10
- LinkeList ll = new LinkedList(Collection c);
Example:
import java.util.*;
class LinkedListDemo{
public static void main(String...s){
LinkedList l = new LinkedList();
l.add("A");
l.add("C");
l.add("Z");
l.add(null);
System.out.println(l);//[A,C,Z,null]
l.set(0,"X");
System.out.println(l);//[X,A,Z,null]
l.removeLast();
l.addFirst("NNN");
System.out.println(l);//[NNN,A,Z,M]
}
}
Difference between ArrayList and LinkedList
Difference between ArrayList and LinkedList
Differences
|
ArrayList
|
LinkedList
|
1
|
Best choice when our frequent operation is retrival
|
Best choice when our frequent operation is insertion
and deletion in th middle
|
2
|
Worst choice when our frequent operation is insertion and deletion in
th middle
|
Worst choice when our frequent operation is retrival
|
3
|
Underlying data structure growable Array
|
Underlying data structure id doublly linked list
|
4
|
Implements RandomAccess interface
|
Do not Implements RandomAccess interface
|


0 comments :
Post a Comment