Observable list insertion order changes every time i run this code. how to solve this?

Viewed 147

I want to compare two lists but this tableview only accepts observable list this list always changes its insertion order which is difficult for comparison. how to keep the list in its insertion order??!

thanks in advance

public class MapCompare extends Application {
    TableView<ECOitem> ecotable;
    @Override
    public void start(Stage arg0) throws Exception {
        VBox v=new VBox();
        ecotable = new TableView<>();

        TableColumn<ECOitem, String> changeColoumn = new TableColumn<>("Change");
        changeColoumn.setMinWidth(200);
        changeColoumn.setCellValueFactory(new PropertyValueFactory<>("change"));

        TableColumn<ECOitem, String> changeAnalystColoumn = new TableColumn<>("Change Analyst");
        changeAnalystColoumn.setMinWidth(200);
        changeAnalystColoumn.setCellValueFactory(new PropertyValueFactory<>("changeAnalyst"));

        TableColumn<ECOitem, String> cycleTimeColoumn = new TableColumn<>("Cycle Time");
        cycleTimeColoumn.setMinWidth(200);
        cycleTimeColoumn.setCellValueFactory(new PropertyValueFactory<>("cycleTime"));

        ecotable.setItems(getProduct());

    }

    private ObservableList<ECOitem> getProduct() {

        Map<ECOitem, String> data = new HashMap<ECOitem, String>();
        data.put(new ECOitem("ECO1001223", "sharon", "68:20:32:31"), "1");
        data.put(new ECOitem("ECO1001266", "anna", "52:21:59:53"), "2");
        data.put(new ECOitem("ECO1001258", "sharon.bosman", "48:0:47:33"), "3");
        data.put(new ECOitem("ECO1001255", "shar", "48:0:46:15"), "4");
        data.put(new ECOitem("ECO1001256", "bosman", "48:0:43:39"), "5");
        data.put(new ECOitem("ECO1001257", "shaman", "42:19:11:32"), "6");

        ObservableList<ECOitem> products = FXCollections.observableArrayList();

        for (Map.Entry<ECOitem, String> value : data.entrySet()) {
            products.add(value.getKey());
        }
        System.out.println(products);

System.out.println(products);
        return products;
    }

    public static void main(String[] args) {
        launch(args);
    }

}
1 Answers

That is because the HashMap you are formerly creating doesn't garantee to maintain order.

The observable ArrayList does maintain order.

If order is important here. You have to use the correct data structure for that purpose.

LinkedHashMap is precisely what you're looking for. It an HashMap excepts it iterate over items in their insertion order.

Related