This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Create a copy of an hashmap within an array list

 
Greenhorn
Posts: 1
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to change the key and value for a chosen index in my array-list that contains hash map. I know that to replace a key you need to first remove the key that is going to be replace and put a new one. But that doesn't save the position of the removed key and value. Thus i thought of iterating through my array-list of hash map and create them one by one in a new hash map in which I can add an if command for my chosen index let say 1,1 there it will change the index position that i wanted to be change and copy the ones that are not to be edited. I don't know how and the right way of how to copy. Please take note that I am just a beginner in java and trying to learn. What is the way to properly do this?

I tried just manually doing an update of key and value but it changed the position/index of where it was supposed to be.


I expect that I can edit both the key and value of any index in any given table. The keys also have to be unique.
 
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

James Mac wrote:. . . to replace a key you need to first remove the key that is going to be replace and put a new one. But that doesn't save the position of the removed key and value. . . .

No, you are correct. A hash map doesn't have any concept about the order of its K‑V pairs. So you are going to have to use something different if you want to retain insertion order and replace a K in the same position as its predecessor was. Remember that if your map already contains that K, calling put() a V against that K, you will have something completely different happen (I think you know that already).
  • I suggest you go back to the Java™ Tutorials and look at the two pages about Maps, and remind yourself of their capabilities, If I haven't made a mistake about your requirements, I don't think any of the types shown there will do. I think, if you put() anything with a new K into a linked hash map, the implementation will perceive that as a new K and put that last in the accompanying list, so it will be out of order.
  • I suggest you try searching for Apache Commons Bidirectional Map or Google Guava Bidirectional Map; maybe a bidirectional map will allow you to change the K on its own.
  • I suggest you consider a plain simple hash map, accompanied by a list for your Ks. Whenever you are altering a K, you can find it in the accompanying List and call set() on that List. I suggest you find out about the performance of that technique if you have a large map (maybe 1,000,000 pairs)
  • I suggest you consider two maps, one being a Mao<String, Long> where the V represents the insertion position. Again find out about the likely performance of such a second map with a large collection.
  • I did wonder whether you could write your own map implementation whereby you can change a K directly, but I think that won't work.
  • I suggest you will learn quite a lot when you read all that lot.
    Find out about ways to copy maps; you will probably find that most implementations have copy constructors. Much better to use such a constructor that trying copying with clear() and addAll().
    There are all sorts of things in your code which could be improved; I hven't got the time to tell you about everything, but there are two things I shall mention:-
  • 1: You shouldn't create a new Scanner to read from System.in for each method call. You only need one Scanner reading System.in for the whole application.
  • 2: It is usually an error to make something static without a good explanation. I think you should have multiple classes and shorten your main method.
  • Well done producing an interesting and unusual question
     
    Saloon Keeper
    Posts: 3411
    149
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry for my late reaction, I've just been attended to this topic.

    Here's my 2 euro cents. Do you have to use a List<LinkedHashMap>>? If you have a Pair<String, String> (for instance, the javaFX Pair class), you could have say an ArrayList<ArrayList<Pair>>, and then you can use the List method set(int index, new Pair<>).
     
    Piet Souris
    Saloon Keeper
    Posts: 3411
    149
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    On second thought: with what I wrote you would loose the O(1) lookup. So, I like the idea of what Campbell suggested: use a second Map. My suggestion is: List<Map<String, Pair<String, Long>>, where the Long is indeed the insertion index.

    Initially your List would be empty. Then, the first element could be like:


    and then later, you could do:


    That still has the O(1) lookup time, and if you want to print out the values of such a Map, then sort the values on their index.

    Global health warning: I have not tested this!
     
    Bartender
    Posts: 10772
    71
    Hibernate Eclipse IDE Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    James Mac wrote:I need to change the key and value for a chosen index in my array-list that contains hash map.


    Unfortunately, you've told us what you want to do without explaining why, and since LinkedHashMap already retains insertion order by key value, I wonder if you're not overcomplicating things.
    Maybe if you can explain why you want to do this, we could suggest a more organic solution.

    My 2¢.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 65050
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mentioned in despatches?

    Congratulations For starting a thread quoted in the July 2019 CodeRanch Journal, you have been awarded a cow.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!