Results 1 to 7 of 7
  1. #1
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default [SOLVED] LinkedHashMap

    hi everyone,
    i would like to know can we change the value of some "key"s in LinkedHashMap, and remain the order of "value" in LinkedHashMap?

  2. #2
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    yes, you would have to make new LinkedHashMap. go through all the elements of the HashMap and put it into the new HashMap with a different key. Other than that i dont believe there is a way to change the key. Hope this helps.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Or write your own implementation that has a changeKey() method. It would involve a remove() and put() in the map, but not changing the list.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    after remove and put, the order of linkedHashMap will change...

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    No, you write your own version of LinkedHashMap. The simplest - though not most efficient - method it to encapsulate bot a Map and a List, then LinkedHashMap.put() calls both Map.put() and List.add(). LinkedHashMap.changeKey(old, new) would call Map.remove(old) and Map.put(new), but leave the List alone.

    Alternatively, you can probably have a look at the source code for Sun's LinkedHashMap and add the functionality there.

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

Similar Threads

  1. LinkedHashMap insertion whilst iterating
    By Paul Richards in forum Advanced Java
    Replies: 7
    Last Post: 02-13-2009, 02:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •