Set vs List vs Map

Posted on Posted in Architect, Java

Basic Question in Java is usually what is the difference between Set/List/Map.

Set – is used usually used when you want to keep a collection of items that have no duplicates. Plain and simple. So think set when you want a unique collection of items.

  • Variations
    • TreeSet (interface: SortedSet) – sorts the value in the set by natural ordering or comparator
    • HashSet – no ordering, puts objects in different buckets based on their hash, faster than treeset

List – is used with regards to just keeping a sequence of objects, it does not care about duplicates the primary focus of sequence and iteration

  • Variations
    • ArrayList – basic list of items, random access, thread-safe
    • Vector – non thread safe version of Arraylist
    • Linked List – items are doubly linked to each other

Map – on the other hand has a key-value collection, you assign a key to a particular object, say you have 2 dog objects, you can assign different keys for each dog objects, like: Brownie and Blackie. So if you fetch from the collection Blackie, it will  return that specific object associated to that key.

  • Variations
    • TreeMap (interface SortedMap) – ordered version of map
    • HashMap – no ordering of map, different buckets per object based on hash
    • LinkedHashMap – doubly linked objects version of map

 

Leave a Reply