Collections in Immutable Class

11 08 2014
  • immutable class in java is not allowing to modify the value of the variables.
  • but in normal immutable class with collections , if we are not using setter method also , we can change the collection values.
  • normally when we use the getter method , we directly return the reference of the collections.
  • here we can change the values for the collection, even though,it is final .

 

public class ImmutableClassCollections {

private final  String name;
private final List arrayList;

public ImmutableClassCollections(String name, List arrayList) {
this.name = name;
this.arrayList = arrayList;
}

public String getName() {
return name;
}

public List getArrayList() {
return arrayList;
}

/**
* @param args
*/
public static void main(String[] args) {
List mylist=new ArrayList<>();
mylist.add(“A”);
mylist.add(“B”);
ImmutableClassCollections example=new ImmutableClassCollections(“Siva”, mylist);

System.out.println(example.getName());

List arrayList2 = example.getArrayList();

for (String string : arrayList2) {
System.out.println(string);
}
System.out.println(“Add C to Array List”);
example.getArrayList().add(“C”);
arrayList2 = (ArrayList) example.getArrayList();
for (String string : arrayList2) {
System.out.println(string);
}

}

}

OUTPUT:

Siva
A
B
Add C to Array List
A
B
C

 

How to Avoid Collection also in Immutable Class:

we can use two approaches in getter method

  1.  create local copy of the collection and return the copy reference only
  2. using collections API-return Collections.unmodifiableXXX(XXX);

 

1) Create  Local Copy:

  • It will create new copy and return that value. so it will not change the original value

public List<String> getArrayList() {
List arrayList1=new ArrayList<>(arrayList);
return arrayList1;
}

OUTPUT:

Siva
A
B
Add C to Array List
A
B

 

Here we are using Collections.unmodifiableList(arrayList);

now we change the return value, it will throw the exception.

public List<String> getArrayList() {
return Collections.unmodifiableList(arrayList);
}

OUTPUT:

Siva
A
B
Add C to Array List
Exception in thread “main” java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
at com.ts.immutable.ImmutableClassCollections.main(ImmutableClassCollections.java:42)

 

unmodifiableList Implementation in Collections Class:

 

/**

* Returns an unmodifiable view of the specified list. This method allows
* modules to provide users with “read-only” access to internal
* lists. Query operations on the returned list “read through” to the
* specified list, and attempts to modify the returned list, whether
* direct or via its iterator, result in an
* <tt>UnsupportedOperationException</tt>.<p>
*
* The returned list will be serializable if the specified list
* is serializable. Similarly, the returned list will implement
* {@link RandomAccess} if the specified list does.
*
* @param list the list for which an unmodifiable view is to be returned.
* @return an unmodifiable view of the specified list.
*/
public static <T> List<T> unmodifiableList(List<? extends T> list) {
return (list instanceof RandomAccess ?
new UnmodifiableRandomAccessList<>(list) :
new UnmodifiableList<>(list));
}








%d bloggers like this: