How to Add Data to a List in Java

  • Last updated Apr 25, 2024

A List is a data structure that allows to store collections of data in a single object.

To add data to a list in Java, you can follow these steps:

  1. Create a new instance of the list with the desired data type. For example:

  2. List myList = new ArrayList<>();

  3. Use the add() method to add elements to the list. Provide the data you want to add as an argument to the add() method. For example:

  4. myList.add("Sunflower");
    myList.add("Lily");
    myList.add("Rose");
    myList.add("Marigold");


Example 1: Adding String values to a List in Java

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

public class ListExample {

   public static void main(String [] args) {

      //creating a list object for storing string values
      List myList = new ArrayList<>();

      //Adding string values into list
      myList.add("Red");
      myList.add("Blue");
      myList.add("Green");
      myList.add("Yellow");
      myList.add("Orange");
      myList.add("Pink");
      myList.add("Purple");
      myList.add("White");
      myList.add("Black");

      //Printing list elements in console
      System.out.println(myList);
   }

}

The output of the above code is as follows:

[Red, Blue, Green, Yellow, Orange, Pink, Purple, White, Black]


Example 2: Adding Integer values to a List in Java

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

public class ListExample {

   public static void main(String [] args) {

      // Creating a list object for storing integer values
      List myList = new ArrayList<>();

      // Adding int values into list
      myList.add(1);
      myList.add(2);
      myList.add(3);
      myList.add(4);
      myList.add(5);
      myList.add(6);
      myList.add(7);
      myList.add(8);
      myList.add(9);

      //printing list in console
      System.out.println(myList);
   }
}

The output of the above code is as follows:

[1, 2, 3, 4, 5, 6, 7, 8, 9]


Example 3: Adding values of Different Data Types to a List in Java

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

public class ListExample {

   public static void main(String [] args) {

      // Creating a list object for storing integer values
      List myList = new ArrayList<>();

      // Adding data of different types into list
      myList.add("Red");
      myList.add(100);
      myList.add("Green");
      myList.add(552);
      myList.add(true);
      myList.add(false);
      myList.add(10.23);
      
      //printing list in console
      System.out.println(myList);
   }
}

The output of the above code is as follows:

[Red, 100, Green, 552, true, false, 10.23]


Example 4: Adding Class Objects to a List in Java

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

//getter setter class
class Student {

   private int id;
   private String firstName;
   private String lastName;
  
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   public String getLastName() {
      return lastName;
   }

   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
	
}

public class ListExample {

   public static void main(String[] args) {

   // Creating a new instance of a List
   List<Student> myList = new ArrayList<>();

   // Creating student object 1
   Student student1 = new Student();
   student1.setId(1);
   student1.setFirstName("John");
   student1.setLastName("A");
   
   //adding first object to the list
   myList.add(student1);

  // Creating student object 2
   Student student2 = new Student();
   student2.setId(2);
   student2.setFirstName("Jane");
   student2.setLastName("B");
   
   //adding second object to the list
   myList.add(student2);
		
   // Iterating using foreach loop
   myList.forEach(student -> {
      System.out.println(student.getFirstName());
      System.out.println(student.getLastName());
      System.out.println(student.getId());
      System.out.println(student.isActive());
      System.out.println("------------------------");
   });
   }

}

The output of the above code is as follows:

1
John
A
------------------------
2
Jane
B
------------------------