ListChangeListener, JavaFX


Question

I have a question related to filters on a ObservableList. The code that i have works fine, but i think is too slow. This is beacause i load like 40,000 orders at the beginning of the app, after that the app keeps receiving orders, but for now i only have the problem in the initial load. My main problem is that the copy of my original collection of orders is considerably slower, why? because the code that i have in the changeListener i think that could be better.. but i haven't found a solution yet. Well here's an example of my code.

public MainController()
{
    filteredData.addAll(Repository.masterObservableList);

    Repository.masterObservableList.addListener(new ListChangeListener<OrderVo>() 
    {
            @Override
            public void onChanged(ListChangeListener.Change<? extends OrderVo> change) 
            {               

                filteredData.clear();                                                              
                for (OrderVo o : Repository.masterObservableList) 
                    filteredData.add(o);
            }
        });
    }    
}

I'll explain the code a little bit. the "Repository" is a singleton, the masterObservableList is a ObservableList and as the name says.. is the "master" or the original. The filteredData is also a ObservableList but is only declared in the controller of my fxml (MainController) and works as the copy of the master collection. Every time that my master collection recieve a change (update or new order) the filteredData should apply that change.. but im doing a for each iteration and this is problem, because it works but, too slow. Why im saying that is slow? Because in the beginning i was using the master collection as the data provider of a TableView that shows the orders and it worked fast and clean. After that i wanted to add filters to the table and thats when i began to do a research and found the filtered data and another methods.. i keeped this method (is a larger code, but the main problem is here).. and it works.. but the time that took to load the orders in the beginning is like 1:30 mins more than before.. So guys, if you have any idea! of how to keep the filteredData updated without making a for each in the changeListener i will be very happy and grateful. Thanks for reading!

1
1
9/10/2013 1:50:06 PM

Your algorithm is forcefully resetting all elements of filteredData everytime masterObservableList changes. This is too exhaustive, as the listener can be triggered upon each item added or removed. Maybe it's better to only add or remove the elements that have changed?

 Repository.masterObservableList.addListener(new ListChangeListener<OrderVo>() 
    {
            @Override
            public void onChanged(ListChangeListener.Change<? extends OrderVo> change) 
            {               

                while(change.next()) {
                    if(change.wasAdded()) {
                        List added =  change.getAddedSubList();
                        //add those elements to your data
                    }
                    if(change.wasRemoved()) {
                        List removed = change.getRemoved();
                        //remove those elements from your data
                    }
                }
            }
        });
    }   
0
11/3/2013 5:33:56 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon