EXPERIMENT NO 13
AIM : Program to implement Collection examples on ArrayList, LinkedList.
OBJECTIVE : Understand collection framework and implement it.
DESCRIPTION:
*The Java Collections Framework standardizes the way in which groups of objects are handled by your
*Collections were not part of the original Java release, but were added by J2SE 1.2. Prior to the
Collections Framework, Java provided ad hoc classes such as Dictionary, Vector, Stack, and Properties
to store and manipulate groups of objects.
*The Collections Framework was designed to meet several goals.
-First, the framework had to be high-performance. The implementations for the fundamental collections
(dynamic arrays, linked lists, trees, and hash tables) are highly efficient. You seldom, if ever, need to code
one of these “data engines” manually.
-Second, the framework had to allow different types of collections to work in a similar manner and with a
high degree of interoperability.
-Third, extending and/or adapting a collection had to be easy. Toward this end, the entire Collections
Framework is built upon a set of standard interfaces. Several standard implementations (such as
LinkedList, HashSet, and TreeSet) of these interfaces are provided that you may use as-is. You may also
implement your own collection, if you choose. Various special-purpose implementations are created for
your convenience, and some partial implementations are provided that make creating your own collection
class easier. Finally, mechanisms were added that allow the integration of standard arrays into the
Collections Framework.
*The Collection Classes
Now that you are familiar with the collection interfaces, you are ready to examine the standard classes
that implement them. The standard collection classes are summarized in the following table:
*ArrayList supports dynamic arrays that can grow as needed Standared array supported by java cannot grow
or shrink, after created you must know in advance how many elements an array will hold, but, sometimes,
you may not know until run time precisely how large an array you need. To handle this situation, Collections
Framework defines ArrayList In essence, an ArrayList is a variable-length array of object REFERENCES
That is, an ArrayList can dynamically increase or decrease in size.
Lists are created with an initial size, when this size is exceeded, the collection is automatically
enlarged.When objects are removed, the array can be shrunk. ArrayList has the constructors shown
here:
ArrayList( )
ArrayList(Collection<? extends E> c)
ArrayList(int capacity)
The first constructor builds an empty array list. The second constructor builds an array list that is
initialized with the elements of the collection c. The third constructor builds an array list that has the
specified initial capacity. The capacity is the size of the underlying array that is used to store the
elements The capacity grows automatically as elements are added to an array list.
*The LinkedList class extends AbstractSequentialList and implements the List, Deque, and Queue
interfaces. It provides a linked-list data structure. LinkedList is a generic class that has this declaration:
class LinkedList<E>
Here, E specifies the type of objects that the list will hold LinkedList has the two constructors shown here:
LinkedList( )
LinkedList(Collection<? extends E> c)
The first constructor builds an empty linked list. The second constructor builds a linked list that is
initialized with the elements of the collection c. programs.
import java.util.*;
class CollectionDemo {
System.out.println(“Om Gaikwad”);
public static void main(String[] args) {
// Demonstrate ArrayList
ArrayList<String> al = new ArrayList<>();
System.out.println("Initial size of ArrayList: " + al.size());
// Add elements to the ArrayList
al.add("C"); al.add("A");
al.add("E"); al.add("B");
al.add("D"); al.add("F");
al.add("T"); al.add("O");
al.add("M");
al.add(1, "A2"); // Insert "A2" at index 1
System.out.println("Size of ArrayList after additions: " + al.size());
System.out.println("Contents of ArrayList: " + al);
// Remove elements from the ArrayList
al.remove("F"); // Remove by value al.remove(2);
// Remove by index
System.out.println("Size of ArrayList after deletions: " + al.size());
System.out.println("Contents of ArrayList: " + al);
// Demonstrate LinkedList
LinkedList<String> ll = new LinkedList<>();
// Add elements to the LinkedList
ll.add("F"); ll.add("B");
ll.add("D"); ll.add("E");
ll.add("Y"); ll.add("X");
ll.add("C");
ll.addLast("Z"); // Add "Z" at the end
ll.addFirst("A"); // Add "A" at the beginning ll.add(1,
"A2"); // Insert "A2" at index 1
System.out.println("\nOriginal contents of LinkedList: " + ll);
// Remove elements from the LinkedList
ll.remove("F"); // Remove by value ll.remove(2);
// Remove by index
System.out.println("Contents of LinkedList after deletion: " + ll);
// Remove first and last elements
ll.removeFirst(); ll.removeLast();
System.out.println("LinkedList after deleting first and last: " + ll); // Get and set a value String
val = ll.get(2);
ll.set(2, val + " Changed"); // Change the value at index 2
System.out.println("LinkedList after change: " + ll);
Output: