Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Collection : LinkedList Example

This tutorial contains description of Collection LinkedList with example.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Collection : LinkedList Example

This tutorial contains description of Collection LinkedList with example.

LinkedList :

LinkedList class implements the List interface. It is ordered by index position. It is like ArrayList ,except that it provides concept of doubly-linked.
It implements methods of List interface and concept of doubly-linked uses some new methods for adding and removing from the beginning or end, which makes easy to use it for Stack and Queue implementation.
Keep in mind that a LinkedList may iterate more slowly than an ArrayList, but it's a good choice when you need fast insertion and deletion

Example :

package collection;

import java.util.LinkedList;

class LinkedListExample {
public static void main(String[] args) {

LinkedList<Integer> list = new LinkedList<Integer>();
System.out.println("Initial size of LinkedList : " + list.size());

// Adding elements to the LinkedList
list.add(111);
list.add(222);
list.add(333);
list.add(1, 666);

System.out.println("Size of LinkedList after adding elements : "
+ list.size());
System.out.println("Elements in LinkedList : " + list);

// Removing elements
list.remove(1);
System.out.println("Size of LinkedList after removing element : "
+ list.size());
System.out.println("Elements in LinkedList : " + list);
}
}

Description : In this example we are creating LinkedList of Integer as - LinkedList<Integer> list = new LinkedList<Integer>();
add()
method is used here to add elements into list. For calculating number of elements in list we are using size() method.
remove() method is used here to delete elements from list.

Output :

Initial size of LinkedList : 0
Size of LinkedList after adding elements : 4
Elements in LinkedList : [111, 666, 222, 333]
Size of LinkedList after removing element : 3
Elements in LinkedList : [111, 222, 333]

Advertisements

Ads
Share on Google+Share on Google+

Collection : LinkedList Example

Posted on: September 7, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Collection : LinkedList Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads