tags:

views:

183

answers:

8

Hi there,

I didnt get the sense of Maps in Java. When is it recommended to use a Map instead of a List?

thanks in advance,

nohereman

+1  A: 

A map is used as an association of a key and a value. With a list you have basically only values.
The indexes in List are always int, whereas in Map you can have another Object as a key.


Resources :

Colin Hebert
+1  A: 

When you want to map instead of list. The names of those interfaces have meaning, and you shouldn't ignore it.

Use a map when you want your data structure to represent a mapping for keys to values. Use a list when you want your data to be stored in an arbitrary, ordered format.

Sheldon L. Cooper
This is like using a word in it's own definition. This is not a very helpful answer...
Chris Thompson
-1 for being asinine
Erick Robertson
unhelpful answer.
Tony Ennis
No, it's pointing out that the names of those interfaces have meaning, and you shouldn't ignore it.
Sheldon L. Cooper
Added your comment and a bit more information, then removed my downvote.
Erick Robertson
+2  A: 

Java map: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Java list: An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

The difference is that they are different. Map is a mapping of key/values, a list of a list of items.

Chris
you forgot lists can contain duplicates
Woot4Moo
Is a List ordered? Or does 'ordered' mean something different than 'sorted.' Not trying to nitpick...
Tony Ennis
the java doc says ordered, I almost put the same thing Tony.
Woot4Moo
@Tony: Sorted != Ordered. For an ordered collection, the elements have an(y) ordering (which is obvious to the programmer, and usually can be manipulated by the programmer). Lists are ordered, e.g. each item in a LinkedList points to the next item. For sorted collections, the order of the items is determined by comparisions of the items, and always kept sorted.
delnan
A: 

Map and List serve different purpose.

List holds collection of items. Ordered (you can get item by index).

Map holds mapping key -> value. E.g. map person to position: "JBeg" -> "programmer". And it is unordered. You can get value by key, but not by index.

amorfis
A: 

Depends on your performance concerns. A Map more explicitly a HashMap will guarantee O(1) on inserts and removes. A List has at worst O(n) to find an item. So if you would be so kind as to elaborate on what your scenario is we may help more.

Woot4Moo
+1  A: 

Its probably a good idea to revise Random Access Vs Sequential Access Data Structures. They both have different run time complexities and suitable for different type of contexts.

Ravi Gummadi
+1  A: 

Say you have a bunch of students with names and student IDs. If you put them in a List, the only way to find the student with student_id = 300 is to look at each element of the list, one at a time, until you find the right student.

With a Map, you associate each student's ID and the student instance. Now you can say, "get me student 300" and get that student back instantly.

Use a Map when you need to pick specific members from a collection. Use a List when it makes no sense to do so.

Say you had exactly the same student instances but your task was to produce a report of all students' names. You'd put them in a List since there would be no need to pick and choose individual students and thus no need for a Map.

Tony Ennis
A: 

I thinks its a lot the question of how you want to access your data. With a map you can "directly" access your items with a known key, in a list you would have to search for it, evan if its sorted.

Compare:

List<MyObject> list = new ArrayList<MyObject>();
//Fill up the list
// Want to get object "peter"
for( MyObject m : list ) {
 if( "peter".equals( m.getName() ) {
    // found it
 }
}

In a map you can just type

Map<String, MyObject> map = new HashMap<String, MyObject>();
// Fill map
MyObject getIt = map.get("peter");

If you have data to process and need to do it with all objects anyway, a list is what you want. If you want to process single objects with well known key, a map is better. Its not the full answer (just my 2...) but I hope it might help you.

InsertNickHere