This discussion is archived
1 Reply Latest reply: Dec 7, 2013 2:03 PM by rp0428 RSS

iterating over Collection veiw of a Map

4bc17f39-a73f-4223-b9a3-009e646e8749 Newbie
Currently Being Moderated

Hi All ,

 

There are 2 TreeMaps <Zinger,String>:

 

tp : it takes a Comparator in its constructor.

tm : it has a no-argument constructor.

 

Then both Trees are converted to their Collection view and  then both collections are given Iterators .

 

Zinger implements Comparable , so has compareTo() which in turn is using Zinger’s getName for comparing two Zingers.

 

Similarly , for the TreeMap which takes Comparator , so Comparator is implemented via class Cataclysm

 

 

 

import java.util.*;

import java.util.Comparator;

 

 

class Zinger implements Comparable<Zinger>

{

int i;

static int c;

Zinger(int m)

{

double d=m*Math.random();

this.i=(int)d;

System.out.println(this.i+"is i of "+this.hashCode()+"currently c is :"+c);

c++;

}

int getRandom()

 

{

return i;

}

String getName()

{

return ("Zinger"+c);

}

public int compareTo(Zinger o2){

return (this.getName()).compareToIgnoreCase(o2.getName());

}

}

 

public class TreeMapz

{

public static void main(String args[])

{

TreeMap tm = new TreeMap<Zinger,String>();

Cataclysm cr= new Cataclysm();

TreeMap tp= new TreeMap<Zinger,String>(cr);

for (int i=0;i<9;i++)

{

Zinger z=new Zinger(i);

  1. tp.put(z,z.getName());
  2. System.out.println("innnnnsssssssssssssssss");
  3. tm.put(z,z.getName());
  4. System.out.println("ooooutssssssssssssssssss");

}

 

Collection cp =tp.values();

Collection cm =tm.values();

Iterator<Map.Entry<Zinger,String>> itp=cp.iterator();

Iterator itm=cm.iterator();

while(itp.hasNext())

{

System.out.println("this is "+itp.next());

//System.out.println(cm.next());

}

while(itm.hasNext())

{

System.out.println("this is widout comparator"+itm.next());

}

/*for(Map.Entry<Zinger,String> e: tp.entrySet())

{

System.out.println(e.getKey()+""+"is the key to zinger"+e.getKey().getName());

System.out.println(e.getValue()+""+"is value to zinger"+e.getKey().getName());

}*/

}

}

class Cataclysm implements Comparator<Zinger>

{

public  int compare(Zinger o1, Zinger o2)

{

if (o1.getRandom()< o2.getRandom())

return 1;

else if(o1.getRandom()> o2.getRandom())

return -1;

else

return 0;

}

}

 

 

'm not able to understand why next() in the code is calling getName() of Zinger on executing the code .

  • 1. Re: iterating over Collection veiw of a Map
    rp0428 Guru
    Currently Being Moderated
    'm not able to understand why next() in the code is calling getName() of Zinger on executing the code .

    That's because it isn't calling that method. And you haven't posted anything that shows that it is. This is the tail end of the output

    . . .

    7is i of 12568800currently c is :8

    innnnnsssssssssssssssss

    ooooutssssssssssssssssss

    this is Zinger9

    this is Zinger8

    this is Zinger5

    this is Zinger6

    this is Zinger7

    this is widout comparatorZinger9

    I assume that the lines such as 'this is Zinger9'  are where you think that 'getName' is being called by 'itp.next()'.

     

    But it is actually YOUR code that creates the collection entries that is calling 'getName'

    tp.put(z,z.getName());

    You are calling 'getName' and using that as the value (e.g. 'Zinger9') of the entry. My 'guess' is that you meant to use 'z.getName' as the KEY and to use 'z' itself (the Zinger instance) as the value. Then you would probably want to create a collection of the 'keys' and iterate over them.

     

    But instead you are creating a collection of those 'string' values

    Collection cp =tp.values();

    Each of those values is a string. And then you create an iterator over that collection of 'strings':

    Iterator<Map.Entry<Zinger,String>> itp=cp.iterator();

    And then in a loop you use that iterator to execute this code:

    System.out.println("this is "+itp.next());

    So 'next' is merely getting the next string from your collection of strings. It isn't referencing any Zinger instance at all.

     

    I suggest that you begin using a GUI such as NetBeans and examine the variables you are creating as you step through the code using the debugger. If you do that you can  see what values those variables and collections contain.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points