List In Java

 List

  1. List is an interface which has Collection as a parent interface.
  2. List allows duplicate elements.
  3. List is indexed bases.
  4. There are multiple implemented classes for list.

 

ArrayList

  1. ArrayList implements the property from List.
  2. Can Store values dynamically in array list.
  3. Can Store different type of values inside array list.
  4. ArrayList is backed by array Data Structure internally.
  5. In arraylist every value is store into a separate index.
  6. Array list maintain the insertion order.
  7. ArrayList allows us random access.
  8. ArrayList provide slower performance for removal and addition (modification) operation.
  9. ArrayList provides faster performance for iteration/retrieval operation.
  10. ArrayList has default capacity 10.

Array List

 

Example 1 :-

import java.util.ArrayList;

public class ArrayListDemo {
	public static void main(String[] args) {
		ArrayList list = new ArrayList();
		list.add(23);
		list.add(54.44);
		list.add("String");
		list.add('C');
		list.add(23);
		list.add("xyz");
		
		System.out.println("List is "+list);
		
		list.remove("String");
		
		System.out.println("After Remove : "+list);
		
		System.out.println("is list contain xyz : "+list.contains("xyz"));
		
		System.out.println("NO. of values in list : "+list.size());
		
		System.out.println("is list empty : "+list.isEmpty());
		
		list.clear();
		
		System.out.println("is list empty after clear : "+list.isEmpty());
		
		ArrayList list1 = new ArrayList();
		list1.add(34);
		list1.add("abcd");
		list1.add(34.44);
		list1.add(55);
		
		list.addAll(list1);
		
		System.out.println("After addAll : "+list);
		
		ArrayList list2 = new ArrayList();
		list2.add(34);
		list2.add(55);
		list2.add("Xyz");
		
		System.out.println("Conatains All : "+list.containsAll(list2));

		list.removeAll(list2);
		
		System.out.println("After remove all : "+ list);
	}
}

 

Example 2 :-

import java.util.ArrayList;

public class ArrayListMethods {

	public static void main(String[] args) {
		ArrayList studId = new ArrayList();
		studId.add(45);
		studId.add(34);
		studId.add(56);
		studId.add(12);
		studId.add(65);
		studId.add(33);
		
		System.out.println(studId);
		
		System.out.println("At 2nd index : "+studId.get(2));;
		studId.add(2, 10);
		System.out.println(studId);
		studId.remove(4);
		System.out.println(studId);
		studId.set(3, 60);
		System.out.println(studId);
	}

}

 

LinkedList

  1. LinkedList implements the property from List, Dqeue and Queue interface.
  2. Can Store values dynamically in linked list.
  3. Can Store different type of values inside linked list.
  4. LinkedList is based on Doubly linked list Data Structure.
  5. LinkedList is not based on index, but you will get the methods for index based operations.
  6. LinkedList is always use for faster modification (addition, removal).
  7. LinkedList provide slower performance for Iteration/retrieval.
  8. Random access is not possible in LinkedList.
  9. LinkedList maintain the insertion order.
  10. Can perform operation from both the ends in LinkedList using peek, add, remove, pop, poll methods.

Example:-

import java.util.LinkedList;
public class LinkedListdemo {

	public static void main(String[] args) {
		LinkedList list = new LinkedList();
		list.add(12);
		list.add("abc");
		list.add("xyz");
		list.add(12);
		 System.out.println(list);
		 
		list.addFirst(7);
		list.addLast("end");
		
		System.out.println("List After add first & last"+list);
		
		System.out.println(list.contains("abc"));
		
		Object val = list.peek();
		 
		System.out.println("peek "+val);
		System.out.println("After peek "+list);
		
		val = list.poll();
		
		System.out.println("poll "+val);
		System.out.println("After poll "+list);
		
		val = list.pop();
		
		System.out.println("pop "+val);
		System.out.println("After pop "+list);
		
		list.push(99);
		
		list.push(00);
		
		System.out.println(list);
	}

}

 

Vector

  1. Vector implements the properties from List interface.
  2. Can Store values dynamically in Vector.
  3. Can Store different type of values inside Vector.
  4. Vector is also based on array data structure.
  5. Vector allows random access
  6. Vector is use for faster iteration and slower updation.
  7. Vector objects are synchronized.
  8. It is slower than ArrayList.
  9. Vector objects are thread safe.
  10. Vector is a legacy class (the class which is introduce in java 1st version)

 

Join Telegram : Click Here

 

All Full Stack Java Study Material

 

Java the programing language

 

Job’s For Fresher

 

Share This Information To Your Friends and Your College Group’s, To Help Them !