jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes why we don't have a map view operation on Lists. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why we don Watch "why we don New topic
Author

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

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
why we don't have a map view operation on Lists.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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: 100
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: 4343
    
    8

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: 100
ok then map generate hashCode from key but how hashcode generate in set.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

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: 38008
    
  22
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: 7545
    
  18

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?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why we don't have a map view operation on Lists.
 
Similar Threads
about hashmap implementation of arraylist
Data Structure & Problem Solving using JAVA
Why are getters called multiple times?
when the HashMap instance need synchronized?
i want to block a user after three successive wrong password entry