This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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?
Articles by Winston can be found here