Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ArrayList and ObservableList?

Tags:

java

javafx-2

Currently I am working with database on javafx and have no idea about ObservableList, can I use ArrayList instead of ObservableList?

like image 281
Matiullah Karimi Avatar asked Jan 29 '17 11:01

Matiullah Karimi


3 Answers

That depends. If you need a ObservableList, you cannot use ArrayList directly. ObservableList adds a way to listen for changes on a list which ArrayList does not implement.

However you could use a ArrayList as backing list of a ObservableList

ArrayList<T> list = ...
ObservableList<T> observableList = FXCollections.observableList(list);

Note that in this case you should make sure you're not modifying the list through any means but observableList, since otherwise the listeners won't be triggered.

Note that FXCollections also provides a method for creating a ObservableList backed by a ArrayList without having to deal with the ArrayList itself:

ObservableList<T> observableList = FXCollections.observableArrayList();
like image 101
fabian Avatar answered Sep 22 '22 13:09

fabian


ArrayList: Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.

ObservableList: A list that allows listeners to track changes when they occur.

like image 35
Monzurul Shimul Avatar answered Sep 19 '22 13:09

Monzurul Shimul


It depends of your case. If you want to show this list in for example tableView or other view then you should use Observable collection which contains listeners ect and other components necessery for doing interaction with view.

like image 42
J. Markiewicz Avatar answered Sep 20 '22 13:09

J. Markiewicz