- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace All Elements Of ArrayList with with Java Collections
In order to replace all elements of ArrayList with Java Collections, we use the Collections.fill() method. The static void fill(List list, Object element) method replaces all elements in the list with the specified element in the argument.
Declaration −The java.util.Collections.fill() method is declared as follows −
public static <T> void fill(List<? super T> list, T obj)
where obj acts as a filler element to the List list.
Let us see a program to replace all elements of ArrayList with Java collections −
Example
import java.util.*; public class Example { public static void main (String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(7); Collections.fill(list,98); System.out.println(list); } }
Output
[98, 98, 98]
- Related Articles
- Replace all occurrences of specified element of ArrayList with Java Collections
- Reverse order of all elements of ArrayList with Java Collections
- Shuffle elements of ArrayList with Java Collections
- Swap elements of ArrayList with Java collections
- Copy Elements of One ArrayList to Another ArrayList with Java Collections Class
- Find maximum element of ArrayList with Java Collections
- Find minimum element of ArrayList with Java Collections
- Get Enumeration over ArrayList with Java Collections
- Perform Binary Search on ArrayList with Java Collections
- Replace all the elements of a Vector with Collections.fill() in Java
- Sort ArrayList in Descending order using Comparator with Java Collections
- Replace all words with another string with Java Regular Expressions
- How to remove all elements of ArrayList in Java?
- Java Lambda Expression with Collections
- Remove all elements from the ArrayList in Java

Advertisements