Java LinkedList class in Collection
Java LinkedList class in Collection
-- LinkedList class uses doubly linked list to store the elements and data. It provide a linked-list data structure.get free coins thetechieking
-- It inherits the AbstractList class and implements List and Deque interfaces in java.
-- LinkedList class is non synchronized.
-- LinkedList class follow insertion order.
-- LinkedList class can allow duplicate elements to store data.
-- manipulation is faster than ArrayList because no shifting needs.
Declaration of LinkedList class in java :
-- public class LinkedList<E> extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
Constructors of Java LinkedList :
LinkedList()
LinkedList(Collection c)
Methods of LinkedList class :
1) boolean add ( Object element ) :
-- It appends the element to the end of the list.
2) void add ( int index, Object element ) :
-- It inserts the element at the position ‘index’ in the list.
3) void addFirst ( Object element ) :
-- It inserts the element at the beginning of the list.
4) void addLast ( Object element ) :
-- It appends the element at the end of the list.
5) boolean contains ( Object element ) :
-- It returns true if the element is present in the list.
6) Object get (int index) :
-- It returns the element at the position ‘index’ in the list.
7) int indexOf ( Object element ) :
-- If element is found, it returns the index of the first occurrence of the
element.
8) Object remove ( int index ) :
-- It removes the element at the position ‘index’ in this list.
9) int size ( ) :
-- It returns the number of elements in this list.
10) void clear ( ) :
-- It remove all of the element from the list in java.
Example of LinkedList class :
import java.util.*;
public class JavaLinkedListExample {
public static void main ( String ar [ ] ) {
LinkedList < String > linkedList = new LinkedList < String > ( );
linkedList.add ("Nirav");
linkedList.add ("Patel");
linkedList.add ("On");
linkedList.add ("Java");
int size = linkedList.size ();
System.out.println("Size of linked list = " + size);
Iterator<String> itr=linkedList.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
public class JavaLinkedListExample {
public static void main ( String ar [ ] ) {
LinkedList < String > linkedList = new LinkedList < String > ( );
linkedList.add ("Nirav");
linkedList.add ("Patel");
linkedList.add ("On");
linkedList.add ("Java");
int size = linkedList.size ();
System.out.println("Size of linked list = " + size);
Iterator<String> itr=linkedList.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output is :
Size of linked list = 4
Nirav
Patel
On
Java
Java
thank you happy coding....
This comment has been removed by the author.
ReplyDeleteVisit thetechieking.com!
ReplyDelete