Today we’re going to begin the JAVA sequence which might be very useful for all groups of individuals like Students, making ready for the JAVA interview?

You should undergo this post before studying anything in JAVA.

Collections are the most necessary half in JAVA which helps to handle information in a structured approach and by optimizing heap reminiscence and stack reminiscence.

What are collections in JAVA?

The Java collections framework is a set of courses and interfaces that implement generally reusable assortment information buildings.

Although known as a framework, it really works within the method of a library. The collections framework offers interfaces that outline numerous collections and courses that implement them.

  • It gives us readymade architecture.
  • set of classes and interfaces
  • storing and manipulating a group of objects
  • Interfaces and Classes (Implementation of Interfaces)
  • Effective usage of Algorithm

Collections in JAVA provides us ways to iterate the elements of a collection and here will learn  difference between ArrayList and LinkedList classes in collection.

If you are looking to know more about the difference between ArrayList and Vector classes in the collection framework then you must read this post.

Collection Interface

The Collection interface is the interface that is carried out by all of the courses within the assortment framework. It declares the strategies that each assortment can have.

In other words, we want to say that the Collection interface builds the foundation on which the gathering framework depends upon.

Some of the strategies of the Collection interface are Boolean add ( Object obj), Boolean addAll ( Collection c), void clear(), and so on. that are carried out by all of the subclasses of the Collection interface.

What is List Interface?

List interface is the child interface of Collection interface. It inhibits a list type data structure in which we can store the ordered collection of objects. List allows duplicate values.

Classed like ArrayList, LinkedList, Vector, and Stack implements List interface. lets see few example to understand this in better way

How to declare List interface?

  • List <data-type> listname1= new ArrayList();  
  • List <data-type> listname2 = new LinkedList();  
  • List <data-type> listname3 = new Vector();  
  • List <data-type> listname4 = new Stack();  

    there are several operations(various methods) like insert, delete, and access can be performed on the elements from the list.

ArrayList

Now lets talk about ArrayList class which implements the List interface. ArrayList can be used  for creating dynamic array to store the duplicate element of different data types.

  • insertion order
  • non-synchronized
  • Elements of ArrayList can be randomly accessed

    Let’s see this with the help of an example
import java.util.*;  
class TestJavaCollectionArrayList{  
public static void main(String args[]){  
ArrayList<String> arraylist=new ArrayList<String>();
// creation of array list above
  
arraylist.add("BlogsPost1"); //Adding object in arraylist  
arraylist.add("BlogsPost2");  
arraylist.add("BlogsPost1");  //duplicate value in arraylist
arraylist.add("BlogsPost3");  
//how to Traversw list through Iterator  
Iterator iteratorex =arraylist.iterator();  
while(iteratorex .hasNext())
{  
System.out.println(iteratorex .next());  
}  
}  
}  

Output of above program will be

BlogsPost1
BlogsPost2
BlogsPost1
BlogsPost3

Lets try to write one program using Array List

Write a JAVA program to find difference between 2 ArrayList

package practice;

//Java program to demonstrate the working of ArrayList in Java 

import java.io.*;
import java.util.*;

class IntegerArrayListDemonstration {
	public static void main(String[] args) {
	// Size of the 	// ArrayList
	int n = 10;

	// Declaring the ArrayList with // initial size n
	ArrayList<Integer> arrli = new ArrayList<Integer>(n);

		int lenlen = arrli.size();
// Appending new elements at // the end of the list
		for (int i = 1; i <= n; i++)
		arrli.add(i);

		// Printing elements
		System.out.println(arrli);

		// Remove element at index 3
		arrli.remove(3);

		// Displaying the ArrayList after deletion
		System.out.println(arrli);

		// Printing elements one by one
		for (int i = 0; i < arrli.size(); i++)
			System.out.print(arrli.get(i) + " ");

		// creating 2nd array list
	ArrayList<Integer> intarray2 = new ArrayList<Integer>();

		intarray2.add(23);
		intarray2.add(2);
		intarray2.add(97);

		arrli.retainAll(intarray2);

	for (int i = 0; i < intarray2.size(); i++) {
		System.out.println(intarray2.get(i) + " ");
		}

System.out.println("\n Difference between two list arrli and intarray2" + arrli);

intarray2.remove(0);
for (int j = 0; j <= intarray2.size() - 1; j++) {
System.out.println(intarray2.get(j) + " ");

		}

	}
}

Output of this ArrayList program will be

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 5, 6, 7, 8, 9, 10]
1 2 3 5 6 7 8 9 10 23
2
97
Difference between two list arrli and intarray2[2]
2
97

Practice ArrayList and see you in next post where will continue discussion about LinkedList and Vector.

Link :

Leave a Reply

Your email address will not be published. Required fields are marked *

Copy link