Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday, 20 April 2017

LinkedList Java

 

Java LinkedList class

LinkeList is a class in java collection framework. LikedList class uses doubly linked list to store the elements. LinkedList class is a collection of nodes.

LinkedList class implements List interface and extends AbstractList class.

LinkedList class maintaining data in particular order.


Some of the important points about LinkedList class show below

  • LinkedList class can contain duplicate elements
  • LinkedList class maintains insertion order
  • LinkedList class are non-synchronized
  • LinkedList class is a collection of nodes
  • LinkedList class allows n number of null values
  • LinkedList class can be used as stack, list and queue.
  • LinkedList class provides fast manipulation because there is no shifting to be occurred 
In doubly linked list we can add or remove elements from both sides.


Hierarchy of LinkedList class in Java

LinkedList Java


Java LinkedList Constructor 

(1) LinkedList()

It is used to create an empty LinkedList.

(2) LinkedList(Collection c)

It is used to create an a linkedlist containing the elements of the specified collection.


LinkedList Methods

1) void add(int index, Object element)

This method is used to insert the specified element at the specified position index in LinkedList.

2) void addFirst(Object o)

This method is used to insert the given element at the begging of a list.

3) void addLast(Object o)

This method is used to add the given element to the end of a list.

4) boolean add(Object o)

This method is used to add the specified element to the end of a list.

5) int size()

This method returns the total number of elements in a list.

6) Object getFirst()

Return the first element in a list.

7) Object getLast()

Return the last element  in a list.

8) boolean contains(Object o)

Return true if the list contains a specified elements.

9) boolean remove(Object o)


Remove the first occurrence of a specified elements in the list.

10) void clear()

Removes all the elements of a list.

11) int indexOf(Object o)

Return the index of specified elements.

12) int lastIndexOf(Object o)

Return the index of last occurrence of the specified elements.

13) Object poll()

This method returns and removes the  first elements in a list.

14) Object pollFirst()

This method removes the  first elements in a list. Same as poll method.

15) Object pollLast()

This method removes and returns last elements in a list.

16) Object clone()

Return the copy of a list.

Java LinkedList Example

import java.util.*;
class LinkedListExample
{
public static void main(String args[])
{

//LinkedList declaration 

LinkedList<String> linkedlist = new LinkedList<String>();
linkedlist.add("red");//add String type elements in a list
linkedlist.add("blue");
linkedlist.add("yellow");
linkedlist.add("pink");
linkedlist.add("blue");

Iterator i = linkedlist.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

output : red
              blue
              yellow
              pink
              blue

Java LinkedList Example 2

import java.util.*;
class LinkedListExample2
{
public static void main(String args[])
{
LinkedList<String> linkedlist = new LinkedList<String>();

//Display the size of LinkedList before adding elements
System.out.println("size of LinkedList"+linkedlist.size());

//adding elements in list
linkedlist.add("ram");
linkedlist.add("ranbir");
linkedlist.add("shahid");
linkedlist.add("varun");
linkedlist.add("alia");

//Display elements of LinkedList

System.out.println("Elements of LinkedList"+linkedlist);

//Display the size of LinkedList after adding elements

System.out.println("size of LinkedList"+linkedlist.size());

//Add First and Last elements in the LinkedList

linkedlist.addFirst("Top");
linkedlist.addLast("Bottom");
System.out.println("after adding first and last elements"+linkedlist);

}

}

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate