Java Problems with Solutions
Find Duration in Hours and Minutes Between Two Dates in Java
Sort a Java List Alphabetically First and then by Numbers
Encryption and Decryption of Strings in Java
Generate a Random String of Specific Length in Java
Split a String by Comma and Space in Java
Remove Duplicates from a Java List
Converting HashSet to List in Java
Calculate Days Between Two Dates in Java
Round a Number to N decimal Places in Java
Java Run Multiple Tasks Simultaneously
Filling Values in Editable PDF Forms in Java
Create ICS Calendar File in Java/Spring Boot
Convert LocalDateTime to Milliseconds in Java
Convert String DateTime To LocalDateTime in Java
Convert LocalDateTime to String in Java
Convert JSON Array to List of Long Type in Java
Convert a List to an Array in Java
Convert an Array to a List in Java
Convert POJO Class Object to JSON String in Java
Convert JSON String to POJO Class Object in Java
How to Add Data to a List in Java
How to avoid Deadlock in Java with Example
Counting Unique Numbers in a List of Integers
Extract Created and Modified Datetime of Files in Java
Remove Matching Items from List Ignoring Case
Java Solved Programs and Problems with Solutions
Java LocalDateTime Formatting
Merge Sets and Convert to List in Java
Working with CSV files in Java: Read and Write Data with Ease
Resolving Java Type Mismatch Error: Cannot Convert from List<int[]> to List<Integer>
Validating Email with Regex in Java
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:
- Create a new instance of the list with the desired data type. For example:
- 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:
ListmyList = new ArrayList<>();
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 ListmyList = 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 ListmyList = 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 ListmyList = 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 ------------------------