Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an observable list to an array list? Java

Tags:

java

javafx

I'm trying to get all the items in a table view and place them in an array list for further processing. This is what I'm trying to achieve but obviously it won't work.

ArrayList<Consultation> showing = consultationTable.getItems();
like image 574
Philayyy Avatar asked Oct 05 '16 11:10

Philayyy


2 Answers

Nice and recommended solution:

List<Consultation> showing = provider.getItems();

Solution to use only if necessary:

    List<Consultation> consultations = provider.getItems();
    ArrayList<Consultation> showing;
    if (consultations instanceof ArrayList<?>) {
        showing = (ArrayList<Consultation>) consultations;
    } else {
        showing = new ArrayList<>(consultations);
    }

If for some reason you need to use an ArrayList method that is not in the List or ObservableList interface (I cannot readily think of why), you may use the latter.

like image 97
Ole V.V. Avatar answered Nov 16 '22 21:11

Ole V.V.


A simple method using Stream class

List<T> list = ObservableList<T>.stream().collect(Collectors.toList());
like image 29
Jason Amade Avatar answered Nov 16 '22 20:11

Jason Amade