Saturday, August 15, 2009

Difference between Set and List (Set vs List)

Set: an Interface available in "java.util" package.
  • a collection that cannot contain duplicate elements
  • generally order of the elements does not matter
  • models the mathematical set abstraction and is used to represent sets, such as the card comprising a poker hand
List: an Interface available in "java.util" package
  • a collection that can contain duplicate elements
  • it is an ordered collection, also called as a sequence
  • user of a List generally has control over where in (at what index) the List the user want to insert an element and access it using its index
When you access the Set using its Iterator, it doesn't guarantee the order of elements, whereas when you access the List using its Iterator, it gives you a sequential access to the elements in the list one by one.

For the better understanding, you can just execute the example program in my previous post "How Do I Convert Set into List".

No comments:

Post a Comment