• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

readonly ArrayList

 
Ranch Hand
Posts: 40
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 40801
829
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 83
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList al = new ArrayList();
al.add("string");

al =(ArrayList)Collections.unmodifiableCollection(al);
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Remove references to the ArrayList?
insert this line of code after calling Collections.unmodiableList(List list);
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And that makes you getting paid more
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic