Searching...
Saturday, 6 October 2012

Java Collections: LinkedList Example

03:50
         Java uses a doubly-linked list
-          it can be traversed from the beginning or the end
         LinkedList provides methods to get, remove and insert an element at the beginning and end of the list
-          these operations allow a linked list to be used as a stack or a queue
         LinkedList is not synchronized
-          problems if multiple threads access a list concurrently
            LinkedList must be synchronized externally
image

Example

package com.vinod.collections;

import java.util.LinkedList;
import java.util.List;

public class LinkedListExample {

    public static void main(String[] args) {
        List<String> lList = new LinkedList<String>();
        // Adding objects
        lList.add("1");
        lList.add("2");
        lList.add("3");
        lList.add("4");
        lList.add("5");

        // Iterating values

        for (String names : lList) {
            System.out.println("LinkedList values : " + names);
        }
    }

}
 
 

Ouput

LinkedList values : 1

LinkedList values : 2

LinkedList values : 3

LinkedList values : 4

LinkedList values : 5

 

 

0 comments: