Friday, 30 December 2016

How to create LinkedList in Java?

package com.kt4study.corejava.collection;

import java.util.LinkedList;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class LinkedListExample {

  public LinkedList<Integer> getLinkedList() {
    // Create An Instance Of LinkedList
    LinkedList<Integer> linkedList = new LinkedList<Integer>();
    // Add Integer Object in LinkedList.
    linkedList.add(52);
    linkedList.add(22);

    return linkedList;
  }

  public static void main(String[] args) {

    LinkedListExample linkedListExample = new LinkedListExample();
    //Print all the elements From LinkedList
    for (Integer element : linkedListExample.getLinkedList()) {
      System.out.println("Element from LinkedList :: " + element);
    }
  }

}

Output

Element from LinkedList :: 52
Element from LinkedList :: 22

No comments:

Post a Comment