• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2577
  • Last Modified:

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

give me the pseudo code for hashmap in java to understand the functionality of hashmap
0
Manikandan Thiagarajan
Asked:
Manikandan Thiagarajan
  • 4
  • 2
1 Solution
 
for_yanCommented:
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
 
Manikandan ThiagarajanSenior consultantAuthor Commented:
give me the pseudo code for how to store the value in hashmap and how to retreive that one
0
 
for_yanCommented:

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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Manikandan ThiagarajanSenior consultantAuthor Commented:
How to iterate hashmap from for loop
0
 
for_yanCommented:

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
 
for_yanCommented:
  method keySet() returns Set which you can iterate and then retrieve corresponding values
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now