ArrayList, Vector and LinkedList

ArrayList

ArrayList is like a array which size will be grown dynamically. Some of the main features of arraylist are given below

  1. Arraylist allows duplicates.
  2. It allows null, also it will allow multiple null.
  3. It’ s not threadsafe which means it is not synchronized.
  4. Arraylist will maintain insertion order
  5. It will not be sorted. (since it maintain insertion order it will not be sorted.) But we can sort the arrralylist using Collections.sort(). We will see arraylist sorting in a more detailed manner soon.
  6. It can be traverse using
    • For each
    • Iterators
    • Indexes

Note: Collections will not allow primitives. So when primitive is added to a collection, it get automatically converted into its equivalent Wrapper Class and loaded as an object into the collection.

ArrayList

package ArrayList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArrayListImplementation {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List integerList = new ArrayList();

		//Adding an Object, int will be automatically
		//converted to Wrapper class
		integerList.add(1);
		integerList.add(2);
		integerList.add(34);
		integerList.add(4);
		integerList.add(3);
		System.out.println("Values in integerList "+integerList);

		integerList.add(3, 98);
		System.out.println("Values in integerList after adding"
				+ " in a specified (3rd) position"+integerList);

		/* addAll -> will add a collection
		   of List objects to another list */
		List integerListTwo = new ArrayList();
		integerListTwo.add(65);
		integerListTwo.add(73);
		integerList.addAll(integerListTwo);
		System.out.println("Values in integerListTwo " +integerListTwo);
		System.out.println("Values in integerList after adding"
				+ " in a Collection (another list)"+integerList);

		/*addAll -> will also a collection of List objects
		to another list at a specified position*/  

		integerList.addAll(6, integerListTwo);
		System.out.println("Values in integerListTwo after adding"
				+ " in a specified (6th) position of integerList "+integerList);

		//contains  -> Returns true if this list contains the specified element.
		System.out.println("Check integerList contains 5 or not :"+integerList.contains(5));

		//containsAll  -> Returns true if this list contains the specified collection (integerListTwo).
		System.out.println("Check integerList contains integerListTwo "
				+ "collection or not :"+integerList.containsAll(integerListTwo));

		//equals
		System.out.println(integerList.equals(integerListTwo));
		System.out.println(integerList.equals(integerList));

		//get -> Returns the element at the specified position in this list
		System.out.println("Get the element at the specified positon "+ integerList.get(8));

		//indexOf  -> Returns the index of the first occurrence of the specified
		//element in this list, or -1 if this list does not contain the element.
		System.out.println("Index of 73 is at "+integerList.indexOf(73));

		//Iterator -> Returns an iterator over the elements in this list in proper sequence.
		System.out.println("Elements in iterator");
		System.out.println("--------------------");
		Iterator iterator=integerList.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}

		//lastIndexOf -> Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
		System.out.println("lastIndexOf of 3 is :"+integerList.lastIndexOf(3));

		//set -> Replaces the element at the specified position in this list with the specified element (optional operation).
		integerList.set(0, 18);
		System.out.println("After setting 18 at 0th position, value of integerLis"+integerList);

		//size -> Returns the number of elements in this list. If this list contains more than
		//Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
		System.out.println("Size of integerList :"+integerList.size());

		//remove ->Removes the element at the specified position in this list (optional operation).
		//Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
		integerList.remove(2);
		System.out.println(" remove 34 which is at 2nd position: "+integerList);

		//removeAll ->Removes from this list all of its elements that are contained in the specified collection (optional operation).
		integerList.removeAll(integerListTwo);
		System.out.println("After removing integerListTwo :"+integerList);		

		integerList.addAll(integerListTwo);
		//retainAll -> Retains only the elements in this list that are contained
		//in the specified collection (optional operation).In other words, removes from this list all
		//of its elements that are not contained in the specified collection.
		integerList.retainAll(integerListTwo);

		System.out.println("retain all will remove all elements except integgerListTwo :"+integerList);		

		/*clear ---> Removes all of the elements from this list (optional operation).
		The list will be empty after this call returns.*/
		integerList.clear();

		System.out.println("After clearing the list :"+integerList);

		//isEmpty() --> will check whether the list is empty or not
		System.out.println("checking the list is empty or not :"+integerList.isEmpty());

	}
}

 



Output is:

Values in integerList [1, 2, 34, 4, 3]
 Values in integerList after adding in a specified (3rd) position[1, 2, 34, 98, 4, 3]
 Values in integerListTwo [65, 73]
 Values in integerList after adding in a Collection (another list)[1, 2, 34, 98, 4, 3, 65, 73]
 Values in integerListTwo after adding in a specified (6th) position of integerList [1, 2, 34, 98, 4, 3, 65, 73, 65, 73]
 Check integerList contains 5 or not :false
 Check integerList contains integerListTwo collection or not :true
 false
 true
 Get the element at the specified positon 65
 Index of 73 is at 7
 Elements in iterator
 --------------------
 1
 2
 34
 98
 4
 3
 65
 73
 65
 73
 lastIndexOf of 3 is :5
 After setting 18 at 0th position, value of integerLis[18, 2, 34, 98, 4, 3, 65, 73, 65, 73]
 Size of integerList :10
 remove 34 which is at 2nd position: [18, 2, 98, 4, 3, 65, 73, 65, 73]
 After removing integerListTwo :[18, 2, 98, 4, 3]
 retain all will remove all elements except integgerListTwo :[65, 73]
 After clearing the list :[]
 checking the list is empty or not :true

package ArrayList;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class linkedListImplementation {

	public linkedListImplementation() {
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List integerList = new LinkedList();

		//Adding an Object, int will be automatically
		//converted to Wrapper class
		integerList.add(1);
		integerList.add(2);
		integerList.add(34);
		integerList.add(4);
		integerList.add(3);
		System.out.println("Values in integerList "+integerList);

		integerList.add(3, 98);
		System.out.println("Values in integerList after adding"
				+ " in a specified (3rd) position"+integerList);

		/* addAll -> will add a collection
		   of List objects to another list */
		List integerListTwo = new LinkedList();
		integerListTwo.add(65);
		integerListTwo.add(73);
		integerList.addAll(integerListTwo);
		System.out.println("Values in integerListTwo " +integerListTwo);
		System.out.println("Values in integerList after adding"
				+ " in a Collection (another list)"+integerList);

		/*addAll -> will also a collection of List objects
		to another list at a specified position*/  

		integerList.addAll(6, integerListTwo);
		System.out.println("Values in integerListTwo after adding"
				+ " in a specified (6th) position of integerList "+integerList);

		//contains  -> Returns true if this list contains the specified element.
		System.out.println("Check integerList contains 5 or not :"+integerList.contains(5));

		//containsAll  -> Returns true if this list contains the specified collection (integerListTwo).
		System.out.println("Check integerList contains integerListTwo "
				+ "collection or not :"+integerList.containsAll(integerListTwo));

		//equals
		System.out.println(integerList.equals(integerListTwo));
		System.out.println(integerList.equals(integerList));

		//get -> Returns the element at the specified position in this list
		System.out.println("Get the element at the specified positon "+ integerList.get(8));

		//indexOf  -> Returns the index of the first occurrence of the specified
		//element in this list, or -1 if this list does not contain the element.
		System.out.println("Index of 73 is at "+integerList.indexOf(73));

		//Iterator -> Returns an iterator over the elements in this list in proper sequence.
		System.out.println("Elements in iterator");
		System.out.println("--------------------");
		Iterator iterator=integerList.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}

		//lastIndexOf -> Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
		System.out.println("lastIndexOf of 3 is :"+integerList.lastIndexOf(3));

		//set -> Replaces the element at the specified position in this list with the specified element (optional operation).
		integerList.set(0, 18);
		System.out.println("After setting 18 at 0th position, value of integerLis"+integerList);

		//size -> Returns the number of elements in this list. If this list contains more than
		//Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
		System.out.println("Size of integerList :"+integerList.size());

		//remove ->Removes the element at the specified position in this list (optional operation).
		//Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
		integerList.remove(2);
		System.out.println(" remove 34 which is at 2nd position: "+integerList);

		//removeAll ->Removes from this list all of its elements that are contained in the specified collection (optional operation).
		integerList.removeAll(integerListTwo);
		System.out.println("After removing integerListTwo :"+integerList);		

		integerList.addAll(integerListTwo);
		//retainAll -> Retains only the elements in this list that are contained
		//in the specified collection (optional operation).In other words, removes from this list all
		//of its elements that are not contained in the specified collection.
		integerList.retainAll(integerListTwo);

		System.out.println("retain all will remove all elements except integgerListTwo :"+integerList);		

		/*clear ---> Removes all of the elements from this list (optional operation).
		The list will be empty after this call returns.*/
		integerList.clear();

		System.out.println("After clearing the list :"+integerList);

		//isEmpty() --> will check whether the list is empty or not
		System.out.println("checking the list is empty or not :"+integerList.isEmpty());

	}
}

Output:

Values in integerList [1, 2, 34, 4, 3]
Values in integerList after adding in a specified (3rd) position[1, 2, 34, 98, 4, 3]
Values in integerListTwo [65, 73]
Values in integerList after adding in a Collection (another list)[1, 2, 34, 98, 4, 3, 65, 73]
Values in integerListTwo after adding in a specified (6th) position of integerList [1, 2, 34, 98, 4, 3, 65, 73, 65, 73]
Check integerList contains 5 or not :false
Check integerList contains integerListTwo collection or not :true
false
true
Get the element at the specified positon 65
Index of 73 is at 7
Elements in iterator
——————–
1
2
34
98
4
3
65
73
65
73
lastIndexOf of 3 is :5
After setting 18 at 0th position, value of integerLis[18, 2, 34, 98, 4, 3, 65, 73, 65, 73]
Size of integerList :10
remove 34 which is at 2nd position: [18, 2, 98, 4, 3, 65, 73, 65, 73]
After removing integerListTwo :[18, 2, 98, 4, 3]
retain all will remove all elements except integgerListTwo :[65, 73]
After clearing the list :[]
checking the list is empty or not :true

Difference between ArrayList and Linked list

Reason ArrayList Linked List
Search Faster because it has index based search. Eg get(index) Slower.Since it implements doubly linked list which requires traversal through all the elements to search an element.
Deletion Slower Faster
Insertion Faster Slower

 

Similarities:

• Both implements List interface and extends AbstractList.
• Both will maintain Insertion Order.
• Both are non-synchronized (non threadsafe).
• Both can be made synchronized by using Collections.synchronizedList method

When to use Linked List and When to use arraylist:
1. For insertion and deletion go for linked list.
2. For search go for arraylist.

Comparing User Defined class with equals and ==

First lets compare without overridingg equals method

  
package Equalsand_Twoequals;

public class StudentCompare {

	public StudentCompare(String studentRollNo, String studentName) {
		// TODO Auto-generated constructor stub
		this.studentName = studentName;
		this.studentRollNo  = studentRollNo;
	}
	private String studentRollNo;
	private String studentName;
	public String getStudentRollNo() {
		return studentRollNo;
	}
	public void setStudentRollNo(String studentRollNo) {
		this.studentRollNo = studentRollNo;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	
	public static void main(String[] args) {
		StudentCompare compare1 = new StudentCompare("001","Sridhar");
		StudentCompare compare2 = new StudentCompare("002","Pathy");
		StudentCompare compare3 = new StudentCompare("003","Pathy");
		StudentCompare compare4 = compare3;
		
		System.out.println("Equals");
		System.out.println("-----");
		System.out.println(compare1.equals(compare2)); // false
		System.out.println(compare1.equals(compare3)); // false
		System.out.println(compare2.equals(compare3)); // false
		System.out.println(compare3.equals(compare4)); // true // since it is same memory location 
		System.out.println(compare2.equals(compare4)); // false
		
		System.out.println("==");
		System.out.println("--");
		System.out.println(compare1==compare2); // false
		System.out.println(compare1==compare3); // false
		System.out.println(compare2==compare3); // false
		System.out.println(compare3==compare4); // true
		System.out.println(compare2==compare4); // false
	}
}

Output will be
Equals
—–
false
false
false
true
false
==

false
false
false
true
false

package Equalsand_Twoequals;

public class StudentCompare {

	public StudentCompare(String studentRollNo, String studentName) {
		// TODO Auto-generated constructor stub
		this.studentName = studentName;
		this.studentRollNo  = studentRollNo;
	}
	private String studentRollNo;
	private String studentName;
	public String getStudentRollNo() {
		return studentRollNo;
	}
	public void setStudentRollNo(String studentRollNo) {
		this.studentRollNo = studentRollNo;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		StudentCompare compare = (StudentCompare)obj;
		if(compare.studentName.equalsIgnoreCase(this.studentName)){
			return true;
		}
		return false;
	}
	public static void main(String[] args) {
		StudentCompare compare1 = new StudentCompare("001","Sridhar");
		StudentCompare compare2 = new StudentCompare("002","Pathy");
		StudentCompare compare3 = new StudentCompare("003","Pathy");
		StudentCompare compare4 = compare3;
		
		System.out.println("Equals");
		System.out.println("-----");
		System.out.println(compare1.equals(compare2)); // false
		System.out.println(compare1.equals(compare3)); // false
		System.out.println(compare2.equals(compare3)); // true
		System.out.println(compare3.equals(compare4)); // true // since it is same memory location 
		System.out.println(compare2.equals(compare4)); // true
		
		System.out.println("==");
		System.out.println("--");
		System.out.println(compare1==compare2); // false
		System.out.println(compare1==compare3); // false
		System.out.println(compare2==compare3); // false
		System.out.println(compare3==compare4); // true
		System.out.println(compare2==compare4); // false
	}
}

Output will be
Equals
—–
false
false
true
true
true
==

false
false
false
true
false

Comparing String Values with Equals and ==

package Equalsand_Twoequals;

public class StringCompare {

	public StringCompare() {
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		String string1 = "sri";
		String string2 = "sri";
		String string3 = new String("sri");
		String string4 = new String("sri");
		
		System.out.println("EQUALS");
		System.out.println("------");
		System.out.println(string1.equals(string2));
		System.out.println(string1.equals(string3));
		System.out.println(string3.equals(string4));

		System.out.println("==");
		System.out.println("--");
		System.out.println(string1==string2);
		System.out.println(string1==string3);
		System.out.println(string3==string4);
	
		System.out.println("Imp One");
		System.out.println("-------");
		String string5 = new String("sri");
		String string6 = string5;
		System.out.println(string5.equals(string6));  //since string5 and string 6 are equal so true
		System.out.println(string5==string6); //since string5 and string 6 are equal so true // both in same memory location
		//
		System.out.println(string6==string3); //since string3 and string 6 are two different obj so false
		System.out.println(string6==string1); //since string1 and string 6 are two different obj so false
		
		
		String string7 = "sri";
		String string8 = string7;
		System.out.println(string8==string7);
		System.out.println(string8==string1);
		System.out.println(string8==string3);
	}
}

Output EQUALS —— true true true == — true false false Imp One ——- true true false false true true false

Comparing Interger Wrapper class with Equals and ==

    package Equalsand_Twoequals;

    public class IntegerCompare {

	public IntegerCompare() {
		// TODO Auto-generated constructor stub
	}
	public static void main(String[] args) {
		Integer i1 = 10;
		Integer i2 = 10;
		Integer i3 = new Integer(10);
		Integer i4 = new Integer(10);
		int i5 = 10;
		
		Integer i6 = 1000;
		Integer i7 = 1000;
		Integer i8 = new Integer(1000);
		Integer i9 = new Integer(1000);		
		int i10 = 1000;
		/* Equals*/
		System.out.println("EQUALS");
		System.out.println(i1.equals(i2));             
		System.out.println(i1.equals(i3));
		System.out.println(i3.equals(i4));
		System.out.println(i1.equals(i5));
		System.out.println(i3.equals(i5));
		
		System.out.println(i6.equals(i7));
		System.out.println(i6.equals(i8));
		System.out.println(i8.equals(i9));
		System.out.println(i6.equals(i10));
		System.out.println(i8.equals(i10));
		
		
		/*==*/
		System.out.println("==");
		System.out.println(i1==i2);             
		System.out.println(i1==i3);
		System.out.println(i3==i4);
		System.out.println(i1==i5);   // int compared either to Integer or new Integer will result true   
		System.out.println(i3==i5);		 // int compared either to Integer or new Integer will result true	
		
		System.out.println(i6==i7);   // false because == will compare only upto 127 
		System.out.println(i6==i8);
		System.out.println(i8==i9);
		System.out.println(i6==i10); // int compared either to Integer or new Integer will result true
		System.out.println(i8==i10); // int compared either to Integer or new Integer will result true
		
		
		Integer i11 = 10;
		Integer i12 = i11;
		Integer i13 = new Integer(10);
		Integer i14 = i13;
		int i15 = 10;
		
		Integer i16 = 1000;
		Integer i17 = i16;
		Integer i18 = new Integer(1000);
		Integer i19 = i18;		
		int i20 = 1000;
		
		System.out.println("======");
		
		System.out.println(i11==i12);
		System.out.println(i13==i14);   // Imp one
		System.out.println(i14==i11);
		
		System.out.println(i16==i17);	// Imp One
		System.out.println(i18==i19);   // Imp one
		System.out.println(i19==i16);
	}
}
       

Output is

EQUALS
true
true
true
true
true
true
true
true
true
true
==
true
false
false
true
true
false
false
false
true
true
======
true
true
false
true
true
false