List or Set?

Discussion in 'Spigot Plugin Development' started by MisterFantasy, Jun 6, 2017.

  1. Hello wonderful people on spigot,

    Today I have a question about on what occasions it's better to use a List or a Set.
    I haven't really used Sets so far so that's why.

    Thanks in advance :)
     
    • Like Like x 1
  2. Alrighty thank you :)
     
  3. Sets have no ordering (usually); the purpose is to store objects that can be quickly looked up without having to traverse an entire list, usually it is used to keep track of objects involved in something where lookup-time is more important than order or memory saving, like players with moderator permissions.

    Lists come in two types, array-backed and linked, each with different properties, but they have the advantage of being analogous to an array, with a specific order and fast access to elements by index. A Set has no easy way to access elements without knowing what they are.
     
    • Friendly Friendly x 1
  4. Okay, thank you! This is really usefull :).
     
  5. If you do not have/need/want duplicates, then use a Set. In some cases, a List can be preferential just because some of the java api accepts Lists instead of Collections (e.g. Collections#sort).

    Sets have ordering, a hashed collection does not (HashSet). SortedSet (TreeSet, ConcurrentSkipListSet, etc) is ordered based on a comparator/natural ordering, and most others are insertion order (though you ideally wouldn't rely on it outside of LinkedHashSet. That's why Collections#sort basically doesn't include sets).

    As for lists, there's many more kinds than LinkedList and ArrayList (esp the parent designs; Stack/Queue).
     
  6. Thank you!
     
  7. Keeping it simple. And i have never heard of a third type of list, there is no logical way to implement a list aside from linked nodes or an array. Every more specialized list in the JVM is based on one of those, to my knowledge.

    Edit: Yes, every list in java is either based on an array or linked list. Anything else is probably not a real list, just some sort of table, set or map masquerading behind a list interface for unknown reasons.