| Author |
why we don't have a map view operation on Lists.
|
Ritesh raushan
Ranch Hand
Joined: Aug 29, 2012
Posts: 99
|
|
|
why we don't have a map view operation on Lists.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
Because lists and maps are two fundamentally different data structures.
A list is just a list of elements. A map is a collection of key-value pairs. What would you expect to get if you'd view a list as if it is a map?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Ritesh raushan
Ranch Hand
Joined: Aug 29, 2012
Posts: 99
|
|
Jesper de Jong wrote:Because lists and maps are two fundamentally different data structures.
A list is just a list of elements. A map is a collection of key-value pairs. What would you expect to get if you'd view a list as if it is a map?
i asked because the two methods of map returns Set why not List.
Set s=map.entrySet();
set s=map.keySet();
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3860
|
|
Because Set represents the meaning better. A Map isn't ordered, and the keys of a Map are unique. These properties correspond to a Set, not a List.
If you happen to want the keys in a List, it's easy to put them in one.
|
 |
Ritesh raushan
Ranch Hand
Joined: Aug 29, 2012
Posts: 99
|
|
|
ok then map generate hashCode from key but how hashcode generate in set.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3860
|
|
Ritesh raushan wrote:ok then map generate hashCode from key but how hashcode generate in set.
From the hashcode of the object in the set.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Ritesh raushan wrote:ok then map generate hashCode from key but how hashcode generate in set.
No, the key generates its hash code in its hashCode() method. The set and map do not generate those hash codes.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
|
|
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.
HIH
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
 |
|
|
subject: why we don't have a map view operation on Lists.
|
|
|