

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Synchronized List from ArrayList in java
In order to get a synchronized list from an ArrayList, we use the synchronizedList(List <T>) method in Java. The Collections.synchronizedList(List <T>) method accepts the ArrayList as an argument and returns a thread safe list.
Declaration −The Collections.synchronizedList(List <T>) method is declared as follows −
public static List <T> synchronizedList(List <T> list)
Let us see a program to get a synchronized List from ArrayList −
Example
import java.util.*; public class Example { public static void main (String[] args) { List<String> list = new ArrayList<String>(); list.add("Hello"); list.add("Hi"); list.add("World"); list = Collections.synchronizedList(list); synchronized(list) { Iterator itr = list.iterator(); while (itr.hasNext()) System.out.print(itr.next()+" "); } } }
Output
Hello Hi World
- Related Questions & Answers
- Get Synchronized Set from HashSet in Java
- Difference between Synchronized ArrayList and CopyOnWriteArrayList in Java
- Check if ArrayList is Synchronized (thread safe) in C#
- Creating a synchronized wrapper for the ArrayList in C#
- synchronized Keyword in Java
- How to get first and last elements from ArrayList in Java?
- Display Sub-List of ArrayList in Java
- Difference Between List and ArrayList in Java
- Get Enumeration over ArrayList with Java Collections
- Retrieve an element from ArrayList in Java
- Get the size of an ArrayList in Java
- How to use Synchronized list in android listview?
- Java Program to display sub-list of ArrayList
- Get the location of an element in Java ArrayList
- Remove duplicate items from an ArrayList in Java
Advertisements