Object assignment via iterator in Java
<p>I have a piece of code like this:</p>

<pre><code>HashSet&lt;Object&gt; Set = new HashSet&lt;Object&gt;();
//add elements to set 'Set'
Object o = null;
for (Iterator&lt;Object&gt; itr=Set.iterator();itr.hasNext();) {
    o = itr.next();
    //doing some processing on o
}
</code></pre>

<p>I assumed that Object o will point to the Object pointed to by itr. But it is not working as expected. The attributes pointed to by itr.next() is not being copied to o.
Can anybody suggest where I am going wrong? And also, is there some useful post on object assignment like </p>

<pre><code>o1 = o2
</code></pre>

<p>and what happens at memory level in Java?</p>

<p>Below is my actual code:</p>

<p>What I am doing: I have created a set TSet of elements of type Types.AdjList and now I want to process each successive pair of elements of type Types.AdjList and have assigned iterator value during each iteration to two Types.AdjList variables T1 and T2. But T1 and T2 attributes are not matching what the iterator is having.</p>

<p>P.S. Types.AdjList is a HashMap</p>

<pre><code>for (int i=0; i&lt;numT; i++) {
        size = generator.nextInt(10)+1;
        T[i] = new Types().new AdjList(size);
}
HashSet&lt;Types.AdjList&gt; TSet = new HashSet&lt;Types.AdjList&gt;(); 
    for (int i=0; i&lt;T.length; i++) {
        TSet.add(T[i]);
    }
Types.AdjList T1 = null, T2 = null;

for (Iterator&lt;Types.AdjList&gt; itr = TSet.iterator(); itr.hasNext();) {
        T1 = itr.next();
        if (T2 != null) {
            size1 = T1.adj.size();
            //size1 is returning 0, though T1.adj has some elements
            size2 = T2.adj.size();
            //do some processing on T1, T2 based on size1 and size2         
        }
        T2 = T1;
}
</code></pre>

<p>Any help will be appreciated.</p>

<p>Thanks,</p>

<p>Somnath</p>
