simple list in java code example

Example 1: how to make java list

//Creating arraylist example 
ArrayList<String> list = new ArrayList<String>();  
 
//Adding objects in arraylist 
list.add("Mango");    
list.add("Banana");   

//Change the element (index,"new value")
list.set(1,"Dates"); 

//Return the 2nd element, because index starts from 0
System.out.println("Returning element: " + list.get(1));

Example 2: how to create a list in java

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class scratch{
    public static void main(String[] args) {
        List<Integer> aList = new ArrayList<>();
        List<Integer> lList = new LinkedList<>();
    }
}

Example 3: list in java

LIST: Can store duplicate values,
      Keeps the insertion order. 
      It allows multiple null values, 
      Also we can read a certain value by index.
- ArrayList not syncronized, array based class 
- LinkedList not synchronized, doubly linked
- Vector is synchronized, thread safe

Example 4: how to add a list in a list java

List<SomePojo> list = new ArrayList<SomePojo>();

List<SomePojo> anotherList = new ArrayList<SomePojo>();
anotherList.add(list);