File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Guaranteed ordering of a collection / collections backed table Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Guaranteed ordering of a collection / collections backed table" Watch "Guaranteed ordering of a collection / collections backed table" New topic
Author

Guaranteed ordering of a collection / collections backed table

Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6035
I am trying to do the following. I want to back a table with a collection. Specifically, I'm thinking of using Hashtable (although I'm open to most any collection). I choose Hashtable, because of the following constaints:
1) The number of rows will vary with time. (It might be fast enough to do it with arrays, since I'm not sure how fast things will vary, but I would rather not risk it if I can avoid it.)
2) I will regularly have to look up specific rows to update them. Each row has a unique long associated with it. I index each row in the Hashtable off of the Long (wrapper) class. This allows O(1) searching so I can quickly update the rows--which will occur very rapidly.
The catch is with Hashtables, I get no ordering guarantees on the iterator. I can't guarantee that this row will always come before that row. Or can I? The sorted collections have iterators which have ordered iterators, right? Of course, these are log(N) search time, becaue they are tree based.
Any ideas how to back a table with a collection? (Maybe a SortedSet or some combination of SortedSet and HashMap.)
--Mark
[ February 03, 2003: Message edited by: Mark Herschberg ]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Mark,
A HashMap does not have an ordering guarantee, and when the array backing the map is resized the order in which keys are iterated through will change.
  • You could use a SortedMap (TreeMap) but would pay the price of O(log N) performance.
  • Depending on what type of ordering you want, a JDK 1.4 LinkedHashMap gives you a guaranteed ordering without the cost. The order is basically insertion order.
  • Finally, if the above doesn't suit you, you can implement your own data structure backed by both a HashMap and a SortedSet. Retrieving and updating can be O(1) but adding and removing are O(log N). Two approaches worth exploring are a SortedSet of keys and a SortedSet of Map.Entry objects.
  • - Peter
    [ February 04, 2003: Message edited by: Peter den Haan ]
    Mark Herschberg
    Sheriff

    Joined: Dec 04, 2000
    Posts: 6035
  • You could use a SortedMap (TreeMap) but would pay the price of O(log N) performance.

  • I'm willing to pay this penalty, but I need to be able to do a get(i) access on it, and I don't think either of those can do it. Do you have any more information on how to do this?
  • Depending on what type of ordering you want, a JDK 1.4 LinkedHashMap gives you a guaranteed ordering without the cost. The order is basically insertion order.

  • I hadn't seent his class before. It's what I want, but again I can't do a get(i).
  • Finally, if the above doesn't suit you, you can implement your own data structure backed by both a HashMap and a SortedSet. Retrieving and updating can be O(1) but adding and removing are O(log N). Two approaches worth exploring are a SortedSet of keys and a SortedSet of Map.Entry objects.

  • Yeah, I this is what I had been doing--something like it anyway. I guess I'll just have to bite the bullet.
    --Mark
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Originally posted by Mark Herschberg:
    [...] I need to be able to do a get(i) access on it [...]
    Is there any way that can be revised? -- you can iterate, you can access via your Long id; is there a way around having get(i) access as well?
    - Peter
    Mark Herschberg
    Sheriff

    Joined: Dec 04, 2000
    Posts: 6035
    Originally posted by Peter den Haan:
    Is there any way that can be revised? -- you can iterate, you can access via your Long id; is there a way around having get(i) access as well?

    Well, I want this data structure to back a table. the table model must support get(row, col). The col comes from mapping the fields of the objects into columns. The row, however, comes from its position in the data set.
    I guess I could convert the collection into an array, and whenver I structurally modify the collction, reproduce the array, but that seems highly inefficent.
    Any other ideas?
    --Mark
    Pete Harris
    Ranch Hand

    Joined: Feb 05, 2003
    Posts: 39
    Mark,
    As I understand it (and please correct me if I'm wrong) You want a list that can be accessed by constant order iteration, a row number or a long identifier. If this is the case, there is no single class in the collections framework that will work for you. My best advice would be either revise the requirements or write your own storage class that uses a HashMap to store the data keyed on the Long identifier, and an ArrayList to map those long identifiers to row numbers.
    Adrian Yan
    Ranch Hand

    Joined: Oct 02, 2000
    Posts: 688
    Mark: Why can't you use AbstractTableModel with a Comparator on it? I know that AbstractTableModel is most often used by JTable. However, I did a project awhile back that does similar to what you want, and AbstractTableModel worked beautifully.
    Mark Herschberg
    Sheriff

    Joined: Dec 04, 2000
    Posts: 6035
    Originally posted by Adrian Yan:
    Mark: Why can't you use AbstractTableModel with a Comparator on it? I know that AbstractTableModel is most often used by JTable. However, I did a project awhile back that does similar to what you want, and AbstractTableModel worked beautifully.

    Comparing on AbstractTableModels? I don't want models comapred, I want their data compared.

    Originally posted by Pete Harris:
    As I understand it (and please correct me if I'm wrong) You want a list that can be accessed by constant order iteration, a row number or a long identifier. If this is the case, there is no single class in the collections framework that will work for you.

    Yeah, I ended up creating my own class...
     
     
    subject: Guaranteed ordering of a collection / collections backed table
     
    Threads others viewed
    Is this on the exam ?
    Trouble with model for calculating and adding table values dependent on multiple rows
    is there something similar to c:forEach in JSF
    Reading a huge file and dumping in DB
    Is there a way to get an absolute row id for a JTable model
    WebSphere development made easy
    without the weight of IBM tools
    http://www.myeclipseide.com