views:

389

answers:

4

Hi,

I was wondering if there is a data structure that acts like an OberservableCollection almost like in C# that is able to take a certain type.

ex:

In C# i am able to say..

 ObservableCollection<Beer> Beer = new ObservableCollection<Beer>();
 Beer.add("Bud"); <br>
 Beer.add("Coors");

Assuming that the class Beer is made and we can change the alcohol content so that

 Beer[1].content = 5;

I was wondering if anyone knows if there is such a data structure/s that work as well with Java.


I am a C# programmer, not much of a Java programmer so just wondering. Also, it has to be able to take in a custom type, not generic.

A: 

org.apache.commons.events.observable Class ObservableCollection

Robert Harvey
A: 

Sure, you can do this. If you had a class called Soda, you could do:

List<Soda> sodas = new ArrayList<Soda>();
sodas.add(new Soda("Coke"));
sodas.add(new Soda("Sprite"));

Then you could do

sodas.get(1).setSugar(255);
broschb
Why would you be using an ArrayList instead of a List<T>?
Ed Swangren
List is the interface, arraylist the implementation. The generic tags didn't show w/ out the code tags that i just added.(happy Snarfblam)
broschb
A: 

If you want to Observe your lists, i.e. be notified when list changes, you can use Glazed Lists.

If you just want to modify objects stored in your lists, you can get your objects by using List.get(int index), or by iterating the list.

If you want to automatically create Beer objects when storing Strings into the list, you'll probably need to write your own simple list wrapper.

Peter Štibraný
A: 

There isn't at least in Java Collections api

http://download-llnw.oracle.com/javase/1.5.0/docs/guide/collections/designfaq.html#27

You can create an wrapper or proxy

yeraycaballero