Skip to content

Instantly share code, notes, and snippets.

@amelieykw
Last active July 17, 2018 07:04
Show Gist options
  • Save amelieykw/e26741b7baefa1ee209be7c44e4eeed9 to your computer and use it in GitHub Desktop.
Save amelieykw/e26741b7baefa1ee209be7c44e4eeed9 to your computer and use it in GitHub Desktop.
[Java - Java Collections] #Java #Collections #tutorial #interview #ArrayList #LinkedList #Vector #HashSet #LinkedHashSet #TreeSet #HashMap #TreeMap #LinkedHashMap #Hashtable #IteratorandListIterator #ComparableandComparator #JavaCollectionsInterviewQuestions
  1. ArrayList
  2. LinkedList
  3. Vector
  4. HashSet
  5. LinkedHashSet
  6. TreeSet
  7. HashMap
  8. TreeMap
  9. LinkedHashMap
  10. Hashtable
  11. Iterator and ListIterator
  12. Comparable and Comparator
  13. Java Collections Interview Questions

Collections Framework hierarchy

Java Collections – List

  • A List is an ordered Collection (sometimes called a sequence).
  • Lists may contain duplicate elements.
  • Elements can be inserted or accessed by their position in the list, using a zero-based index.

Table of Contents

ArrayList Basics

ArrayList in Java

Arraylist class implements List interface.

Most of the developers choose Arraylist over Array

ArrayList is a resizable-array implementation of the List interface.

It implements all optional list operations, and permits all elements, including null.

Array VS ArrayList

Array

Arrays is that they are of fixed length so if it is full you cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink.

ArrayList

ArrayList can dynamically grow and shrink after addition and removal of elements.

ArrayList class enables us to use predefined methods of it which makes our task easy.

ArrayList Example in Java
import java.util.*;

public class ArrayListExample {
   public static void main(String args[]) {
      /*Creation of ArrayList: I'm going to add String
       *elements so I made it of string type */
	  ArrayList<String> obj = new ArrayList<String>();

	  /*This is how elements should be added to the array list*/
	  obj.add("Ajeet");
	  obj.add("Harry");
	  obj.add("Chaitanya");
	  obj.add("Steve");
	  obj.add("Anuj");

	  /* Displaying array list elements */
	  System.out.println("Currently the array list has following elements:"+obj);

	  /*Add element at the given index*/
	  obj.add(0, "Rahul");
	  obj.add(1, "Justin");

	  /*Remove elements from array list like this*/
	  obj.remove("Chaitanya");
	  obj.remove("Harry");

	  System.out.println("Current array list is:"+obj);

	  /*Remove element from the given index*/
	  obj.remove(1);

	  System.out.println("Current array list is:"+obj);
   }
}

Output:

Currently the array list has following elements:[Ajeet, Harry, Chaitanya, Steve, Anuj]
Current array list is:[Rahul, Justin, Ajeet, Steve, Anuj]
Current array list is:[Rahul, Ajeet, Steve, Anuj]

Methods of ArrayList class

index function name description example
1 add( Object o) adds an object o to the arraylist at last position obj.add("hello");
2 add(int index, Object o) adds the object o to the array list at the given index obj.add(2, "bye");
3 remove(Object o) Removes the object o from the ArrayList obj.remove("Chaitanya");
4 remove(int index) Removes element from a given index obj.remove(3);
5 set(int index, Object o) updating an element. replaces the element present at the specified index with the object o. obj.set(2, "Tom");
6 int indexOf(Object o) Gives the index of the object o. If the element is not found in the list then this method returns the value -1. int pos = obj.indexOf("Tom");
7 Object get(int index) returns the object of list which is present at the specified index String str= obj.get(2);
8 int size() gives the size of the ArrayList – Number of elements of the list int numberofitems = obj.size();
9 boolean contains(Object o) checks whether the given object o is present in the array list if its there then it returns true else it returns false. obj.contains("Steve");
10 clear() for removing all the elements of the array list in one go obj.clear();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment