How do you get an immutable collection

This functionality is provided by the Collections class, which is a wrapper implementation using the decorator design pattern.

public class ReadOnlyExample {

public static void main(String args[ ]) {

Set<string> set = new HashSet<string>( );

set.add(“Java”);

set.add(“JEE”);

set.add(“Spring”);

set.add(“Hibernate”);

set = Collections.unmodifiableSet(set);

set.add(“Ajax”);                                           // not allowed.

}

}