Java/List

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Data Types, Variables,and Arrays
Operators
Packages, Classes, and Interfaces
Java Program
Modifiers
Control Statements
Exception Handling
Object Oriented Programming
Wrappers
Strings
Math
Arrays
Random Numbers
Date and Time
Regular Expressions
Java Collections
* List
* Set
* Comparators
* Iterators
* Map
* Legacy Classes and Interfaces
Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
RMI
Image, Audio, Video
GUI
Applets
Internationalization
JDBC
Java and XML

Contents

[edit] Java Lists

The list Interface extends Collection and it stores a sequence of elements as in list , all these elements can be easily accessed with their serial numbers or sequences and any element can be inserted to the list. But we have the situations when we get UnsupportedOperationException if the collection can’t be modified. List interface is implemented by two concrete classes


1) ArrayList

2) LinkedList

[edit] Java ArrayLists

This class extends AbstractList and implements the List interface.This class can be said to be the improved version of the arrays as in arrays we have the limitation of the fixed size which has to be known and defined well in advance and the n nothing much can be done with respect to the increase and decrease in the size of the array , so we have got the extended class from collections framework whch gives us the flexibility of having the variable array structure which can be dynamically increased or reduced as per the programmers need. An ArrayList is a variable-length array of object references.

ArrayList has three constructors shown here:

ArrayList( )

ArrayList(Collection c)

ArrayList(int capacity)

Array list (): This constructor builds an empty array list.

ArrayList(Collection c): This constructor builds an array list that is initialized with the elements of the collection c.

ArrayList (int capacity) : This constructor builds an array list that has the specified initial capacity. The capacity grows automatically as elements are added to an array list.


import java.util.ArrayList;
import java.util.List;
 
 
public class CreateArrayList {
 
	public static void main(String args[]) {
		List list = new ArrayList();
		list.add("1");
		list.add("2");
		list.add("3");
	}
}

The above code will create an ArrayList object and store three String objects in the ArrayList object.

import java.util.ArrayList;
import java.util.List;
 
 
public class DisplayArrayList {
 
	public static void main(String args[]) {
		List list = new ArrayList();
		list.add("1");
		list.add("2");
		list.add("3");
 
		for(int i = 0; i < 3; i++) {
			System.out.println(list.get(i));
		}
	}
}


Output of the above code

1
2
3

Below code is an example where the arraylist contains many Employee objects. Try this code a see how it works.

import java.util.ArrayList;
import java.util.List;
 
public class Employees {
 
	public static void main(String args[]) {
		List employeesList = new ArrayList();
		employeesList.add(new Employee("Sam", 100000));
		employeesList.add(new Employee("Rohan", 200000));
		employeesList.add(new Employee("John", 300000));
		employeesList.add(new Employee("Willey", 400000));
 
		int size = employeesList.size();
		for (int i = 0; i < size; i++) {
			Employee employee = (Employee) employeesList.get(i);
			System.out.println(employee.toString());
		}
	}
}
 
class Employee {
 
	private String name;
 
	private long sal;
 
	public Employee(String name, long sal) {
		this.name = name;
		this.sal = sal;
	}
 
	public String getName() {
		return name;
	}
 
	public long getSal() {
		return sal;
	}
 
	public String toString() {
 
		return "Name : " + getName() + "\n" + "Salary of " + getName() + ": "
				+ getSal();
	}
}


[edit] Java LinkedList

LinkedList Class: This Class extends AbsractSequentialList and implements the List Interface. It as it defines automatically by it’s term provides a Linked-list structure.

It has two constructors which are mentioned below:

LinkedList ( ) LinkedList(Collection c)

LinkedList ( ): This constructor builds an empty Linked List. LinkedList(Collection c): This builds a linked list which is initialized with elements of collection .

Try the below code

import java.util.LinkedList;
import java.util.List;
 
 
public class LinkedListExample {
 
	public static void main(String args[]) {
 
		List linkedList = new LinkedList();
		linkedList.add("1");
		linkedList.add("2");
		linkedList.add("3");
	}
}

[edit] Java ArrayList Vs. Linked List

There are some differences between an java.util.ArrayList and java.util.LinkedList. These differences are not so important for any small applications that processes small amount of data. Then they can very important for any small application that processes huge amount of data. These do affect the performance of a application if not analyzed properly.

An empty LinkedList takes 2-3 times less memory than an empty ArrayList. This becomes important if you plan to keep an adjacency list of nodes for each node in the graph (graph can be like a TreeSet. Tree is a type of graph) and you know beforehand that the nodes will have at most one or two adjacent nodes and the total number of nodes in the graph can be quite large.


Although both ArrayList and LinkedList allow insertion/deletion in the middle through similar operations (ie; by invoking add(int index) or remove(index)), these operations do not offer you the advantage of O(1) insertion/deletion for a LinkedList. For that, you must work through a ListIterator.


The important thing to remember when comparing LinkedList and ArrayList is that linked lists are more faster when inserting and removing at random locations in the list multiple times. If you want to add to the end of the list then an ArrayList would be the best choose.