commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew M" <>
Subject RE: [Collections] LinkedMap question
Date Tue, 09 Jun 2009 19:09:08 GMT
It doesn't have an indexOf(Object) method.   I need something like a List
whose indexOf() runs in
constant time instead of iterating looking for the target object.


-----Original Message-----
From: JEFFREY Mark [] 
Sent: Friday, June 05, 2009 11:11 PM
To: Commons Users List
Subject: RE: [Collections] LinkedMap question

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?


 * <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>
 * <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
 * 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
 * is likely to be more expensive, requiring time proportional to its
 * <i>capacity</i>.

-----Original Message-----
From: Andrew M []
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. 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:

  Object o = myCollection.get(index); //fast like an Arraylist
  int index = myCollection.indexOf(object); //fast like a HashMap
  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,
  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() );
	   // HashMap no longer accurate
         this.fireTableRowsDeleted(row, row);

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



To unsubscribe, e-mail:
For additional commands, e-mail:

No virus found in this incoming message.
Checked by AVG -
Version: 8.5.339 / Virus Database: 270.12.50/2150 - Release Date: 06/05/09

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message