# I guess I am iterating key(itr) wrong way?

this is the code I have written in Java, itr is the iterator for the key, I am facing a problem in iterating the key, the question for the code is :We are given an array of 2n integers wherein each pair in this array of integers represents the year of birth and the year of death of a dinosaurs respectively. The range of valid years we want to consider is [-100000 to 2005]. For example, if the input was:

-80000 -79950 20 70 22 60 58 65 1950 2004

it would mean that the first dinosaur had a birth year of –80000 and an year of death of –79950 respectively. Similarly the second dinosaur lived from 20 to 70 and so on and so forth.

We would like to know the largest number of dinosaurs that were ever alive at one time. Write a method to compute this, given the above array of 2n integers.

Write one of the following methods:

C/C++: int find_max_dinosaur (int[] years);

Java: public int findMaxdinosaur (int[] years);

```package usingarray;

int dinoStrength;
int deathOfDino;
int dinoBirthAge;
int currentDinoBirthAge;
int currentDinoDeathAge;
int count;

TreeMap dino=new TreeMap();
List<Integer> dinos = new ArrayList<Integer>();

Scanner sc=new Scanner(System.in);

public void getDionoAges()
{
System.out.println("How many Dinosaur");
dinoStrength=sc.nextInt()+1;
for(int i=1;i<dinoStrength;i++)
{
System.out.println("Please enter "+i+" dino birth age and death age:");
dino.put(sc.nextInt(),sc.nextInt());
}
}

public void logic()
{
Collection value = dino.values();
Collection key = dino.values();
Iterator it = value.iterator();
Iterator itr = key.iterator();

System.out.println("logic");

for(int x=0;x<dinoStrength;x++)
{
System.out.println("in for");
while(it.hasNext() && itr.hasNext())
{
System.out.println("in while");
deathOfDino=(int) it.next();
//currentDinoDeathAge=(int) it.next();
dinoBirthAge=(int) itr.next();

while(itr.hasNext())
{
System.out.println("In itr while");
currentDinoBirthAge=(int) itr.next();
if(dinoBirthAge<=currentDinoBirthAge && currentDinoBirthAge<=deathOfDino)
{
count++;
System.out.println(count);
}
}
dinos.add(count);
}
}
}

public void display()
{
Iterator<Integer> it = dinos.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void main(String[] args) {
VickyDino vd=new VickyDino();
vd.getDionoAges();
vd.logic();
vd.display();
}
```

}

## Answer

I would assume your problem is this:

```Collection value = dino.values();
Collection key = dino.values();
```

You’re using `dino.values()` twice; the second should probably be `dino.keys()`. However, since you’re putting the birth year into the keys and the year of death into the values, you probably may wanna switch the two. Note: I didn’t check whether the logic in your loops is correct.