Dear all,

I have stuck with one problem.
I have taken one Hashmap, Performing some input operation,remove operation on it.
So after performing remove operation, i want value of 2 successive key in two different array.
Also i want to match 1st key's value with all other keys' value.
Like 1st key value matched with 2nd kay value,then 3rd key value upto last key
Again 2nd key value matched with 3rd kay value,then 4th key value upto last key.

I ahve also make some code but it's not working properly.

Here "m1" is my hash map.Originally there were 20 key value pair, after performing operation there will bw only 9 kay value pair.

So i have do following thing,but it only iterates for first key only.
My question is:How to iterate it through all key?

Map<Integer,String[]> m1 = new HashMap<Integer,String[]>();
//defines map m1
loc[0] = 1;
Set s1 = m1.keyset();
Iterator it1 = s1.iterator();
Iterator it2 = s1.iterator();
System.out.println(s1);
//it prints all the keys available in hashmap m1.
while(it1.hasNext()
{
String[] a1 = (String[])m1.get(it1.next());
//it gives the value of 1st key in m1
while(it2.hasNext())
{
String[] a2 = (String[])m1.get(it2.next());
//it gives the value of 2nd kay from map m1
if(a2[loc[0]]==a1[loc[0]])
{
System.out.println("Matched");
}
else
{
System.out.println("Matched");
}
}
}

It will run for inside iterator it2 for 8 times,but when it comes to next kaey of it1,it2 loop will be stopped.
so how to solve this?

Thank you.

Recommended Answers

All 5 Replies

Can you post a small complete program that compiles, executes and show the problem.
I don't see where you add anything to the hashmap in the code you posted.

Be sure to properly format the code. The posted code does not have proper formattin with all the statements starting in the first column.

hi,

make use of this simple example hope may work.



import java.util.*;

public class hashmap {
        public static void main(String[] args) {
                HashMap hash = new HashMap();
                hash.put("roll", new Integer(12));
                hash.put("name", "jack");
                hash.put("age", 22);
                Set s = hash.entrySet();
                Iterator i = s.iterator();
                while (i.hasNext()) {
                        System.out.println(i.next());
                }
        }
}    

Here's what your code is doing at the moment...

iterator1 = ...
iterator2 = ....
while (iterator1.hasNext()) {
    ...
    while (iterator2.hasNext()) {
        ...
    } 
    // after the first pass you have now used up all the elements in iterator2
    // next time round the outer loop iterator2 will still be all used up, so
    // the inner loop will never be excuted again
}

... and here's what it should be doing...

iterator1 = ...
while (iterator1.hasNext()) {
    ...
    iterator2 = ....
    // every time you go round the outer loop you need to start a 
    // new iterator for the inner loop    
    while (iterator2.hasNext()) {
        ...
    } 
}
commented: really useful... +1

thanks james cherill.

I got it. and i have completed it.

Excellent! Please mark this thread "solved" for our knowledge base. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.