3

To my understanding, a List is an ordered collection of items. And a Set is a collection of unique items.

Now my question is, why does LinkedHashSet, which describes an ordered collection of unique items, implement the Set interface (=> unique), but not the List interface (=> ordered)?

One possible argument is that List is intended for random access datastructures, but that would be invalidated by the fact that LinkedList doesn't have "true" random access either. In fact, LinkedHashSet is backed by an internal linked list. Also the documentation for List says otherwise:

Note that these [positional index] operations may execute in time proportional to the index value for some implementations.

Felk
  • 7,720
  • 2
  • 35
  • 65
  • Because a `List` allows duplicates. – Tom Aug 29 '17 at 11:34
  • 1
    because as the name suggest is a ***SET*** – ΦXocę 웃 Пepeúpa ツ Aug 29 '17 at 11:34
  • `In fact, LinkedHashSet is backed by a LinkedList` - wrong. It is backed by an internal implementation of a linked list, not by java.util.LinkedList. – Eran Aug 29 '17 at 11:34
  • @Eran thanks, I updated my question – Felk Aug 29 '17 at 11:34
  • @Tom the list interface doesn't specify that it has to allow duplicates. it even says: "It is not inconceivable that someone might wish to implement a list that prohibits duplicates" – Felk Aug 29 '17 at 11:37
  • @ΦXocę 웃 Пepeúpa ツ that's just the class' name, but the behaviour suggests that it's both a set and a (immutable) list – Felk Aug 29 '17 at 11:39
  • I don't know the answer, but I have a suggestion for how you could find out: write a class that implements `List` and delegates all its operations to an internal `LinkedHashSet`. Find out where the implementation gets messy. – Kevin Krumwiede Aug 29 '17 at 11:45
  • 2
    This is not opinion based. This is a question about what were the reasons of `LinkedHashSet` not implementing `List` interface when it was created by Sun/Oracle. – Jaroslaw Pawlak Aug 29 '17 at 12:37
  • @KevinKrumwiede I went ahead and implemented a version of `LinkedHashSet` that implements `List`: https://gist.github.com/Felk/185fec05c0d081908ec668a3fb208723 looks fine to me – Felk Aug 29 '17 at 12:50
  • @JaroslawPawlak - Exactly, the class was created by Sun/Oracle. It is vanishingly unlikely anyone here is close enough to the designers of the Java class library to be able to accurately explain why the class is the way it is. Hence answers are going to share our opinions on why we might have done it this way were we in their place. Which we're not. Furthermore this question is not asking anybody to solve a problem the OP is having - it's not like we can change the Java standard library for them. – millimoose Aug 29 '17 at 14:23

2 Answers2

3

If it implemented a List you would be able to use it as a List:

List list = new LinkedHashSet();

This might lead to issues with duplicates which don't appear in Set but are allowed in List.

In other words, you shouldn't declare that something is a List when it doesn't allow duplicates even if it holds the order and allows adding, getting, removing and checking the size.

Unlike sets, lists typically allow duplicate elements
--List documentation

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • 2
    From that link: "It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them . . ." – Kevin Krumwiede Aug 29 '17 at 11:49
  • @KevinKrumwiede "...but we expect this to be rare" – xenteros Aug 29 '17 at 11:49
  • 3
    Rare, not prohibited. Accepting duplicate elements is *not* a characteristic of `List`. It's a characteristic of common implementations of `List`. – Kevin Krumwiede Aug 29 '17 at 11:51
  • 1
    @KevinKrumwiede And that's the issue. It would be quite confusing when you have a list which doesn't allow dupe and you don't know the actual type, because you only work with `List`. Although the interface itself might allow it, it isn't what the developer would expect and that counts more (at least on my count, since code readability is a big plus). – Tom Aug 29 '17 at 11:57
  • 3
    `List.add(E e)` documentation says "Appends the specified element to the end of this list (optional operation).". The method returns boolean saying whether collection has been changed by the call, as specified in `Collection.add(E e)` documentation: "Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)". – Jaroslaw Pawlak Aug 29 '17 at 12:33
  • 2
    Hence, having `List list = new LinkedHashSet();` and `list.add(1); list.add(1);` returning first `true` and then `false` looks like logical scenario to me. – Jaroslaw Pawlak Aug 29 '17 at 12:35
0

because LinkHashSet is class which implements set interface . List and Set has its own functionality List allowed duplicates while set do not allowed duplicates but if you want linear order insertion in a HastSet then LinkedHashSet is used with no duplicates in itself ..

Set s = new LinkedHashSet();

is the implementation of a set in which insertion order is preserved and duplicates do not allowed..

Adonis
  • 4,670
  • 3
  • 37
  • 57
  • List does not necessarily allow duplicates, please see the other answers and comments. – Felk Aug 29 '17 at 12:07
  • https://www.google.co.in/search?source=hp&q=list+allow+duplicate+values%3F&oq=list+allow+duplicate+values%3F&gs_l=psy-ab.3..0i22i30k1l3.588.5222.0.5584.13.11.0.0.0.0.350.2066.2-6j2.8.0....0...1.1.64.psy-ab..5.8.2059.0..0j35i39k1j0i67k1j0i20k1.utT8YhyMEcY please gather the knowledge then comment – chetan mehra Aug 31 '17 at 07:24