Use of Some common methods in LinkedList?

Use of Some common methods in LinkedList?

Previous Home Next

 

This example will shows the operation of some common methods of  LinkedList

some method like add, remove , contain,addlast,addfirst,pull,pop etc are used in this example
 


 

package Example;
import java .util.LinkedList;
import java .util.Iterator;
public class LinklistExample {

/**
* @param args
*/
public static void main(String[] args) {
LinkedList Lst= new LinkedList(); // create object of LinkedList

// add elements to the LinkedList using add method
Lst.add("4");
Lst.add("6");
Lst.add("8");
Lst.add("10");
Lst.add("12");
Lst.add("14");
Lst.add("16");
Lst.add("18");

System.out.println("The elements of LinkedList prior to addition of other elemnts are:"+Lst);
Lst.addFirst("2"); // add element at the first positionof linklist
System.out.println("After adding element at the first position linkedList is:"+Lst);
Lst.addLast("20"); // add element at the last of linklist
System.out.println("after adding element Now the LinkedList contains:"+Lst);
// now we want add element at specific position of index using add(int index,element) method
Lst.add(5, "11");
System.out.println(" after inserting specific element at specific position :" +Lst);
// now we have to check the existence of the element in the LinkedList .we use contain(object o) method
//it will shows the result in boolean value true/false
boolean blnExists=Lst.contains("14");
System.out.println("The specific element is present in the LinkedList: "+blnExists);
// now make this list empty
// Lst.clear();
System.out.println("The linklist is empty:"+Lst.isEmpty());
boolean obj =Lst.containsAll(Lst);
System.out.println("The elements are present :"+obj);

//To iterate throught the list in Descendin order
Iterator itr = Lst.descendingIterator();
System.out.println("The elements of LinkedList are in descending order:");
while(itr.hasNext())
System.out.println(itr.next());
// To retrieve the first element of the list.use element().this retrieve the first element but not delete from the list.
Object obj1=Lst.element();
System.out.println("The elements of linkedlist after retrievel:"+obj1);
//to get the element at the specific positionof index
//it will return the elemnt at the specific index
Object obj2=Lst.get(6);
System.out.println(" The element at the specific index:"+obj2);
//To retrieve the element at the first position of the List
Object obj3=Lst.getFirst();
System.out.println("The element at the first position of the index is :"+obj3);
// To retrieve the element at the last positionof the index
Object obj4=Lst.getLast();
System.out.println("The element at the last position of the index is :"+obj4);
//To retrieve head of the List using peek() without removing it sismilarly used peekfirst() method to retrieve the element first element.
Object obj5=Lst.peek();
System.out.println("The head element of the index is :"+obj5);
// to get Last element using peeklast()
Object obj6=Lst.peekLast();
System.out.println("The element at the last position of the index is :"+obj6);
//Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.
Object obj7=Lst.pop();
System.out.println("The element retrieve and remove from the first position of the index is :"+obj7);
//public E remove()
// Retrieves and removes the head (first element) of this list.
Object obc=Lst.remove();
System.out.println("The element retrieve and remove head of the index is :"+obc);
// public List<E> subList(int fromIndex,
// int toIndex)
//Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive
Object sobj=Lst.set(2,4);
System.out.println("The specific portion of the List is :"+sobj);


// TODO Auto-generated method stub

}

}

The elements of LinkedList prior to addition of other elemnts are:[4, 6, 8, 10, 12, 14, 16, 18]
After adding element at the first position linkedList is:[2, 4, 6, 8, 10, 12, 14, 16, 18]
after adding element Now the LinkedList contains:[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
 after inserting specific element at specific position :[2, 4, 6, 8, 10, 11, 12, 14, 16, 18, 20]
The specific element is present in the LinkedList: true
The linklist is empty:false
The elements are present :true
The elements of LinkedList are in descending order:
20
18
16
14
12
11
10
8
6
4
2
The elements of linkedlist after retrievel:2
 The element at the specific index:12
The element at the first position of the index is :2
The element at the last position of the index is :20
The head element of the index is :2
The element at the last position of the index is :20
The element retrieve and remove from the first position of the index is :2
The element retrieve and remove head of the index is :4
The specific portion of the List is :10

Previous Home Next