HashMap example in Java

By: Charles Viewed: 155012 times  Printer Friendly Format    


The HashMap class uses a hash table to implement the Map interface. This allows the execution time of basic operations, such as get() and put(), to remain constant even for large sets.
The following constructors are defined: HashMap( )
HashMap(Map m)
HashMap(int capacity)
HashMap(int capacity, float fillRatio)

The first form constructs a default hash map. The second form initializes the hash map by using the elements of m. The third form initializes the capacity of the hash map to capacity. The fourth form initializes both the capacity and fill ratio of the hash map by using its arguments. The meaning of capacity and fill ratio is the same as for HashSet, described earlier.

HashMap implements Map and extends AbstractMap. It does not add any methods of its own. You should note that a hash map does not guarantee the order of its elements. Therefore, the order in which elements are added to a hash map is not necessarily the order in which they are read by an iterator.

The following program illustrates HashMap. It maps names to account balances. Notice how a set-view is obtained and used.

import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("John Doe", new Double(3434.34));
hm.put("Tom Smith", new Double(123.22));
hm.put("Jane Baker", new Double(1378.00));
hm.put("Todd Hall", new Double(99.22));
hm.put("Ralph Smith", new Double(-19.08));
// Get a set of the entries
Set set = hm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double)hm.get("John Doe")).doubleValue();
hm.put("John Doe", new Double(balance + 1000));
System.out.println("John Doe's new balance: " +
hm.get("John Doe"));
}
}

Output from this program is shown here:

Ralph Smith: -19.08
Tom Smith: 123.22
John Doe: 3434.34
Todd Hall: 99.22
Jane Baker: 1378.0
John Doe's current balance: 4434.34

The program begins by creating a hash map and then adds the mapping of names to balances. Next, the contents of the map are displayed by using a set-view, obtained by calling entrySet(). The keys and values are displayed by calling the getKey() and getValue() methods that are defined by Map.Entry. Pay close attention to how the deposit is made into John Doe's account. The put() method automatically replaces any preexisting value that is associated with the specified key with the new value. Thus, after John Doe's account is updated, the hash map will still contain just one "John Doe" account.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Great tutorial. THX.
View Tutorial          By: Velvetin at 2009-03-21 11:05:19

2. kevelamane tutorial
View Tutorial          By: pudungi at 2009-07-13 03:29:01

3. fhrdghdddf
View Tutorial          By: dgfghrfhgr at 2009-08-10 07:02:41

4. fhrdghdddf
View Tutorial          By: dgfghrfhgr at 2009-08-10 07:02:55

5. shunmughapathy....
View Tutorial          By: dgfghrfhgr at 2009-08-10 08:01:11

6. shunmughapathy....
View Tutorial          By: dgfghrfhgr at 2009-08-10 08:01:15

7. Nice Example...!
View Tutorial          By: Kuldeep Singh at 2009-10-18 23:58:17

8. Hi there, what a nice example, very useful for me
View Tutorial          By: Yanny at 2009-12-10 12:34:26

9. nice article..
View Tutorial          By: yummy at 2010-01-05 21:18:35

10. It is very simple and understandable tutorial, the
View Tutorial          By: Syed Turab at 2010-03-09 07:27:15

11. gr8 tute dude.... ezy n nice..
View Tutorial          By: hErC!! at 2010-04-20 23:52:19

12. The last output is not :John Doe's current balance
View Tutorial          By: Santosh at 2010-06-01 22:54:21

13. This contents and example taken by java complete
View Tutorial          By: kumar at 2010-06-05 03:02:13

14. nice
View Tutorial          By: rida at 2010-07-03 22:49:04

15. Good Sample, thanks
View Tutorial          By: Tharani at 2010-08-26 23:10:59

16. Nice, Thanx a lot...
View Tutorial          By: Satish Puli at 2010-09-06 04:01:11

17. i really appricate the web site devoloping team
View Tutorial          By: rakesh at 2010-09-18 01:49:25

18. Não entendi
View Tutorial          By: Fernando at 2010-09-22 10:15:59

19. Nice tutorial
View Tutorial          By: Sai at 2010-09-23 21:52:38

20. This is a very great tutoril for the java beginner
View Tutorial          By: Sujoy Saha at 2010-10-01 03:27:57

21. Nice n simple ex!!!!!!!!!!!!!!!!!!!
View Tutorial          By: Siya at 2010-11-17 07:08:34

22. accha h....baht accha h....
View Tutorial          By: Amit at 2010-11-22 06:02:06

23. Great Tutorial for the beginners
View Tutorial          By: Pravat at 2010-12-16 06:08:16

24. YA IT'S VERY USEFUL
View Tutorial          By: JOTHIBASU at 2010-12-20 05:29:38

25. nice one superb..
View Tutorial          By: Rajesh B at 2011-02-01 21:59:56

26. Very Good Tutorial...
Very Good For Beginne

View Tutorial          By: Yatiraj Chakre at 2011-02-02 04:28:57

27. I need more example........
View Tutorial          By: niyaz d at 2011-02-20 23:44:56

28. great resource
View Tutorial          By: Anonymous at 2011-04-14 03:14:43

29. its very easy to understand and implements.
View Tutorial          By: ashish at 2011-05-20 03:12:07

30. its good
View Tutorial          By: Srinu at 2011-05-26 23:57:18

31. nice one
View Tutorial          By: priyaaaaaaaaaaaaaaa at 2011-06-21 08:04:39

32. good. keep it up !
View Tutorial          By: hashu at 2011-06-21 01:49:59

33. i din understand
View Tutorial          By: panda at 2011-07-14 09:35:36

34. easy.......
View Tutorial          By: Suman at 2011-07-20 07:04:28

35. Nice and easy to understand......Thanks for same
View Tutorial          By: vikas at 2011-07-21 07:44:30

36. thanks..very nice and easy to understand for new j
View Tutorial          By: Ranjan at 2011-07-22 08:34:50

37. Please help I want to map the data say username an
View Tutorial          By: akshat at 2011-07-25 12:49:06

38. Simple Example. Understood. Thanx.
In aksha

View Tutorial          By: Amila Nadanda at 2011-07-28 06:21:05

39. Thanks Its nice example for learn basic related to
View Tutorial          By: Milind Deshmukh at 2011-08-10 10:47:57

40. Thanks Its nice example for learn basic related to
View Tutorial          By: Milind Deshmukh at 2011-08-10 10:53:17

41. great.............
View Tutorial          By: chanti at 2011-08-21 07:09:28

42. thank you so much!
View Tutorial          By: anita at 2011-10-10 12:16:35

43. nice one....but i have a problem


View Tutorial          By: sami at 2011-10-23 15:32:44

44. I do not understand this line:
Map.Entry me

View Tutorial          By: hap Og at 2011-11-03 01:42:33

45. great example for beginners. Thank you
View Tutorial          By: kumar at 2011-11-24 08:15:14

46. this is simple example but need to more explanatio
View Tutorial          By: Ramalingam at 2012-01-05 06:25:12

47. How do you remove an element from the map?
View Tutorial          By: Shreejit at 2012-01-29 16:53:27

48. Nice example bro.....Thank u so much
View Tutorial          By: kathir at 2012-02-15 02:20:14

49. nice article. how to rewrite the iterator in ja
View Tutorial          By: junnydc at 2012-02-16 04:04:55

50. Thx for a good one, Can we use anything else inste
View Tutorial          By: NagaSudheer at 2012-04-03 11:53:36

51. Thanks for such a great example .... As a beginner
View Tutorial          By: Sudhir singh at 2012-04-09 08:11:34

52. very easy explain
View Tutorial          By: Rohit Bhojani at 2012-04-10 12:49:20

53. Nice Article.
View Tutorial          By: Swathi Kumar at 2012-04-15 10:44:14

54. Thanks.... Nice Example
View Tutorial          By: Nagarjuna at 2012-05-29 12:00:50

55. Do you have similar sample for nested HashMap?
View Tutorial          By: Vivian T. at 2012-06-14 17:47:20

56. hiiiii .... nice and very much help fullll example
View Tutorial          By: Amith K Bharathan at 2012-06-25 03:52:57

57. hmmmmmm good
View Tutorial          By: Haritha chinnu at 2012-06-30 09:37:49

58. Good one.....
View Tutorial          By: Ratnam.. at 2012-07-03 07:11:26

59. answers to the questions asked by ranjan in commen
View Tutorial          By: A Sadist at 2012-07-05 12:13:45

60. i'm doing project on hotel management...
an

View Tutorial          By: prashant patil at 2012-08-19 05:16:20

61. Very nice one, it really helps me. Thank you very
View Tutorial          By: eky1412 at 2012-09-10 03:45:07

62. hi ..,answer me the comment posted on #26
View Tutorial          By: jag at 2012-09-10 04:56:30

63. Great post. But on theory part I recommend <a h
View Tutorial          By: Java programmer at 2012-10-24 07:42:23

64. good one.......thanks
View Tutorial          By: raghubir at 2012-11-10 19:01:45

65. Why the output list in the wrong order?
View Tutorial          By: Pavel at 2013-02-23 12:05:02

66. Plz catch the exception to make the program comple
View Tutorial          By: Naman at 2013-04-19 06:01:45

67. how to comapre two hash map
View Tutorial          By: vinay at 2013-05-03 10:21:57

68. Thanks for the simple tutorial on hashmaps. I am n
View Tutorial          By: ipkwena at 2013-08-19 09:02:49

69. HashMap works on the principle of hashing. In orde
View Tutorial          By: Ashish at 2014-07-23 10:18:16

70. simple examples that i can understand ,.,,by the w
View Tutorial          By: Chriskuang at 2015-03-19 03:05:59

71. Very Nice and Perfectly Understandable.....
View Tutorial          By: rex at 2015-04-16 12:27:03

72. I want an example...to get multiple values using S
View Tutorial          By: Madhu at 2015-07-13 19:57:14

73. cell phone lookup by name I was just searching for
View Tutorial          By: Smitha8 at 2015-11-25 17:42:17

74. Hey, I thibk yoir site might be having browser com
View Tutorial          By: organic Traffic at 2017-05-12 00:21:42

75. alimexg
View Tutorial          By: alimexg at 2017-05-17 20:09:37

76. ekexnoi
View Tutorial          By: ekexnoi at 2017-05-17 23:31:24

77. ebeenehedif
View Tutorial          By: ebeenehedif at 2017-05-18 01:57:07

78. ruanikiz
View Tutorial          By: ruanikiz at 2017-05-18 03:11:05

79. eedaviy
View Tutorial          By: eedaviy at 2017-05-18 22:15:15

80. ajemihaw
View Tutorial          By: ajemihaw at 2017-05-19 10:56:24

81. omerebu
View Tutorial          By: omerebu at 2017-05-19 11:43:04

82. idaiudiseged
View Tutorial          By: idaiudiseged at 2017-05-19 12:00:43

83. esekxlixejis
View Tutorial          By: esekxlixejis at 2017-05-19 12:41:59

84. upeqikegawe
View Tutorial          By: upeqikegawe at 2017-05-19 12:57:45

85. anadowunepifa
View Tutorial          By: anadowunepifa at 2017-05-19 14:04:26

86. ilujusoneahoi
View Tutorial          By: ilujusoneahoi at 2017-05-20 04:08:40

87. ubwizrawegoag
View Tutorial          By: ubwizrawegoag at 2017-05-20 04:22:10

88. oacuerugo
View Tutorial          By: oacuerugo at 2017-05-20 13:29:52

89. Thankfulness to my father who shared with me on th
View Tutorial          By: Gotard Lupa at 2017-07-07 08:42:23

90. dorpornxxxlop
View Tutorial          By: dorpornxxxlop at 2017-07-10 16:04:38

91. WOW just what I was looking for. Came here

View Tutorial          By: keys copy near me at 2017-09-07 13:38:12