Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

List interface in Java

List is a fundamental and widely-used collection type in the Java Collections Framework. as shown in diagram List is an interface in java which contains following common features

It allows duplicate element.
Elements are ordered(orders them by their insertion order (index)).
It's default capacity is 10 (Except LinkedList)
It does not use hashCode.

Collection class Hierarchy:


Implementation of List interface:

  ArrayList.
  LinkedList
 Vector

ArrayList:

Java ArrayList is the resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking).

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not * specified beyond the fact that adding an element has constant amortized.

Note that ArrayList implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.

 A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification. This is typically accomplished by synchronizing on some object that naturally encapsulates the list.


Creating instance of ArrayList:

You can use ArrayList in Java with or without Generics both are permitted by generics version is recommended because of enhanced type-safety
List listwithString = new ArrayList(); // Generic ArrayList to store only String
List listwithString = new ArrayList<>(); // Using Diamond operator from Java 1.7

How to find size of ArrayList

The size of an ArrayList in Java is a total number of elements currently stored in ArrayList
int size = listwithString.size();

Checking Index of an Item in Java ArrayList

int index = stringList.indexOf("Item"); //location of Item object in List
Note: We must override only equals method inside java class which we are going to use inside ArrayList not hashCode;

How to check if ArrayList is Empty in Java

boolean result = stringList.isEmpty(); //isEmpty() will return true if List is empty
if(stringList.size() == 0){
System.out.println("ArrayList is empty");
}

How to remove an element from ArrayList

stringList.remove(0);
stringList.remove(item);

How to make ArrayList readonly in Java?

List list = Collections.unmodifiableList(new ArrayList(...));

How to synchronized ArrayList in Java?

List list = Collections.synchronizedList(new ArrayList(...));

How to sort ArrayList in Java?

Collections.sort(List list)

How to converting from ArrayList to Array in Java

String[] itemArray = new String[stringList.size()];
String[] returnedArray = stringList.toArray(itemArray);

How to create ArrayList from Array in Java?

ArrayList fruitList = Arrays.asList(new String[]{"Apple", "Mango", "Banana"); //this is not read only List you can still update value of existing elements