Java Collection: LinkedList - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Java Collection: LinkedList

Share This

It's a doubly-linked list implementation of the List and Deque interfaces.

The important facts about LinkedList class are:

  1. It can contain duplicate elements.
  2. Elements are stored according to the insertion order.
  3. It is not synchronized.

Hierarchy of ArrayList class


It inherits AbstractSequentialList class and implements Serializable, Cloneable, Iterable, Collection, Deque, List, and Queue interfaces.

Source code of Item.java
package com.t4b.test; public class Item { String name; int id; double price; public Item(String name, int id, double price) { super(); this.name = name; this.id = id; this.price = price; } @Override public String toString() { return "Item [name=" + name + ", id=" + id + ", price=" + price + "]"; } }

Source code of TestMain.java
package com.t4b.test; import java.util.LinkedList; public class TestMain { public static void main(String[] args) { LinkedList<Item> items = new LinkedList<Item>(); items.add(new Item("Apple", 1, 150.0)); items.add(new Item("Grape", 2, 250.0)); items.addFirst(new Item("Mango", 3, 10)); items.addLast(new Item("Pine Apple", 4, 100)); for (Item i : items) System.out.println(i); System.out.println(items.size()); } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.