You're right that it's a waste of lines, at least if the method fits in your head properly. For larger methods (and the ensuing debate about method sizes) it may help, just as making local variables final can help.
On Wed, Mar 30, 2011 at 10:03 AM, Reinier Zwitserloot <[email protected]>wrote: > On Wednesday, March 30, 2011 5:18:12 AM UTC+2, Alexey wrote: >> >> There's very little memory or performance overhead. There's no deep >> copy. The returned collection is a proxy to the underlying one, insuring >> immutability. >> > > This is not true; ImmutableList.copyOf() actually makes a copy. Meaning, > it's truly immutable (well, the refs are; you can still change elements > contained in such a list): If you change the source list, then the immutable > copy you just made will not change. Hence the wording "copyOf". You're > thinking of Collections.unmodifiableList(), which indeed does NOT make any > copies and just creates a view-only proxy list. But such a list is not > immutable if someone changes the source material. Hence the naming > "unmodifiable". > > For what its worth: If its method-internal then no, this is a waste of > lines. I wouldn't worry about CPU load unless your profiler tells you you > should. Also, if you need to go back and modify the original collection in a > loop of some sort, making a copy is perfectly acceptable if trying to work > with a listIterator is going to create uglier code. > > -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en. > -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
