?
Solved

give me the pseudo code for hashmap in java to understand the functionality of hashmap

Posted on 2011-10-06
6
Medium Priority
?
2,241 Views
Last Modified: 2012-05-12
give me the pseudo code for hashmap in java to understand the functionality of hashmap
0
Comment
  • 4
  • 2
6 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36925748
What do you mean by pseudo-ccode ?
You probably don't need pseudocode of the methods to understand functionality and to use HashMap

Hashmap is really not that difficult - this is an object that allows to
maintain corresspondence between the keys and the values.
Objects of any type (buit ono-primitive) could server as keys and as values.
Each key can have only one associated value (but of course several different keys can point to the same value).

And there are two main methods

put(Object key, Object value) - this method adds a new pair to a set of assocciatioopns which you may already have in th e HasMap

and another method

get(Object key)

this will retuen the vlaue Object correspnding to the provided key

As each key can have only one value
if you call put(key, vlaue) with the value of key which already exsists in the HashMap
you'll overwrite the previous pair with the same key


For more deatils read the preamble of the API doc for HashMap
http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html

HashMap along with the ArrayList is I believe one of the most uused collections in Java
priogramms as indeed very often in programming situations it is convenient to
establsih this kind of correspodedce between two sets of objects

Let me know if you have any questions about it.







0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 36928614
give me the pseudo code for how to store the value in hashmap and how to retreive that one
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928708

This is how you put:
HashMap<String,String> m = new HashMap<String,String>();
m.put("a","b");
String s0 = "c";
String s1 = "d";
m.put(s0,s1);

This is how you retrieve:

String res = m.get("a");

res will be equal to  "b" in this case.

String res1 = m.get(s0);

res1 will be equal to "d";

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 36928724
How to iterate hashmap from for loop
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36928760

HashMap<String,String> m = new HashMap<String,String>();
m.put("a","b");
String s0 = "c";
String s1 = "d";
m.put(s0,s1);

for (String ss : m.keySet()){
Systemn.out.println(ss + "  " + m.get(ss));

}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928763
  method keySet() returns Set which you can iterate and then retrieve corresponding values
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question