What are the difference between Set and List.in Java ?

October 28, 2007 · Filed Under Java SE Interview questions, Placement Questions 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

The Java 2 container library takes the issue of “holding your objects” and divides it into two distinct concepts:

1. Collection: a group of individual elements, often with some rule applied to them. A List must hold the elements in a particular sequence, and a Set cannot have any duplicate elements. (A bag, which is not implemented in the Java container library—since Lists provide you with enough of that functionality—has no such rules.)

2. Map: a group of key-value object pairs. At first glance, this might seem like it ought to be a Collection of pairs, but when you try to implement it that way the design gets awkward, so it’s clearer to make it a separate concept. On the other hand, it’s convenient to look at portions of a Map by creating a Collection to represent that portion. Thus, a Map can return a Set of its keys, a Collection of its values, or a Set of its pairs.

Maps, like arrays, can easily be expanded to multiple dimensions without adding new concepts; you simply make a Map whose values are Maps (and the values of those Maps can be Maps, etc.).

Related Articles


Get Latest JobsAdda.com Jobs, news & updates  via Email