• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Map object but one that maintains insert/add order.  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the Map object (Map <Integer, Integer>) and it works great as numerous values can be inserted and don't have to be concerned about duplicate objects as it sort of takes care of itself.

What I need is the same behavior as the Map object but the order values are add are stored and retrieved in the same order without having to add an extra parameter and handle it myself.


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote:Using the Map object (Map <Integer, Integer>) and it works great as numerous values can be inserted and don't have to be concerned about duplicate objects as it sort of takes care of itself.

What I need is the same behavior as the Map object but the order values are add are stored and retrieved in the same order without having to add an extra parameter and handle it myself.


How about the LinkedHashMap class?

Henry
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
How about the LinkedHashMap class?

Henry


Just tested this and the only issue is that it maintains the insert order (which is what I was looking to do) but if records happened to be inserted a 2nd time in reverse order than
the original order is maintained.
Currently if 1,2,3 is inserted then 1,2,3 is being returned (which is great), but if afterward 3,2,1 is inserted then 1,2,3 is still being returned when I am hoping it would be 3,2,1.
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote:
Henry Wong wrote:
How about the LinkedHashMap class?

Henry


Just tested this and the only issue is that it maintains the insert order (which is what I was looking to do) but if records happened to be inserted a 2nd time in reverse order than
the original order is maintained.
Currently if 1,2,3 is inserted then 1,2,3 is being returned (which is great), but if afterward 3,2,1 is inserted then 1,2,3 is still being returned when I am hoping it would be 3,2,1.



Just did some further testing and if I simply do a "remove" before any "put" it appears to be working in a way I need as any subsequent inserts are perceived to be the last if the remove is done first. The "remove" doesn't error out even if the record does not exist which is nice as it avoids any additional error/validation handling code.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote: . . . if records happened to be inserted a 2nd time in reverse order than
the original order is maintained.
. . .
That is how the class is supposed to work; I presume you have read its documentation. You would have to consider a different class if you need to record most recent “put” call. Map#remove, put and get run in constant time, List#add runs in (amortised) constant time and List#remove runs in linear time, so you are risking slow execution with remove.
What about a pair of Maps the second recording K↦most recent insertion number? Search for Apache commons bi‑directional Map; that may be useful. Find the Java Tutorials section about Maps and there is an example about counting. You can alter that to count insertions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!