JDK-4349858 : The List Interface should extends Cloneable to ease list cloning
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2000-06-30
  • Updated: 2021-03-03
  • Resolved: 2000-07-14
Related Reports
Relates :  
Relates :  
Description

Name: stC104175			Date: 06/30/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


I request a small feature list enhancement to the abstract List interface of
the Collection Framework.

The problem occurs when one whishes to clone a list through the abstract List
interface.
This is impossible because the List interface doesn't extend Cloneable nor
redefine the clone method public:
  List l= new LinkedList();
  List l2= (List ) l.clone();
produce an error "method clone is protected in Object".

The only way I have found so far is to go to the actual implementation:
  List l2= (List ) ((LinkedList ) l).clone();
This is not only ugly, but also is in a sense against the Collection framework
objectives.

A better alternative would be that List extends Cloneable or "publicize" the
clone method.

Thank you for your attention.
(Review ID: 106736) 
======================================================================

Comments
EVALUATION The preferred method of copying a List is via the "conventional copy constructor": List copy = new ArrayList(original); Further, having List extend Cloneable would be of no help, as Cloneable is broken: it lacks a public clone operation (See RFE 4098033). As a practical matter, it is to late to change any of the "core collection interfaces" (Collections, Set, List, Map, etc.) joshua.bloch@Eng 2000-07-14
14-07-2000