0

Why doesn't there exist a class in the standard library that implements both the Set and the List interfaces? Linked set retains the order of a set but doesn't allow random access.

Why isn't this seen as useful?

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
Benedikt Bünz
  • 648
  • 7
  • 22
  • 1
    Why don't you write one? There's nothing magic about the implementations of the current stuff -- you can do anything they can do. – Hot Licks Nov 07 '14 at 21:06
  • See also http://stackoverflow.com/questions/7780813/java-combination-of-set-and-list-interfaces – Raedwald Nov 09 '14 at 00:54
  • I know that "there is now magic" and yes I can and have implemented it myself. This question is out of mere interest. – Benedikt Bünz Nov 09 '14 at 09:02

2 Answers2

2

Because it's extremely hard to implement efficiently, for one, at least if you're supporting removes. For example, Guava's ImmutableSet supports random access via its asList() view, but doesn't support mutation.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

Because it is impossible to do so without breaking the contract of Setor List. A Setmay not hold duplicates. What happens if you use the container as a List and try to add a duplicate?

Raedwald
  • 46,613
  • 43
  • 151
  • 237