Tuesday, September 6, 2011

Java LinkedList Collection Framework

What is a LinkedList ? LinkedList is a class extend from java.util.AbstractSequentialList and implement the interface List and Deque, this framework is used to handle the type of data structures stack and queue. In this post I will not discuss about the stack and queue. so I just focus on How to Implement  Stack & Queue in Java.

Why use a LinkedList ?
1. An unlimited number of elements
2. The number of elements that are flexible. When an index removed, the total index on the index will also change so none index is empty.
3. Able to handle the data structure of type stack, queue

Some of the most common method used in the LinkedList object:
1. Adding Element Methods

RETURN METHODE STACK QUEUE EXPLAIN
boolean add(E e)
*
Appends the specified
element to the end of this list
void addFirst(E e)
*
Inserts the specified element
at the beginning of this list.
void addLast(E e)
*
Appends the specified element
to the end of this list.
boolean offer(E e)
*
Adds the specified element
as the tail (last element) of this list.
boolean offerFirst(E e)
*
Inserts the specified element
at the front of this list.
boolean offerLast(E e)
*
Inserts the specified element
at the end of this list.
void push(E e)
*
Pushes an element onto
the stack represented by this list.

2. Get Element Methods
RETURN METHODE STACK QUEUE EXPLAIN
E get(int index)
*
*
Returns the element at the specified position in this list.
E getFirst()
*
*
Returns the first element in this list.
E getLast()
*
*
Returns the last element in this list.
E peek()
*
Retrieves, but does not remove, the head (first element) of this list.
E peekFirst()
*
Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.
E peekLast
*
Retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

3. Remove Element Methods
RETURN METHODE STACK QUEUE EXPLAIN
E poll()
*
Retrieves and removes the head (first element) of this list
E pollFirst()
*
Retrieves and removes the first element of this list, or returns null if this list is empty.
E pollLast()
*
Retrieves and removes the last element of this list, or returns null if this list is empty.
E remove(int index)
*
*
Removes the element at the specified position in this list.
E removeFirst()
*
*
Removes and returns the first element from this list.
E removeLast()
*
*
Removes and returns the last element from this list.
E pop()
*
Pops an element from the stack represented by this list.

PRACTICE
Class Person
package person;
/**
*
* @author Faicker Himmah
*/
public class Person {
    String name;
    String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

STACK
Insert Element
//create object LinkedList to contain object Person
LinkedList<Person> linked = new LinkedList<Person>();

        Person p1 = new Person();
        p1.setName("Ar-Rohman Software House");
        p1.setAddress("Malang");
        Person p2 = new Person();
        p2.setName("Jasa Pembuatan Software");
        p2.setAddress("Malang");
        Person p3 = new Person();
        p3.setName("Faiqul Himmah");
        p3.setAddress("Malang");
        Person p4 = new Person();
        p4.setName("Zahroul Ulum");
        p4.setAddress("Malang");
        // insert object Person into linked
        linked.push(p1);
        linked.push(p2);
        linked.push(p3);
        linked.push(p4);
       for(Person p : linked){
            System.out.println("Name : "+p.getName());
            System.out.println("Address : "+p.getAddress());
        }

Remove Element
linked.pop() //remove last inserted

QUEUE
Insert Element
//create object LinkedList to contain object Person
LinkedList<Person> linked = new LinkedList<Person>();
        ArrayList<Person> list = new ArrayList<Person>();
        Person p1 = new Person();
        p1.setName("Ar-Rohman Software House");
        p1.setAddress("Malang");
        Person p2 = new Person();
        p2.setName("Jasa Pembuatan Software");
        p2.setAddress("Malang");
        Person p3 = new Person();
        p3.setName("Faiqul Himmah");
        p3.setAddress("Malang");
        Person p4 = new Person();
        p4.setName("Zahroul Ulum");
        p4.setAddress("Malang");
        // insert object Person into linked
        linked.add(p1);
        linked.add(p2);
        linked.add(p3);
        linked.add(p4);
       for(Person p : linked){
            System.out.println("Name : "+p.getName());
            System.out.println("Address : "+p.getAddress());
        }

Remove Element
linked.poll(); //remove first inserted

CONCLUSION
What is the difference ArrayList & Linked List ?
ArrayList
LinkedList
An unlimited number of elements
*
*
The number of elements that are flexible
*
*
Able to handle the data structure of type stack, queue
*
Implemeted interface List
*
*
Implemeted interface Deque
*

Source : Java 2 SE 6 Documentation (HtmlHelp version by F. Allimant)

0 comments:

Post a Comment

If you have something to say about this post, please fell free to write it in the comment form below. We will reply to all comments as soon as possible. Thanks ...