get objects back from collection
<p>I am trying to modify a specific object which is returned as an Object collection from a method. Now lots of other objects are also added to this collection. Hence finding the specific object gets difficult. I tried to solve it using "instanceof", but am getting the following exception:</p>

<pre><code>java.lang.ClassCastException: com.sheesoft.MockBean cannot be cast to com.sheesoft.TestBean
at com.sheesoft.Main.main(Main.java:30)
</code></pre>

<p>I have been able to reproduce the same exception with some test classes. Please let me know where I am going wrong.</p>

<p>Main:</p>

<pre><code>package com.sheesoft;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    Collection&lt;Object&gt; beanCollection = new ArrayList&lt;Object&gt;();
    TestBean tb1 = new TestBean();
    MockBean mb1 = new MockBean();

    beanCollection.add(tb1);
    beanCollection.add(mb1);

    try{
    Iterator&lt;Object&gt; itr = beanCollection.iterator();
    while(itr.hasNext()){
        Object object = itr.next();
        if(object instanceof TestBean){
            TestBean tb2 = (TestBean) itr.next();
            tb2 = modifyTestBody(tb2);
            System.out.println(tb2.getBody());
        }
        if(object instanceof MockBean){
            MockBean mb2 = (MockBean) itr.next();
            System.out.println(mb2.getMockPayload());
        }
    }
    }catch(Exception e){
        e.printStackTrace();
    }
}

    private static TestBean modifyTestBody(TestBean tb) {
    tb.setBody("NewBody");
            return tb;
    }

}
</code></pre>

<p>Bean1:</p>

<pre><code>package com.sheesoft;

public class TestBean {
private String header;
private String body;

public TestBean(){
    setHeader("header");
    setBody("body");
}

public String getHeader(){
    return header;
}
public String getBody(){
    return body;
}
public void setHeader(String header){
    this.header=header;
}
public void setBody(String body){
    this.body = body;
}
public String toString(){
    StringBuilder sb = new StringBuilder();
    sb.append("header = "+header+"\n");
    sb.append("body = "+body+"\n");
    String returns = sb.toString();
    return returns;
}
}
</code></pre>

<p>Bean 2:</p>

<pre><code>package com.sheesoft;

public class MockBean {
private String mockHeader;
private String mockBody;
private String mockPayload; 

public MockBean(){
    setMockHeader("mockHeader");
    setMockBody("mockBody");
    setMockPayload("mockPayload");
}



public String getMockHeader(){
    return mockHeader;
}
public String getMockBody(){
    return mockBody;
}
public String getMockPayload(){
    return mockPayload;
}
public void setMockHeader(String header){
    this.mockHeader=header;
}
public void setMockBody(String body){
    this.mockBody = body;
}
public void setMockPayload(String payload) {
    this.mockPayload = payload;
}

public String toString(){
    StringBuilder sb = new StringBuilder();
    sb.append("mockHeader = "+mockHeader+"\n");
    sb.append("mockBody = "+mockBody+"\n");
    sb.append("mockPayload"+mockPayload+"\n");
    String returns = sb.toString();
    return returns;
}
}
</code></pre>
