java - Is there a LinkedHashSet / Map equivalent, with order preserved for inserted duplicates? -


i wondering 2 things.

1) hashset added duplicate value? believe replaces value? if case, linkedhashset? i'm pretty sure doesn't change order, still replace value? (why it?)

2) if wanted use ordered collection doesn't allow duplicates, replace existing value it's duplicate, re-ordering position? ie. linkedhashset, except duplicate values added replaced , positions updated. there collection might this? or have write own? don't want have write own!

1) adding duplicate set not (it returns false immediately, , contents of set not affected), regardless of particular implementation, hashset, treeset or linkedhashset.

2) check out linkedhashmap, is, probably, closest want. has boolean argument constructor, lets specify whether want use "insertion-order" (false) or "access-order". former same linkedhashset, latter "bump" key if re-insert it, , also if up:

map<string, integer> map = new linkedhashmap(10, 0.75, true); map.put("foo", 1); map.put("bar", 2); system.out.println(map.keyset().iterator().next()); // prints "bar" map.put("foo", 1);  system.out.println(map.keyset().iterator().next()); // prints "foo" map.get("bar"); system.out.println(map.keyset().iterator().next()); // prints "bar" 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -