Introduction
This Java tutorial explains how to initialize a list, update an initialized list and show the output of all the elements of the java initialized list.
Java list is just like a list that we use in real life for example a list of items to get from a grocery store would go something like milk, cheese, oranges, tape, etc. Similarly, a java List contains a list of items.
Imports used for java initialize list
First of all, to initialize a list, we need to import a list library
// important imports
import java.util.List;
Different methods to initialize a Java list
The list is declared by the following method:
// java initialize list through List constructor.
List<datatype> ListName=new List();
In this way, a list can be initialized. The list interface can be initialized through 4 list classes.
- ArrayList
- LinkedList
- Stack
- Vector
The code below implements how to initialize the list through these classes.
// important imports
import java.util.*;
import java.util.List;
public class Initialize_List {
public static void main(String[] args) {
// Java Initialize List, instance ArrayList.
List<String>myArraylist = new ArrayList();
// Java Initialize List, instance LinkedList.
List<String>myLinkedList = new LinkedList();
// Java Initialize List, instance Vector
List<String>myVector = new Vector();
// Java Initialize List,instance Stack.
List<String>myStack = new Stack();
}
}
In this code, we declared 4 different lists all of the datatype String, and different types of lists.
Method-1: Using add()
If the Java list initialized is an ArrayList
, LinkedList
or Stack then items can be added to it through the add function.
The add function is a function of the class ArrayList
and in this function, we pass the list item as a parameter of the function, i.e.
ArrayList.add(list_item);
LinkedList.add(list_item);
Stack.add(list_item);
Example-1: Using add() method with ArrayList
Following code depicts how we can use the add method by using the instance ArrayList
.
// important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
// Java Initialize List, instance ArrayLit
List<String>myArraylist = new ArrayList();
// a string item to add is declared here
String str = "this is a linux cloud tutorial";
// another string item to add is declared here
String str1 = "Java initialize list";
// add the item, used add function, string is passed as a parameter
myArraylist.add(str);
myArraylist.add(str1);
// loop to display output.
for (String exer: myArraylist) {
System.out.println(exer + " ");
}
}
}
The following code depicts how we can use the add method by using the instance LinkedList.
The output of this code is :
this is linux cloud tutorial
Java initialize list
Example-2: Using add() method with LinkedList
The following code depicts how we can use the add method by using the instance LinkedList
.
// important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
// Java Initialize List, instance LinkedList.
List<String>myLinkedlist = new LinkedList();
// a string item to add is declared here
String str = "this is a linux cloud tutorial";
// another string item to add is declared here
String str1 = "Java initialize list";
// add the item, used add function, string is passed as a parameter
myLinkedlist.add(str);
myLinkedlist.add(str1);
// loop to display output.
for (String exer: myLinkedlist) {
System.out.println(exer + " ");
}
}
}
Example-3: Using add() method with instance Stack
The following code depicts how we can use the add method by using the instance Stack.
// important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
// Java Initialize List, instance Stack
List<String>myStack = new Stack();
// a string item to add is declared here
String str = "this is a linux cloud tutorial";
// another string item to add is declared here
String str1 = "Java initialize list";
// add the item, used add function, string is passed as a parameter
myStack.add(str);
myStack.add(str1);
// loop to display output.
for (String exer: myStack) {
System.out.println(exer + " ");
}
}
}
The output of all these codes will be:
this is a linux cloud tutorial
Java initialize list
Method-2: Using asList()
The as All method is used as a method of the class Arrays, the object made is of list type, the list initialized is of instance list. The code below shows how asList()
the method is used to add elements to a list through asList()
method.
// important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
// Java initialize List using Arrays.asList()
List<String>myStringlist = Arrays.asList("this", "is",
"a", "linux cloud tutorial");
// Print the list
System.out.println("List : " + myStringlist);
}
}
The output of this code will be
List : [this,is,a,linux cloud tutorial]
We can also use this method to add items to an ArrayList
, the following code will show how we can use the asList()
method for an ArrayList.
//important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
//java initialize list using instance ArrayList
List<String>myArraylist = new ArrayList ();
Arrays.asList("this tutorial works", "java is fun",
"initialize lists");
// Print the list
System.out.println("List : " + myArraylist);
}
}
The output of the following code is
List : [this tutorial works,java is fun,initialize lists]
Method-3: Using UnmodifiableList
Through this method, a list can be made and elements can be added to it through the collections.unmofiableList()
but this list cannot be then modified.
The following code shows how collections.modifiable()
method works
//important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
// initialize list using collections.unmofiableList method.
List < String > unModifiablelist = Collections.unmodifiableList(Arrays.asList("abc", "def", "ghi"));
// Print the list
System.out.println("List : " + unModifiablelist);
}
}
The output of the following code is
List : [abc, def, ghi]
Understanding List of Lists
Another major concept to grasp in lists is that of the list of lists, this list has a number of lists to it and the code below will demonstrate how we can make a list of lists
It basically works as a 2D list.
// important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
// initialize a list of lists
// initialize a list of lists
List<ArrayList<String>> myListofLists = new ArrayList<>();
// create a list that can be added to the list of lists
// initialize list using instance ArrayList
ArrayList<String>listOne = new ArrayList<String>();
listOne.add("tutorial");
listOne.add("linuxCloud");
// add this list to the list of lists.
myListofLists.add(listOne);
// create another list and add elements to it
ArrayList<String>listTwo = new ArrayList<String>();
listTwo.add("initialize list"); listTwo.add("java tutorial");
// add the list to list of lists
myListofLists.add(listTwo);
// display the contents of list of lists
System.out.println("all lists :");
// print lists
myListofLists.forEach((list) -> { list.forEach((n) -> System.out.print(n + " ")); });
}
}
The output of the following code is :
all lists :
tutorial linuxCloud initialize list java tutorial
Practice code
The code below is for practice, before running it on your ide determine its output and then check its output by copying this code and running it in your ide i.e. Eclipse, Intellij, NetBeans.
//important imports
import java.util.*;
public class Initialize_List {
public static void main(String[] args) {
List<String>myStack = new Stack();
//a string item to add is declared here
String str = "this is a linux cloud tutorial";
//another string item to add is declared here
String str1 = "initialize list";
//add the item, used add function, string is passed as a parameter
myStack.add(str);
myStack.add(str1);
//loop to display output.
for (String exer: myStack) {
System.out.println(exer + " ");
}
List<String>myStringlist = Arrays.asList("this",
"is", "a", "linux cloud tutorial");
// Print the list
System.out.println("List : " + myStringlist);
List<String>unModifiablelist =
Collections.unmodifiableList(Arrays.asList("abc",
"def", "ghi"));
// Print the list
System.out.println("List : " + unModifiablelist);
//initialize a list of lists
List<ArrayList<String>>myListofLists = new ArrayList<>();
//create a list that can be added to the list of lists
//initialize list using instance ArrayList
ArrayList<String>listOne = new ArrayList<String>();
listOne.add("tutorial");
listOne.add("linuxCloud");
//add this list to the list of lists.
myListofLists.add(listOne);
//create another list and add elements to it
ArrayList<String>listTwo = new ArrayList<>();
listTwo.add("initialize list");
listTwo.add("java tutorial");
//add the list to list of lists
myListofLists.add(listTwo);
//display the contents of list of lists
System.out.println("all lists :");
//print lists
myListofLists.forEach((list) -> {
list.forEach((n) -> System.out.print(n + " "));
});
}
}
Conclusion
In this article, we studied how we can initialize lists using different classes like ArrayList, LinkedList, Stack and Vector. After that, we studied how items can be added to a list using different functions and finally we studied how we can make a list of lists.
Further Reading