commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew M" <and...@oc384.net>
Subject RE: Collections LinkedMap question
Date Fri, 05 Jun 2009 22:20:53 GMT
I just tested the speed of 

Int x = myLinkedMap.indexOf(someObject);

It's running in linear time. A 10x increase in map size results in a 10x
increase in indexOf() lookup time.
org.apache.commons.collections.map.ListOrderedMap is the same way so neither
of these work for me.   I need something like a List whose indexOf() runs in
constant time.   

I need methods like:

  myCollection.add(object); 
  Object o = myCollection.get(index); //fast like an Arraylist
  int index = myCollection.indexOf(object); //fast like a HashMap
Object->Index
  myCollection.remove(object); //index of every Object right of the deleted
object shifts left by one per List interface

I had been using both a Vector and HashMap in a TableModel of Order objects
as shown below.  That works fine until I want to delete items. Then the
HashMap of OrderId->Row is no longer correct above the row deleted.  Surely
someone has an elegant solution for stuff like this? 

  HashMap<Integer, Integer> orderIdToRowMap = new HashMap<Integer,
Integer>();
  Vector<Order> displayedVector = new Vector<Order>();

   public void entryInserted(Order o) {
      synchronized (displayedVector) {
         int row = displayedVector.size();
         orderIdToRowMap.put(o.getID(), row);
         displayedVector.add(row, o);
         fireTableRowsInserted(row, row);
      }
   }
 
   public void entryUpdated(Order o) {
      synchronized (displayedVector) {
         int row = orderIdToRowMap.get(o.getID());
         displayedVector.set(row, o);
         fireTableRowsUpdated(row, row);
      }
   }
 
   public void entryDeleted(Order o) {
      synchronized (displayedVector) {
         int row = orderIdToRowMap.get(o.getID());
         orderIdToRowMap.remove( o.getID() );
         displayedVector.remove(row); 
	   // HashMap no longer accurate
         this.fireTableRowsDeleted(row, row);
       }
   }

   public Order getDisplayedOrder(int row) {
      synchronized (displayedVector) {
           return displayedVector.get(row);
      }
   }


Suggestions?

Thanks!
Andrew




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message