The fill() method in Java is a utility method that replaces all the elements of a specified list with a specified element. This method is part of the java.util.Collections class and provides a convenient way to update all elements of a list to a single, specified value.
Table of Contents
- Introduction
fill()Method Syntax- Examples
- Basic Usage of
fill() - Using
fill()with Custom Classes
- Basic Usage of
- Real-World Use Case
- Conclusion
Introduction
The Collections.fill() method is used to overwrite every element in a list with the specified element. This is useful when you need to reset or initialize all elements of a list to the same value. The method modifies the list in place, meaning that the list itself is changed.
This method works with any List implementation and is especially useful when you need to ensure that a list contains only a single repeated element.
fill() Method Syntax
The syntax for the fill() method is as follows:
public static <T> void fill(List<? super T> list, T obj)
Parameters:
list: The list to be filled with the specified element.obj: The element with which to fill the list.
Returns:
- This method does not return a value.
Throws:
UnsupportedOperationExceptionif the list does not support thesetoperation.NullPointerExceptionif the list is null.
Examples
Basic Usage of fill()
The following example demonstrates how to use the fill() method to replace all elements in a list with a specified value.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FillExample {
public static void main(String[] args) {
// Create a list with initial elements
List<String> fruits = new ArrayList<>();
Collections.addAll(fruits, "Apple", "Banana", "Cherry");
// Display the original list
System.out.println("Original List: " + fruits);
// Use the fill() method to replace all elements with "Mango"
Collections.fill(fruits, "Mango");
// Display the modified list
System.out.println("Modified List: " + fruits);
}
}
Output:
Original List: [Apple, Banana, Cherry]
Modified List: [Mango, Mango, Mango]
Using fill() with Custom Classes
You can also use the fill() method with lists containing instances of custom classes.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student {
String name;
Student(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public class CustomFillExample {
public static void main(String[] args) {
// Create a list of students
List<Student> students = new ArrayList<>();
students.add(new Student("Amit"));
students.add(new Student("Neha"));
students.add(new Student("Raj"));
// Display the original list
System.out.println("Original Student List: " + students);
// Use the fill() method to replace all elements with a new student
Student defaultStudent = new Student("Default");
Collections.fill(students, defaultStudent);
// Display the modified list
System.out.println("Modified Student List: " + students);
}
}
Output:
Original Student List: [Amit, Neha, Raj]
Modified Student List: [Default, Default, Default]
Explanation:
-
Original List: The initial list contains unique instances of the
Studentclass with different names. -
Modified List: After using the
fill()method, all elements in the list are replaced with a newStudentinstance named "Default".
Real-World Use Case
Initializing or Resetting List Elements
In real-world applications, the fill() method can be used to initialize or reset the elements of a list to a specific value. This is particularly useful in scenarios such as resetting game board states, initializing default values, or clearing sensitive data.
Example
Imagine a scenario where you have a list representing the state of a game board. You can use the fill() method to reset the board to its initial state.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Cell {
String state;
Cell(String state) {
this.state = state;
}
@Override
public String toString() {
return state;
}
}
public class GameBoardExample {
public static void main(String[] args) {
// Create a list representing a game board with initial states
List<Cell> board = new ArrayList<>();
board.add(new Cell("X"));
board.add(new Cell("O"));
board.add(new Cell("X"));
// Display the original board state
System.out.println("Original Board State: " + board);
// Reset the board to its initial empty state
Cell emptyCell = new Cell("-");
Collections.fill(board, emptyCell);
// Display the reset board state
System.out.println("Reset Board State: " + board);
}
}
Output:
Original Board State: [X, O, X]
Reset Board State: [-, -, -]
Explanation:
-
Original Board State: The list contains cells with initial states representing a game board.
-
Reset Board State: After using the
fill()method, all cells are reset to an empty state represented by"-".
Conclusion
The Collections.fill() method is a powerful utility for replacing all elements of a list with a specified value in Java. By providing a simple way to update or reset list contents, it enhances the flexibility and readability of your code. This method is particularly valuable in scenarios where you need to initialize or reset list elements to a consistent state, improving the robustness and maintainability of your Java applications.