The returned list will be serializable if the specified list is serializable. Similarly, the returned list will implement RandomAccess
if the specified list does.
import java.util.RandomAccess;
import it.unimi.dsi.fastutil.longs.LongArrayList;import it.unimi.dsi.fastutil.longs.LongList;import it.unimi.dsi.fastutil.longs.LongLists;
public class Test{ public static void main(String[] args) { LongList l = new LongArrayList(); System.out.println("Random? " + (l instanceof RandomAccess)); LongList ul = LongLists.unmodifiable(l); System.out.println("Random? " + (ul instanceof RandomAccess)); }}
Random? true
Random? false
java.util.Collections.unmodifiableList() provides the following guarantee:The returned list will be serializable if the specified list is serializable. Similarly, the returned list will implementRandomAccess
if the specified list does.It looks like IntLists.unmodifiableList(), LongLists.unmodifiableList(), etc., do not guarantee that the returned list will implement RandomAccess if the underlying list does. This means that operations, like java.util.Collections.binarySearch(), may be slower than expected if they're used on a fastutil UnmodifiableList.
Thanks! The changes look good.When reviewing the JDK source, I note that they had to add some special handling to support deserializing UnmodifiableRandomAccessLists in pre-1.4 JDKs (prior to the introduction of RandomAccess). Since fastutil UnmodifiableLists are also serializable, the same issue could affect fastutil if someone serializes an unmodifiable random-access list with a new version of fastutil and tries to deserialize it with an older version. However I suspect that this is a very unlikely scenario.