aspose file tools*
The moose likes Java in General and the fly likes readonly ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "readonly ArrayList" Watch "readonly ArrayList" New topic
Author

readonly ArrayList

Bala Raju Mandala
Ranch Hand

Joined: Sep 21, 2006
Posts: 40
Hi Ranchers,

Recently in a interview the guy asked me these question, i have no idea about these, if anybody knows please tellme.

1. How to make readonly Arraylist?
2. What is Hashing in Java?
3. What pre-cautions we need to take while coding, if we are working on files, to avoid choas and get good performance?

I will be glad if somebody give answers to all these questions. Thank you in advance.

regards,
Bala Raju M.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31050
    
162

Bala,
Take a look at Collections.unmodifiableList and HashMap in the JavaDoc for #1 and #2.

#3 requires some more thought. It could mean making sure to close resources when done or to read from the file in an efficient manner.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 81

ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
vinayak jog wrote: ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);


Throws ClassCastException. And also please avoid raw types in new code.

This should work


Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Bala Raju Mandala wrote:
1. How to make readonly Arraylist?

As well as using Collections.unmodifiableList you will also have to make sure that all other references to the ArrayList instance are removed.


Joanne
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287


Remove references to the ArrayList?
insert this line of code after calling Collections.unmodiableList(List list);
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Harsha Smith wrote:

Remove references to the ArrayList?
insert this line of code after calling Collections.unmodiableList(List list);

That would work. I was just making the point that simply calling Collections.unmodifiableList does not make the ArrayList read-only and I think mentioning that in the interview would probably impress the interviewer a little more.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287

And that makes you getting paid more
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Collections.unmodifiableList(...) indeed returns a view on an existing List that is read-only; if you try to call any of the methods that modify the list on the view, it will throw an exception. But if some other part of the program still has a reference to the original list, then it can still modify the list, as Joanne and others have noted.

To make sure that a list is really immutable, you could use for example ImmutableList from the Google Guava library (an excellent library by the very smart people at Google that I'd highly recommend for any project). The only way to create an ImmutableList is through a Builder object. Once the Builder object has created the list for you, there is no way that you can modify the list - the program never has access to a mutable version of the list.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Jesper de Jong wrote:To make sure that a list is really immutable, you could use for example ImmutableList from the Google Guava library...

In plain vanilla Java the following one-liner will also do the trick:
The ArrayList constructor makes a copy of the original list, reference to which is passed to the unmodifiableList method and not stored anywhere else.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Martin Vajsar wrote:
Jesper de Jong wrote:To make sure that a list is really immutable, you could use for example ImmutableList from the Google Guava library...

In plain vanilla Java the following one-liner will also do the trick:
The ArrayList constructor makes a copy of the original list, reference to which is passed to the unmodifiableList method and not stored anywhere else.


That won't work. Throws ClassCastException. May we should go with guava instead of vanilla.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Harsha Smith wrote:That won't work. Throws ClassCastException. May we should go with guava instead of vanilla.

Ouch. How could I ever wrote something like that?

Anyway, not using guava is not the cause of the problem. Declaring the variable as ArrayList instead of List is. Analogous construct would not compile with guava either - ImmutableList is not a subclass of ArrayList.

Guava does look interesting. Immutable collections would not be the driving force for me, but if I include it for the other interesting classes, the immutables might be useful too. Just a pity that so many interesting functionalities are declared @Beta

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Martin Vajsar wrote:
Jesper de Jong wrote:To make sure that a list is really immutable, you could use for example ImmutableList from the Google Guava library...

In plain vanilla Java the following one-liner will also do the trick:
The ArrayList constructor makes a copy of the original list, reference to which is passed to the unmodifiableList method and not stored anywhere else.

The whole point of my post was to explain that that plain Java one-liner does not ensure that nobody can mutate the list...
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

Jesper de Jong wrote:The whole point of my post was to explain that that plain Java one-liner does not ensure that nobody can mutate the list...

The one-liner I've posted creates a copy of the array list and passes the newly created reference to the unmodifiableList call. The mutable list's reference is inaccessible to anyone, except maybe by reflection, which probably is not a concern here. So this line actually does ensure nobody can mutate the list, regardless of where the original list came from.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Jesper de Jong wrote:To make sure that a list is really immutable, you could use for example ImmutableList

To be slightly pedantic, the question asked for a read only ArrayList, so I'm not sure creating an ImmutableList instance would count.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: readonly ArrayList