How is the list different from the map?

In java, List and Map are used in collections. But I could not understand in what situations we should use List and what time to use Map. What is the main difference between the two?

+5
source share
7 answers

Now is the right time to read the Java collections tutorial - but essentially a list is an ordered sequence of elements that you can access by index, and a map is usually an unordered mapping from keys to values. (Some cards retain the insertion order, but are implementation specific.)

This is usually pretty obvious when you need to display a key / value and when you just need a set of elements. This becomes less clear if the key is part of the value, but you want to be able to efficiently retrieve an element by that key. This is still a good example of using a card, although in some ways you do not have a separate collection of keys.

Here also Set, which is a (usually disordered) set of individual elements.

+11
source

Map for a Key:Valuepair of data types. For example, if you want to match student roll numbers with their names.

List for a simple ordered set of elements that allow duplication. for example, to represent a list of student names.

+4

. () , , - , , . , , . , equals(), , .

. , , - , . , get (int index), indexOf (Object o), add (int index, Object obj) .. . - , , , .

+1

list - , . , , O (1), .

, , , .

, , hashFunction(key), . Map taks O (1) ( , 2 ), .

wikipedia HashMap

+1

HashList - , -, list.it - . acess . HashMap - - , , HashTable, , . , . . Set - , , . , .

+1

List - (). . ( ) . NULL.

Map - , . . ; .

0
List - This datastructure is used to contain list of elements. 
       In case you need list of elements and the list may contain duplicate values, 
       then you have to use List.

Map - It contains data as key value pair. When you have to store data 
      in key value pair,so that latter you can retrieve data using the key,
      you have to use Map data structure.

- ArrayList, LinkedList
- HashMap, TreeMap

In comparison HashMap- ArrayList- A hash map is the fastest data structure if you want to get all the nodes for a page. The list of nodes can be selected in constant time (O (1)), and in the lists the time is O (n) (n = the number of pages, faster in sorted lists, but never approaches O (1))

0
source

All Articles