• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fill the key in hashmap after the key's value is deleted  RSS feed

 
dev goku
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hava a hashmap,
i want to delete a Key's value and fill the value by the next key's value automatically,
and remove the last unused key automatically,is this possible ? or is there any work around ?

 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want the HashMap to look like once you do that remove call?

Also, what does "fill the value by the next key's value automatically" mean?  What is the "next value"?  HashMap's have no ordering.
 
dev goku
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use it to get data for my recyclerview by serialToId Hashmap ,after i delete a item in RV ,I have to delete the data hashmap also by the serial key ,but the position in Rv is identified by serial No.So, I only have to delete the value by the serial key and not the key itself,and want the values move to the above positions..

HashMap's have no ordering.


Is there any way ?
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really don't follow what you're trying to achieve here. Perhaps another example might help?
 
dev goku
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can delete map values by keys,What i am hoping to get is that when i delete a value i want the(some ways to do ordering) following values to move up and link with those keys.

 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this?


{1:"a",2:"b",3:"c"}


{1:"a",2:"c"}
 
dev goku
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it looks to me like what you're actually doing is removing the next key (assuming an ordered Map, like a TreeMap) and setting the given key's value to that value.
Do all the other keys move up as well?

{1:"a",2:"b",3:"c",4:"d"}

becomes (on a remove(2)):

{1:"a",2:"c",3:"d"}
?

That would involve shuffling the whole lot up.

Is there a fixed sequence of keys by the way?
I'm wondering if this could be done using something other than a Map.
 
Campbell Ritchie
Marshal
Posts: 55758
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't think of an easy way to do that with a Map. I never thought I would say this, but maybe parallel Lists might work. Make sure to keep the List well hidden inside your implementation.
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're looking at here is maintaining an index for a list of values. A HashMap is not the right thing for this as you're having to maintain the index yourself and shuffle values onto different keys every time you remove an item. Difficult to do, inefficient, and prone to errors.

I'd recommend using something like a List instead as it's ordered and maintains its own index. A List has methods that allow you to get and remove items by index.

 
Campbell Ritchie
Marshal
Posts: 55758
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A plain simple List will do nicely as long as the indices are consecutive. A List usually has its first element at index 0, but you can easily subtract 1 for 1‑based indices.
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know of any List implementations where the indices are non-consecutive?
 
Campbell Ritchie
Marshal
Posts: 55758
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant the indices the user wants; I meant that you can't use a single List if the keys shown in the first post ever cease to be consecutive.
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see what you mean now. We'll have to wait for the OP to come back for the answer to that one.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the keys are always sorted you can actually do this with NavigableMap:

This operates in quadratic time. If you need linear time, you need to iterate through the entry set yourself:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!