The example aims to get a synchronized list from ArrayList by synchronizedList method in Java.
Source Code
package com.beginner.examples;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class GetSynchronizedListFromArrayList {
public static void main(String[] args) {
//create ArrayList.
ArrayList arrayList = new ArrayList();
/*
Java ArrayList is NOT synchronized. To get synchronized Map from
TreeMap useing the method synchronizedList() of Collections.
*/
List list = Collections.synchronizedList(arrayList);
list.add("Hello");
list.add("World");
//Use Iterator to output
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next().toString());
}
}
}
Output:
HelloWorld
References
Imported packages in Java documentation: