// creating List in JavaList<
String> names=
new ArrayList<
String>();
// initializing List in Javanames.add("Raja");
names.add("Ramesh");
names.add("Ramu");
Set<
String> readOnlySet =
new HashSet<
String>(names);
//Set is not yet read-only you can still add elements into SetreadOnlySet.add("Ravi");
//making Set readonly in Java - no add remove or set operation permittedreadOnlySet =
Collections.unmodifiableSet(readOnlySet);
//trying to add element in read only Set - java.lang.UnSupportedOperationExceptionreadOnlySet.add("You can not add element in read Only Set");
//trying to remove element from read only setreadOnlySet.remove("Ramu");
//you can not remove elements from read only SetMap<
String,
String> agesmap =
new HashMap<
String,
String>();
agesmap.put("raja", "24");
//Map is not read only yet, you can still add entries intoagesmap.put("ramesh", "22");
System.out.println("Map in Java before making read only: " + contries);
//Making Map read only in JavaMap readOnlyMap =
Collections.unmodifiableMap(agesmap);
//you can not put a new entry in read only Map in JavareadOnlyMap.put("ra", "33");
//java.lang.UnSupportedOperation //you can not remove keys from read only Map in JavareadOnlyMap.remove("raja");
//java.lang.UnSupportedOperation