How can I replace object in java collection?
<p>I am trying to replace element in collection with new modified version. Below is short code that aims to demonstrate what I'd like to achieve. </p>

<p>The whole idea is that I have one object that consists of collections of other objects. At some point in time I am expecting that this objects in collections (in my example phones) might require some modifications and I'd like to modify the code in one place only. </p>

<p>I know that in order to update the object's attributes I can use setters while iterating through the collection as demonstrated below. But maybe there is better, more general way to achieve that.</p>

<pre><code>public class Customer {
    private int id;
    private Collection&lt;Phone&gt; phoneCollection;
    public Customer() {
        phoneCollection = new ArrayList&lt;&gt;();
    }
//getters and setters    

}
</code></pre>

<p>and Phone class</p>

<pre><code>public class Phone {
    private int id;
    private String number;
    private String name;
//getters and setters    
}
</code></pre>

<p>and</p>

<pre><code>public static void main(String[] args) {
        Customer c = new Customer();

        c.addPhone(new Phone(1, "12345", "aaa"));
        c.addPhone(new Phone(2, "34567", "bbb"));
        System.out.println(c);

        Phone p = new Phone(2, "9999999", "new name");

        Collection&lt;Phone&gt; col = c.getPhoneCollection();
        for (Phone phone : col) {
            if (phone.getId() == p.getId()) {
//             This is working fine
//                phone.setNumber(p.getNumber());
//                phone.setName(p.getName());

//              But I'd like to replace whole object if possible and this is not working, at least not that way
                  phone = p;
            }
        }
        System.out.println(c);
    }
}
</code></pre>

<p>Is this possible to achieve what I want? 
I tried copy constructor idea and other methods I found searching the net but none of them was working like I would expect.</p>

<p><strong>EDIT 1</strong></p>

<p>After reading some comments I got an idea</p>

<p>I added the following method to my Phone class</p>

<pre><code>public static void replace(Phone org, Phone dst){
    org.setName(dst.getName());
    org.setNumber(dst.getNumber());
}
</code></pre>

<p>and now my foreach part looks like that</p>

<pre><code>    for (Phone phone : col) {
        if (phone.getId() == p.getId()) {
            Phone.replace(phone, p);
        }
    }
</code></pre>

<p>And it does the job.
Now if I change the Phone class attributes I only need to change that method. Do you think it is OK solving the issue that way?</p>