I was just wondering if Java provides anything that would allow the following:
<Some List> list = new <Some List>;
Foo f = null;
list.add(f);
f = new Foo();
//Where this would be true
list.contains(f)
//As well as this (which is kind of implied by the contains call)
f == list.get(0)
From what I can tell, this does not work with any of Java's lists. In essence, I am asking if there is a collection type that will update their inner elements in accordance with their external references. In the example above, I would like it so setting 'f' to the new instance of Foo would also be reflected in the list entry. This would also imply the following would be true:
<Some List> list = new <Some List>;
Foo f = null;
list.add(f);
f = new Foo();
f.name = "banana";
//Where this would be true and not cause an NPE, as it does with List
f.name == list.get(0).name //Both would equal "banana"
Does anything like this exist?
Edit:
To clarify, the original object added to the list needs to be null. That object is then updated, and that update should be reflected in the list. In the examples above, the Foo variable is null at first, and is added to the list when it is still null. After adding it, I then set it to a new instance of Foo. I am wondering if there is a list type that, when I instantiate Foo, also updates the list.