This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes when to use  ArrayList and when to use HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "when to use  ArrayList and when to use HashMap" Watch "when to use  ArrayList and when to use HashMap" New topic
Author

when to use ArrayList and when to use HashMap

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
when to use ArrayList and when to use HashMap in webApplication. I know by using ArrayList we can store values. by using HashMap we can store key , value pairs. what situation we use HashMap
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

ArrayList and HashMap are two different datastructures, as you already know - an ArrayList is an ordered sequence (a list), and a HashMap is a dictionary (which contains key-value pairs, and where you can efficiently lookup the value if you know the key).

It's hard to say when you use which; for some problems, it's natural to use a list, and for other problems, it's natural to use a dictionary. You'll have to think in each situation which data structure fits best.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I Understand your point, can you give examples for ArrayList and HashMap . I say if you want to store Employee name ,in this situation i can use ArrayList . if you want to store Employee name with his salary i can use Employee name as key and salary as value . Is it correct.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Jesper Young: an ArrayList is an ordered sequence (a list)


Do you mean arraylist is an ordered list and it maintains insertion oder?
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
what i know is "list is ordered" but not maintains insertion order . I asked when to store values in ArrayList and when to store Key value Pairs in hashmap . For that please give example
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Rajendra Prakash wrote: if you want to store Employee name with his salary i can use Employee name as key and salary as value . Is it correct.

No. use List instead unless you have some unique key.

http://muhammadkhojaye.blogspot.com/
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
That is what i am asking , why you use key . when you prefer to use key value pair.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
A list is like a shopping list
  • Eggs
  • Milk
  • Flour
  • only it maintains the order; that list is different from milk-flour-eggs.

    A map is like an address book
  • Campbell ↦ 123 High St
  • Muhammad ↦ 987 London Rd
  • Rajendra ↦ 345 Station Rd
  • Jesper ↦ 567 Church St
  • Rahul ↦ 765 North St
  • There the name is the "key" and the address is the "value".
    Rahul P Kumar
    Ranch Hand

    Joined: Sep 26, 2009
    Posts: 188
    usually key should be immutable. String is fit for key. you can use them as key. but the problem is name can be duplicate. In map, if you will use names for key then corresponding salaries will be overriden by latest employee name in case of duplicate names. To avoid such situations you can choose to override equals method, where in case of duplicate first name, try combination of last name and so on. Don't forget to override hashcode(), if you overridden equals().

    Same thing you could have achieved through list by storing value objects in it. But guess you have to search salary of employee. In list you have to iterate over whole list to compare first name or so to find your desired employee, while in map you just have to supply name of the employee, which is key and you get the corresponding salary.
    Contrast this with the situation , where there are values but nothing to associate as key. You don't have to find search a particular value in the data structure, then go for list.
    Jer Deras
    Greenhorn

    Joined: Sep 29, 2009
    Posts: 10
    when you populate a table you would use an array list where you have to iterate over an entire collection or list.

    hashmap is used when you are intersted with a particular value to be used from the entire collection or list.

    example: you have your name in an arraylist. in order to print it you need to iterate the entire list and compare each entry.
    in case of hashmap you will be having an equivalent key for your name value. instead of iterating the list you can get the value by specifying the key associated with it and only that value will be printed.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: when to use ArrayList and when to use HashMap