Normally, an array is a collection of similar type of elements that have a contiguous memory location.
Java array is an object which contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
Array in java is index-based, the first element of the array is stored at the 0 index.
Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.
There are two types of array.
//Java Program to illustrate how to declare, instantiate, initialize
//and traverse the Java array.
class Testarray{
public static void main(String args[]){
int a[]=new int[5]; //declaration and instantiation
a[0]=10; //initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}}
The Queue interface is available in java.util package and extends the Collection interface. The queue collection is used to hold the elements about to be processed and provides various operations like the insertion, removal etc. It is an ordered list of objects with its use limited to insert elements at the end of the list and deleting elements from the start of list i.e. it follows the FIFO or the First-In-First-Out principle.
LinkedList, ArrayBlockingQueue and PriorityQueue are the most frequently used implementations.
The Collection interface is the foundation upon which the collections framework is built. It declares the core methods that all collections will have.
A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following :
In addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Although maps are not collections in the proper use of the term, but they are fully integrated with collections.
The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.
ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
Code to create a generic integer ArrayList :
ArrayList<Integer> arrli = new ArrayList<Integer>();
Note : More examples are given below.
The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.
import java.util.*; // All the classes and interfaces are part of the util package
public class CollectionsDemo {
public static void main(String[] args) {
// ArrayList
List a1 = new ArrayList();
a1.add("Zara");
a1.add("Mahnaz");
a1.add("Ayan");
System.out.println(" ArrayList Elements");
System.out.print("\t" + a1);
// LinkedList
List l1 = new LinkedList();
l1.add("Zara");
l1.add("Mahnaz");
l1.add("Ayan");
System.out.println();
System.out.println(" LinkedList Elements");
System.out.print("\t" + l1);
// HashSet
Set s1 = new HashSet();
s1.add("Zara");
s1.add("Mahnaz");
s1.add("Ayan");
System.out.println();
System.out.println(" Set Elements");
System.out.print("\t" + s1);
// HashMap
Map m1 = new HashMap();
m1.put("Zara", "8");
m1.put("Mahnaz", "31");
m1.put("Ayan", "12");
m1.put("Daisy", "14");
System.out.println();
System.out.println(" Map Elements");
System.out.print("\t" + m1);
}
}