This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Ritesh raushan wrote:i asked because the two methods of map returns Set why not List.
Matthew's quite right about why Sets make more sense, but for keySet() this is mainly because Java chose to make Map keys unique - my guessing is for simplicity and consistency. There are Map structures (such as "tree"- and skiplist-based maps) for which this is NOT a fundamental requirement. That said, mappings (key-value pairs) should generally be unique; so entrySet() does make a lot of sense.
It's also possible to set up a Map where the "element index" is the key; however, this is more akin to a sparse array than a List, because insertion and removal, even from the middle of the "list" doesn't affect the indexes of other elements.
You might also want to have a look at Multimaps, which allow a key to represent more than one value. At present, Java doesn't include an implementation for one though.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here