You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Collections in java is a framework that provides an architecture to store and manipulate the group of objects.

All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections.

Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).

ArrayList

Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

ArrayList Example

Let’s see the ArrayList example first then we will discuss it’s methods and their usage.

collections in Java
import java.util.ArrayList;
... 
ArrayList<String> al = new ArrayList<String>();
al.add("a");
al.add("b");
al.add("c");
System.out.println(al);
Collections in Python
aList = []
aList.append("a");
aList.append("b");
aList.append("c");
print aList;

In the above example we have used only add method, that adds the object to the array list. However there are number of methods available which can be used directly using object of ArrayList class (set, remove, indexOf, get, size, contain, clear).

  • No labels