Note that there are some explanatory texts on larger screens.

plurals
  1. POGuava: ImmutableList magic of the copyOf() method
    primarykey
    data
    text
    <p>I would like to feel the 'magic power' of the <code>copyOf()</code> method of Guava <code>guava-libraries</code>. </p> <p>There is small app that I use to check it. </p> <p>Here is the <a href="https://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained" rel="nofollow noreferrer">documentation</a>:</p> <blockquote> <p>The JDK provides <code>Collections.unmodifiableXXX</code> methods, but in our opinion, these can be</p> <ul> <li>unwieldy and verbose; unpleasant to use everywhere you want to make defensive copies</li> <li>unsafe: the returned collections are only truly immutable if nobody holds a reference to the original collection</li> </ul> </blockquote> <p>So, I try to build a model where <code>"someone holds a reference to the original collection"</code>. Thus, working with a copy of collection I should not be worried about changing value on copy. But magic does not work so far (there are two tries: 1. <code>copyOf(collection)</code>, 2. <code>copyOf(iterator)</code>):</p> <pre><code>import com.google.common.collect.ImmutableList; import java.util.LinkedList; import java.util.List; class MyObject { String name; public MyObject(String name) {this.name = name;} @Override public String toString() { return name; } } public class ListUnsafe { List&lt;MyObject&gt; list = new LinkedList&lt;MyObject&gt;(); { list.add(new MyObject("a")); list.add(new MyObject("b")); list.add(new MyObject("c")); } public List&lt;MyObject&gt; getList() { return ImmutableList.copyOf(list); } public List&lt;MyObject&gt; getCopyIterator() { return ImmutableList.copyOf(list.iterator()); } public static void main(String[] args) { ListUnsafe obj = new ListUnsafe(); { MyObject ref = obj.list.get(0); List&lt;MyObject&gt; myList = obj.getList(); MyObject copyObj = myList.get(0); copyObj.name = "new"; System.out.println("ref: " + ref); } obj = new ListUnsafe(); { MyObject ref = obj.list.get(0); List&lt;MyObject&gt; myList = obj.getCopyIterator(); MyObject copyObj = myList.iterator().next(); copyObj.name = "new"; System.out.println("ref: " + ref); } } } </code></pre> <p>The output:</p> <pre><code>ref: new ref: new </code></pre> <p>It means that we changed original data. What we did not want.</p> <h2>Question</h2> <p>Why it does not do copy?<br> How it differs from <code>unmodifiableXXX</code>?</p> <p>There is <a href="https://stackoverflow.com/questions/9517228/guava-immutablelist-copyof-vs-builder">link</a> to similar question:</p> <p>The answer says about <code>copyOf</code>:</p> <ul> <li>(from source) <code>copyOf(Collection)</code> instance doesn't create temporary <code>ArrayList</code> (<code>copyOf(Iterable)</code> and <code>copyOf(Iterator)</code> do so).</li> </ul>
    singulars
    1. This table or related slice is empty.
    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.
 

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