commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "JEFFREY Mark" <Mark.JEFF...@post.be>
Subject RE: [Collections] LinkedMap question
Date Sat, 06 Jun 2009 04:10:34 GMT

Hi Andrew,

Have you tried java.util.LinkedHashMap?
It retains insertion order and remove() seems to be handled correctly (from code inspection),
is this what you want?

cheers,
Mark


 * <p>This class provides all of the optional <tt>Map</tt> operations, and
 * permits null elements.  Like <tt>HashMap</tt>, it provides constant-time
 * performance for the basic operations (<tt>add</tt>, <tt>contains</tt>
and
 * <tt>remove</tt>), assuming the hash function disperses elements
 * properly among the buckets.  Performance is likely to be just slightly
 * below that of <tt>HashMap</tt>, due to the added expense of maintaining the
 * linked list, with one exception: Iteration over the collection-views
 * of a <tt>LinkedHashMap</tt> requires time proportional to the <i>size</i>
 * of the map, regardless of its capacity.  Iteration over a <tt>HashMap</tt>
 * is likely to be more expensive, requiring time proportional to its
 * <i>capacity</i>.


-----Original Message-----
From: Andrew M [mailto:andrew@oc384.net]
Sent: Sat 06/06/2009 00:20
To: 'Commons Users List'
Subject: RE: Collections LinkedMap question
 
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