• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FIFO Linked Map

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a requirement, I want a map which will preserve insertion order and give me access to remove the first inserted element.

Here is an example:



How should I do this? I thought of using queue, but I need both the key and value. I also need access to individual entries to remove then if necessary.

With Map, I loose the insertion order.

I am thinking maybe some way of recursion might solve the problem, but not getting any idea.


Thanks

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LinkedHashMap is a Map that preserves insertion order.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:LinkedHashMap is a Map that preserves insertion order.



Agreed. If I iterate over the map, the values will be printed in insertion order. But my problem is that everytime I have to remove the last element. I can either use the iterator object and keep removing elements whenever I use them or use recursion and keep branching till my initial value = 0.

Is there any other alternative, if not I will one of these two ways to implement it. It would have been great to have a map which has properties similar to queues, i.e. queue.poll(), which will do the work which I want.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s ravi chandran wrote:I have a requirement, I want a map which will preserve insertion order and give me access to remove the first inserted element.


s ravi chandran wrote:But my problem is that everytime I have to remove the last element.


Which is it, the last element or the first inserted (so the first) element?

Also, does it really need to be a Map, or do you just need a key-value pair? Because if it's the latter, you can use any structure (List, Queue, ...) with something that is a key-value pair. You can simply use Map.Entry for that, with two possible implementations given in JSE itself: AbstractMap.SimpleEntry and AbstractMap.SimpleImmutableEntry.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article might interest you http://techtipsjava.blogspot.co.uk/2013/05/queue-map-hybrid-creating-data.html
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:
Which is it, the last element or the first inserted (so the first) element?



Sorry for the ambiguous statement.

What I meant was to remove the first inserted element always. other way to look at it was the last element ( in queue structure).

I will check these links. I just need to store key value pair, with an option to access any element based on key if required. this is for handling occasional remove element operations.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could download the Apache Commons Collections package and use ListOrderedMap see API Docs
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Or you could download the Apache Commons Collections package and use ListOrderedMap see API Docs



Thanks.  

This one has everything I need. Will use this one in my implementation.
 
What's that smell? Hey, sniff this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic