Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we don't have a map view operation on Lists.

 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we don't have a map view operation on Lists.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok then map generate hashCode from key but how hashcode generate in set.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic