Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically <code>unModifiable</code> Collection is a view, So indirectly it could still be 'modified' from some other reference that is modifiable. Also as its just a <strong><em>readonly view</em></strong> of annother collection , When the source collection changes unModifiable Collection will always present with latest values.</p> <p>However <code>immutable</code> Collection can be treated as a <strong><em>readonly copy</em></strong> of another collection and can not be modified. In this case when the source collection changes , immutable Collection do not reflect the changes</p> <p>Here is a testcase to visualise this difference. </p> <pre><code>@Test public void testList() { List&lt;String&gt; modifiableList = new ArrayList&lt;String&gt;(); modifiableList.add("a"); System.out.println("modifiableList:"+modifiableList); System.out.println("--"); //unModifiableList assertEquals(1, modifiableList.size()); List&lt;String&gt; unModifiableList=Collections.unmodifiableList( modifiableList); modifiableList.add("b"); boolean exceptionThrown=false; try { unModifiableList.add("b"); fail("add supported for unModifiableList!!"); } catch (UnsupportedOperationException e) { exceptionThrown=true; System.out.println("unModifiableList.add() not supported"); } assertTrue(exceptionThrown); System.out.println("modifiableList:"+modifiableList); System.out.println("unModifiableList:"+unModifiableList); assertEquals(2, modifiableList.size()); assertEquals(2, unModifiableList.size()); System.out.println("--"); //immutableList List&lt;String&gt; immutableList=Collections.unmodifiableList( new ArrayList&lt;String&gt;(modifiableList)); modifiableList.add("c"); exceptionThrown=false; try { immutableList.add("c"); fail("add supported for immutableList!!"); } catch (UnsupportedOperationException e) { exceptionThrown=true; System.out.println("immutableList.add() not supported"); } assertTrue(exceptionThrown); System.out.println("modifiableList:"+modifiableList); System.out.println("unModifiableList:"+unModifiableList); System.out.println("immutableList:"+immutableList); System.out.println("--"); assertEquals(3, modifiableList.size()); assertEquals(3, unModifiableList.size()); assertEquals(2, immutableList.size()); } </code></pre> <p><strong>Output</strong></p> <pre><code>modifiableList:[a] -- unModifiableList.add() not supported modifiableList:[a, b] unModifiableList:[a, b] -- immutableList.add() not supported modifiableList:[a, b, c] unModifiableList:[a, b, c] immutableList:[a, b] -- </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload