DEVFYI - Developer Resource - FYI

How do you create a read-only collection?

Java Interview Questions and Answers (part 1)


(Continued from previous question...)

147. How do you create a read-only collection?

The Collections class has six methods to help out here:
1. unmodifiableCollection(Collection c)
2. unmodifiableList(List list)
3. unmodifiableMap(Map m)
4. unmodifiableSet(Set s)
5. unmodifiableSortedMap(SortedMap m)
6. unmodifiableSortedSet(SortedSet s)
If you get an Iterator from one of these unmodifiable collections, when you call remove(), it will throw an UnsupportedOperationException.

(Continued on next question...)

Other Interview Questions