Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized generic types  RSS feed

 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Here's my problem. I have the following datastructure:

Now, I need to synchronize this data structure, however, you probably realize, as I quickly learned, some interesting caveats are associated with generic Collections and runtime initializations.
For instance:

This will not work because of "erasure", and inevitably I get the runtime ClassCastException. So, my question is, how in the world do I synchronize this LinkedList and still use generics?
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronizedList returns List, not LinkedList. This has nothing to do with generics.

Type of data variable should be List<Data[][]>, not LinkedList<Data[][]>, then it will work (without explicit cast).


It is good practice to use interfaces (List, Set, Map, Collection, ...) instead of concrete classes (LinkedList, HashMap, TreeSet, ...) as types for your variables, parameters and return types.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Vlado. You are right in that this has nothing to do with generics. It was a mis-diagnosis on my part. Thanks for the tip, though. It brought me to the following solution:


I was pulling my hair out trying to do an explicit cast, all the while I could have simply passed the List into the constructor of the LinkedList! Haha, always good to get someone else's input!

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

Your latest code copies the synchronized list's elements into a new unsynchronized list. I don't think that's what you want!

Look more carefully at what Vlado showed you. The problem is just that the List returned by synchronizedList is not a LinkedList<String>, but rather just a List<String>, and the only change you need to make to your original example is to declare it as such.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, I didn't even realize this. Not as easy as I thought, thanks Ernest. I'm resistent to declaring this structure as a List because the List interface does not define addFirst() and removeLast(), and these are methods that I've called in my code.

I can roll my own implementations of these methods, but is that really what I want to do?

Thanks for guiding me through this guys. I suppose my question now is, knowing I would like to implement LinkedList class methods, how do I proceed to synchronize the data structure using the Collections.synchronizeList(...)?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you not simply cast it to a LinkedList after you get the List back?



*Edited to get rid of smilies. Stupid smilies.
[ November 11, 2005: Message edited by: Ken Blair ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
Can you not simply cast it to a LinkedList after you get the List back?


No, that's what he started with. The problem is that the return value of synchronizedList() is not a LinkedList.

Jeff, the only choice I'm aware of for you is to make a SynchronizedLinkedList wrapper that wraps a LinkedList, forwards every method to that member LinkedList, except that all the methods are synchronized. A decent IDE should be able to generate this baby for you without any effort on your part.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ernest. Now that I know I'm not missing anything, I can proceed in developing the wrapper or some similar solution. Thanks again.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!