This discussion is archived
10 Replies Latest reply: Jun 7, 2012 10:05 PM by 941867 RSS

how do i make an ArrayList read only?

Murray9654 Newbie
Currently Being Moderated
How can i make an ArrayList read only without extending it.
  • 1. Re: how do i make an ArrayList read only?
    sabre150 Expert
    Currently Being Moderated
    You can make a java.util.List unmodifiable using static method unmodifiableList() in java.util.Collections. An ArrayList isA List.
  • 2. Re: how do i make an ArrayList read only?
    aksarben Journeyer
    Currently Being Moderated
    Not understanding the context of your question, it's hard to know what you want to achieve. If you merely want to return a list to a caller and not worry about whether he modifies it, you could always clone the list and return the clone instead of your private copy. That's not the same as read only, so it may or may not be what you want.
  • 3. Re: how do i make an ArrayList read only?
    796440 Guru
    Currently Being Moderated
    Also, note that making the List read-only doesn't make the objects it contains read-only.
  • 4. Re: how do i make an ArrayList read only?
    939520 Explorer
    Currently Being Moderated
    I see that java.util.Collections.unmodifiableList(x1); will make it read only (with the limitations mentioned by others in this post) by throwing an exception if someone called x1.add(). However, I don't think its a good idea to let List expose the add() function to the caller if it always throws an exception. Better to not expose the add() method(s) at all.

    One solution is to return a deep clone of the Arraylist (allowing them to call its add() function). However, this has performance issues if your dealing with a lot of list objects. A deep clone of the list will protect against modifying of the objects it contains.

    Another possible solution is to add a comment in the function's javadoc, asking them not to alter the data in the returned data object and why (what the affects would be).
  • 5. Re: how do i make an ArrayList read only?
    796440 Guru
    Currently Being Moderated
    936517 wrote:
    I see that java.util.Collections.unmodifiableList(x1); will make it read only (with the limitations mentioned by others in this post) by throwing an exception if someone called x1.add(). However, I don't think its a good idea to let List expose the add() function to the caller if it always throws an exception. Better to not expose the add() method(s) at all.
    Then it won't be an ArrayList, a List, or a Collection. You'll have to define your own type, completely outside the Collections Framework in the Core API, or find a thirdparty library that has such a class.
    Another possible solution is to add a comment in the function's javadoc, asking them not to alter the data in the returned data object and why (what the affects would be).
    In which case we can just use Collections.unmodifiableList(). It's all well and good to ask them not to call the method, but better still to enforce it. If they ignore our docs, it's better to throw an exception than to corrupt the data.
  • 6. Re: how do i make an ArrayList read only?
    941867 Newbie
    Currently Being Moderated
    Create ArrayList by Arrays.asList() method it returns unmodifiable List
  • 7. Re: how do i make an ArrayList read only?
    sabre150 Expert
    Currently Being Moderated
    938864 wrote:
    Create ArrayList by Arrays.asList() method it returns unmodifiable List
    Not according to http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList and if the OP starts with an ArrayList he will have to first convert it to an array. So what is wrong with Collections.unmodifiableList() ?
  • 8. Re: how do i make an ArrayList read only?
    941867 Newbie
    Currently Being Moderated
    sabre150 wrote:
    938864 wrote:
    Create ArrayList by Arrays.asList() method it returns unmodifiable List
    Not according to http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList and if the OP starts with an ArrayList he will have to first convert it to an array. So what is wrong with Collections.unmodifiableList() ?
    well, As far I know you can not add any element in the ArrayList returned by Arrays.asList and its best way to create read only arraylist if you know the contents in front but I don't disagree your point that its not useful if you want to make an existing ArrayList readOnly for that there is other method mentioned in earlier response i.e. Collections.unmodifiableList(). So nothing wrong with that. It just best upon situation.
  • 9. Re: how do i make an ArrayList read only?
    EJP Guru
    Currently Being Moderated
    It is fixed size but you can use get() and set() on it.
  • 10. Re: how do i make an ArrayList read only?
    941867 Newbie
    Currently Being Moderated
    Got your point mate. Since you can change the value of existing element, this is really not a readonly List rather a Fixed size List. Thanks

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points