Java Vector is a legacy class. And it is significantly faster in comparison to a list obtained through Collections.synchronizedList().
Vector has loads of legacy operations and hence the manipulations in Vector needs to be done through the List interface, otherwise you won't be able to replace the implementation at a later time.
Arrays.asList() is better choice if the list is of fixed size and any kind of size mutation of the collection results in UnsupportedOperationException. The underlying array is updated whenever the list is updated (or vice-versa), but the array reference isn't retained.
Collections.nCopies() is another convenient mini-implementation which can be useful in two ways -
Vector has loads of legacy operations and hence the manipulations in Vector needs to be done through the List interface, otherwise you won't be able to replace the implementation at a later time.
Arrays.asList() is better choice if the list is of fixed size and any kind of size mutation of the collection results in UnsupportedOperationException. The underlying array is updated whenever the list is updated (or vice-versa), but the array reference isn't retained.
Collections.nCopies() is another convenient mini-implementation which can be useful in two ways -
- initialize a newly created list with n null values (need not be only null values) - new ArrayList
(Collections.nCopies(1000, (Type)null) - grow an existing list - lovablePets.addAll(Collections.nCopies(69, "fruit bat"))
Collections.singleton()/Collections.singletonList()/Collections.singletonMap() method returns an immutable version of the specified collection containing specified elements. It is useful for removing all occurrences of the specified elements from a collection.
Collections.emptySet()/Collections.emptyList()/Collections.emptyMap() returns empty collections which can be used as parameters to the methods where a collection is expected, but none is ready to be used.
Collections.emptySet()/Collections.emptyList()/Collections.emptyMap() returns empty collections which can be used as parameters to the methods where a collection is expected, but none is ready to be used.
Comments